#include #include #include #define ZF_LOGD(...) /** * Display a repr of the given string. * * This converts most \n\r\v\f\t codes, * defaults to \xHH (hex value). */ char *repr(const char *data) { static char buffer[40960]; char *cp; strcpy(buffer, data); cp = buffer; while (*cp != 0) { char c = *cp; if (c == ' ') { cp++; continue; }; /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'), * horizontal tab ('\t'), and vertical tab ('\v') */ if (strchr("\f\n\r\t\v\?", c) != NULL) { memmove(cp + 1, cp, strlen(cp) + 1); *cp = '\\'; cp++; switch (c) { case '\f': *cp = 'f'; cp++; break; case '\n': *cp = 'n'; cp++; break; case '\r': *cp = 'r'; cp++; break; case '\t': *cp = 't'; cp++; break; case '\v': *cp = 'v'; cp++; break; default: *cp = '?'; cp++; break; } continue; } if (c == '\\') { memmove(cp, cp + 1, strlen(cp) + 1); *cp = '\\'; cp += 2; continue; } if (c == '"') { memmove(cp, cp + 1, strlen(cp) + 1); *cp = '\\'; cp += 2; continue; } if (strchr("[()]{}:;,.<>?!@#$%^&*", c) != NULL) { cp++; continue; } if (strchr("0123456789", c) != NULL) { cp++; continue; } if (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", c) != NULL) { cp++; continue; } // Ok, default to \xHH output. memmove(cp + 3, cp, strlen(cp) + 1); char buffer[10]; sprintf(buffer, "\\x%02x", (int)c & 0xff); strncpy(cp, buffer, 4); cp += 4; continue; } return buffer; } int read_file(const char *filename) { FILE *fp; char buffer[10240]; int read; fp = fopen(filename, "rb"); if (fp == NULL) { ZF_LOGD("Failed to open %s", filename); return 0; } strcpy(buffer, filename); char *cp; if ((cp = strchr(buffer, '.')) != NULL) { *cp = 0; } cp = buffer; while (*cp != 0) { if (!isalpha(*cp)) memmove(cp, cp + 1, strlen(cp)); cp++; } printf("const char * %s[] = {\n", buffer); if ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) { char *nl; buffer[read] = 0; cp = repr(buffer); int first = 1; while ((nl = strstr(cp, "\\r\\n")) != NULL) { strcpy(nl, "\""); if (!first) { printf(",\n"); }; first = 0; printf(" \"%s", cp); cp = nl + 4; } if (strlen(cp) > 0) printf(",\n \"%s\"\n", cp); else printf("\n"); printf("};\n"); }; fclose(fp); return 1; } int main(int argc, char *argv[]) { read_file(argv[1]); return 0; }