Setup and clear of string pool.
[SquirrelJME.git] / nanocoat / cmake / utils / sourceize.c
blob8b77b0f49d39a31d1392ae17c3897fc623150f96
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "sjmeint.h"
16 #define BUF_SIZE 4096
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];
24 uint8_t d;
25 int readCount, i, totalSize, charCol;
26 const char* uChar;
28 /* Calculate the most efficient symbol that can be used. */
29 for (i = 0; i < 256; i++)
31 /* Reset. */
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,
39 "%d%s", i, uChar);
41 /* Is octal shorter? */
42 memset(quickBuf, 0, sizeof(quickBuf));
43 snprintf(quickBuf, EFFICIENT_SYM_SIZE - 1,
44 "0%o%s", i, uChar);
45 if (strlen(quickBuf) < strlen(efficientSym[i]))
46 memmove(efficientSym[i], quickBuf, EFFICIENT_SYM_SIZE);
48 /* Is hex shorter? */
49 memset(quickBuf, 0, sizeof(quickBuf));
50 snprintf(quickBuf, EFFICIENT_SYM_SIZE - 1,
51 "0x%x%s", i, uChar);
52 if (strlen(quickBuf) < strlen(efficientSym[i]))
53 memmove(efficientSym[i], quickBuf, EFFICIENT_SYM_SIZE);
56 /* Start header. */
57 fprintf(stdout, "#include <sjme/stdTypes.h>\n");
58 fprintf(stdout, "const sjme_jubyte %s__bin[] = {\n",
59 properName);
61 /* Process all the bytes. */
62 totalSize = 0;
63 charCol = 0;
64 for (;;)
66 /* Read in. */
67 readCount = fread(buf, 1, BUF_SIZE, stdin);
69 /* EOF? */
70 if (readCount <= 0 && feof(stdin))
71 break;
73 /* Process everything. */
74 for (i = 0; i < readCount; i++)
76 /* Comma? */
77 if (totalSize > 0)
79 fprintf(stdout, ",");
80 charCol++;
83 /* Encode to stream, efficiently. */
84 d = buf[i];
85 fprintf(stdout, "%s", efficientSym[d & 0xFF]);
87 /* Count size up. */
88 totalSize++;
90 /* Newline for more space. */
91 charCol += strlen(efficientSym[d & 0xFF]);
92 if (charCol > 60)
94 fprintf(stdout, "\n");
95 charCol = 0;
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",
110 properName);
111 fprintf(stdout, "extern const sjme_jint %s__len;\n",
112 properName);
115 int main(int argc, char** argv)
117 char c;
118 char properName[BUF_SIZE];
119 int i;
121 /* Not enough arguments? */
122 if (argc <= 2)
124 fprintf(stderr, "Usage: %s fileName C|H\n", argv[0]);
125 return EXIT_FAILURE;
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? */
136 c = properName[i];
137 if (c == 0)
138 break;
140 /* Lowercase it. */
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')))
146 properName[i] = '_';
149 /* Which kind of output is used? */
150 if (0 == strcmp(argv[2], "C"))
151 makeC(properName);
152 else if (0 == strcmp(argv[2], "H"))
153 makeH(properName);
154 else
156 fprintf(stderr, "Invalid file type: %s.\n", argv[2]);
157 return EXIT_FAILURE;
160 /* Make sure output is written. */
161 fflush(stdout);
163 /* Success! */
164 return EXIT_SUCCESS;