1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
17 #define EFFICIENT_SYM_SIZE 8
19 static void makeC(const char* properName
)
21 char efficientSym
[256][EFFICIENT_SYM_SIZE
];
22 char quickBuf
[EFFICIENT_SYM_SIZE
];
23 uint8_t buf
[BUF_SIZE
];
25 int readCount
, i
, totalSize
, charCol
;
28 /* Calculate the most efficient symbol that can be used. */
29 for (i
= 0; i
< 256; i
++)
32 memset(efficientSym
[i
], 0, sizeof(efficientSym
[i
]));
34 /* Large numbers are treated as unsigned for certain compilers. */
35 uChar
= (i
>= 128 ? "U" : "");
37 /* Start with normal number. */
38 snprintf(efficientSym
[i
], EFFICIENT_SYM_SIZE
- 1,
41 /* Is octal shorter? */
42 memset(quickBuf
, 0, sizeof(quickBuf
));
43 snprintf(quickBuf
, EFFICIENT_SYM_SIZE
- 1,
45 if (strlen(quickBuf
) < strlen(efficientSym
[i
]))
46 memmove(efficientSym
[i
], quickBuf
, EFFICIENT_SYM_SIZE
);
49 memset(quickBuf
, 0, sizeof(quickBuf
));
50 snprintf(quickBuf
, EFFICIENT_SYM_SIZE
- 1,
52 if (strlen(quickBuf
) < strlen(efficientSym
[i
]))
53 memmove(efficientSym
[i
], quickBuf
, EFFICIENT_SYM_SIZE
);
57 fprintf(stdout
, "#include <sjme/stdTypes.h>\n");
58 fprintf(stdout
, "const sjme_jubyte %s__bin[] = {\n",
61 /* Process all the bytes. */
67 readCount
= fread(buf
, 1, BUF_SIZE
, stdin
);
70 if (readCount
<= 0 && feof(stdin
))
73 /* Process everything. */
74 for (i
= 0; i
< readCount
; i
++)
83 /* Encode to stream, efficiently. */
85 fprintf(stdout
, "%s", efficientSym
[d
& 0xFF]);
90 /* Newline for more space. */
91 charCol
+= strlen(efficientSym
[d
& 0xFF]);
94 fprintf(stdout
, "\n");
100 /* Finish it off and write the size. */
101 fprintf(stdout
, "};\n");
102 fprintf(stdout
, "const sjme_jint %s__len = %d;\n",
103 properName
, totalSize
);
106 static void makeH(const char* properName
)
108 fprintf(stdout
, "#include <sjme/stdTypes.h>\n");
109 fprintf(stdout
, "extern const sjme_jubyte %s__bin[];\n",
111 fprintf(stdout
, "extern const sjme_jint %s__len;\n",
115 int main(int argc
, char** argv
)
118 char properName
[BUF_SIZE
];
121 /* Not enough arguments? */
124 fprintf(stderr
, "Usage: %s fileName C|H\n", argv
[0]);
128 /* Copy the file name. */
129 memset(properName
, 0, sizeof(properName
));
130 snprintf(properName
, BUF_SIZE
- 1, "%s", argv
[1]);
132 /* Normalize all characters accordingly. */
133 for (i
= 0; i
< BUF_SIZE
; i
++)
135 /* Which character? */
141 if (c
>= 'A' && c
<= 'Z')
142 properName
[i
] = 'a' + (c
- 'A');
144 /* Invalid C identifier characters. */
145 else if (!((c
>= 'a' && c
<= 'z') || (i
> 0 && c
>= '0' && c
<= '9')))
149 /* Which kind of output is used? */
150 if (0 == strcmp(argv
[2], "C"))
152 else if (0 == strcmp(argv
[2], "H"))
156 fprintf(stderr
, "Invalid file type: %s.\n", argv
[2]);
160 /* Make sure output is written. */