#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 (isspace(c)) { if (c == ' ') { cp++; continue; }; /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'), * horizontal tab ('\t'), and vertical tab ('\v') */ 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 (isprint(c)) { cp++; continue; }; // Ok, default to \xHH output. memmove(cp + 3, cp, strlen(cp) + 1); *cp = '\\'; cp++; *cp = 'x'; cp++; char buffer[3]; sprintf(buffer, "%02x", (int)c & 0xff); *cp = buffer[0]; cp++; *cp = buffer[1]; cp++; 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; }