#include "stdio.h" #include "string.h" char *staticTabs(int nTabs) { static char szTabs[32]; int i; for (i = 0; i < nTabs; i++) szTabs[i] = '\t'; szTabs[i] = 0; return szTabs; } int main(int argc, const char **argv) { if (argc < 3) printf("Syntax: list2txt "); FILE *pIn = NULL; FILE *pOut = NULL; if (!(pIn = fopen(argv[1], "rb"))) printf("Could not open %s.", argv[1]); if (!(pOut = fopen(argv[2], "wb"))) printf("Could not open %s.", argv[2]); if (pIn && pOut) { int nTabLevel = 0; bool bInString = false; while(!feof(pIn)) { char cIn; char szOut[32]; if (!fread(&cIn, 1, 1, pIn)) continue; switch (cIn) { case ' ': if (!bInString) szOut[0] = 0; else strcpy(szOut, " "); break; case '\"': { // if (bInString) // { // } bInString = !bInString; strcpy(szOut, "\""); } break; case '\\': { fread(&cIn, 1, 1, pIn); szOut[0] = '\\'; szOut[1] = cIn; szOut[2] = 0; } break; case ',': if (!bInString) { strcpy(szOut, ",\n"); strcat(szOut, staticTabs(nTabLevel)); } else strcpy(szOut, ","); break; case '{': { nTabLevel++; strcpy(szOut, "{\n"); strcat(szOut, staticTabs(nTabLevel)); } break; case '}': { nTabLevel--; strcpy(szOut, "\n"); strcat(szOut, staticTabs(nTabLevel)); strcat(szOut, "}"); // strcat(szOut, "}\n"); // strcat(szOut, staticTabs(nTabLevel)); } break; // parse in/out of strings, tabs case 13: // fall through case 10: szOut[0] = 0; break; default: szOut[0] = cIn; szOut[1] = 0; } fwrite(szOut, 1, strlen(szOut), pOut); printf(szOut); } } if (pIn) fclose(pIn); if (pOut) fclose(pOut); return 0; } .