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 snprintf _snprintf
21 #define EFFICIENT_SYM_SIZE 8
23 static void makeC(const char* properName
)
25 char efficientSym
[256][EFFICIENT_SYM_SIZE
];
26 char quickBuf
[EFFICIENT_SYM_SIZE
];
27 uint8_t buf
[BUF_SIZE
];
29 int readCount
, i
, totalSize
, charCol
;
32 /* Calculate the most efficient symbol that can be used. */
33 for (i
= 0; i
< 256; i
++)
36 memset(efficientSym
[i
], 0, sizeof(efficientSym
[i
]));
38 /* Large numbers are treated as unsigned for certain compilers. */
39 uChar
= (i
>= 128 ? "U" : "");
41 /* Start with normal number. */
42 snprintf(efficientSym
[i
], EFFICIENT_SYM_SIZE
- 1,
45 /* Is octal shorter? */
46 memset(quickBuf
, 0, sizeof(quickBuf
));
47 snprintf(quickBuf
, EFFICIENT_SYM_SIZE
- 1,
49 if (strlen(quickBuf
) < strlen(efficientSym
[i
]))
50 memmove(efficientSym
[i
], quickBuf
, EFFICIENT_SYM_SIZE
);
53 memset(quickBuf
, 0, sizeof(quickBuf
));
54 snprintf(quickBuf
, EFFICIENT_SYM_SIZE
- 1,
56 if (strlen(quickBuf
) < strlen(efficientSym
[i
]))
57 memmove(efficientSym
[i
], quickBuf
, EFFICIENT_SYM_SIZE
);
61 fprintf(stdout
, "#include \"sjme/stdTypes.h\"\n");
62 fprintf(stdout
, "const sjme_jubyte %s__bin[] = {\n",
65 /* Process all the bytes. */
71 readCount
= fread(buf
, 1, BUF_SIZE
, stdin
);
74 if (readCount
<= 0 && feof(stdin
))
77 /* Process everything. */
78 for (i
= 0; i
< readCount
; i
++)
87 /* Encode to stream, efficiently. */
89 fprintf(stdout
, "%s", efficientSym
[d
& 0xFF]);
94 /* Newline for more space. */
95 charCol
+= strlen(efficientSym
[d
& 0xFF]);
98 fprintf(stdout
, "\n");
104 /* Finish it off and write the size. */
105 fprintf(stdout
, "};\n");
106 fprintf(stdout
, "const sjme_jint %s__len = %d;\n",
107 properName
, totalSize
);
110 static void makeH(const char* properName
)
112 fprintf(stdout
, "#include \"sjme/stdTypes.h\"\n");
113 fprintf(stdout
, "extern const sjme_jubyte %s__bin[];\n",
115 fprintf(stdout
, "extern const sjme_jint %s__len;\n",
119 int main(int argc
, char** argv
)
122 char properName
[BUF_SIZE
];
125 /* Not enough arguments? */
128 fprintf(stderr
, "Usage: %s fileName C|H\n", argv
[0]);
132 /* Copy the file name. */
133 memset(properName
, 0, sizeof(properName
));
134 snprintf(properName
, BUF_SIZE
- 1, "%s", argv
[1]);
136 /* Normalize all characters accordingly. */
137 for (i
= 0; i
< BUF_SIZE
; i
++)
139 /* Which character? */
145 if (c
>= 'A' && c
<= 'Z')
146 properName
[i
] = 'a' + (c
- 'A');
148 /* Invalid C identifier characters. */
149 else if (!((c
>= 'a' && c
<= 'z') || (i
> 0 && c
>= '0' && c
<= '9')))
153 /* Which kind of output is used? */
154 if (0 == strcmp(argv
[2], "C"))
156 else if (0 == strcmp(argv
[2], "H"))
160 fprintf(stderr
, "Invalid file type: %s.\n", argv
[2]);
164 /* Make sure output is written. */