Set calling convention for sjme_scritchui_core_grabExternalThreadId.
[SquirrelJME.git] / nanocoat / cmake / utils / sourceize.c
blob2f25cbf9bf09322c5e78a1805e0caff8aa5859e5
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 #if defined(_MSC_VER)
17 #define snprintf _snprintf
18 #endif
20 #define BUF_SIZE 4096
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];
28 uint8_t d;
29 int readCount, i, totalSize, charCol;
30 const char* uChar;
32 /* Calculate the most efficient symbol that can be used. */
33 for (i = 0; i < 256; i++)
35 /* Reset. */
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,
43 "%d%s", i, uChar);
45 /* Is octal shorter? */
46 memset(quickBuf, 0, sizeof(quickBuf));
47 snprintf(quickBuf, EFFICIENT_SYM_SIZE - 1,
48 "0%o%s", i, uChar);
49 if (strlen(quickBuf) < strlen(efficientSym[i]))
50 memmove(efficientSym[i], quickBuf, EFFICIENT_SYM_SIZE);
52 /* Is hex shorter? */
53 memset(quickBuf, 0, sizeof(quickBuf));
54 snprintf(quickBuf, EFFICIENT_SYM_SIZE - 1,
55 "0x%x%s", i, uChar);
56 if (strlen(quickBuf) < strlen(efficientSym[i]))
57 memmove(efficientSym[i], quickBuf, EFFICIENT_SYM_SIZE);
60 /* Start header. */
61 fprintf(stdout, "#include \"sjme/stdTypes.h\"\n");
62 fprintf(stdout, "const sjme_jubyte %s__bin[] = {\n",
63 properName);
65 /* Process all the bytes. */
66 totalSize = 0;
67 charCol = 0;
68 for (;;)
70 /* Read in. */
71 readCount = fread(buf, 1, BUF_SIZE, stdin);
73 /* EOF? */
74 if (readCount <= 0 && feof(stdin))
75 break;
77 /* Process everything. */
78 for (i = 0; i < readCount; i++)
80 /* Comma? */
81 if (totalSize > 0)
83 fprintf(stdout, ",");
84 charCol++;
87 /* Encode to stream, efficiently. */
88 d = buf[i];
89 fprintf(stdout, "%s", efficientSym[d & 0xFF]);
91 /* Count size up. */
92 totalSize++;
94 /* Newline for more space. */
95 charCol += strlen(efficientSym[d & 0xFF]);
96 if (charCol > 60)
98 fprintf(stdout, "\n");
99 charCol = 0;
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",
114 properName);
115 fprintf(stdout, "extern const sjme_jint %s__len;\n",
116 properName);
119 int main(int argc, char** argv)
121 char c;
122 char properName[BUF_SIZE];
123 int i;
125 /* Not enough arguments? */
126 if (argc <= 2)
128 fprintf(stderr, "Usage: %s fileName C|H\n", argv[0]);
129 return EXIT_FAILURE;
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? */
140 c = properName[i];
141 if (c == 0)
142 break;
144 /* Lowercase it. */
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')))
150 properName[i] = '_';
153 /* Which kind of output is used? */
154 if (0 == strcmp(argv[2], "C"))
155 makeC(properName);
156 else if (0 == strcmp(argv[2], "H"))
157 makeH(properName);
158 else
160 fprintf(stderr, "Invalid file type: %s.\n", argv[2]);
161 return EXIT_FAILURE;
164 /* Make sure output is written. */
165 fflush(stdout);
167 /* Success! */
168 return EXIT_SUCCESS;