/* Strip JavaScript Copyright (C) 2015 Frans Faase This program is a simple program that strips JavaScript files from unneccessary spaces and comments. It also strips 'src' fields from data structures. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. GNU General Public License: http://www.iwriteiam.nl/GNU.txt Contact: http://www.iwriteiam.nl/ */ #include #include #include char scan(FILE* fin, char &ch, char *buffer) { // skip spaces: while (!feof(fin) && (ch == ' ' || ch == '\t' || ch == '/')) if (ch == '/') { ch = fgetc(fin); if (feof(fin)) return '\0'; if (ch == '*') { for (;;) { ch = fgetc(fin); if (feof(fin)) return '\0'; if (ch == '*') { ch = fgetc(fin); if (feof(fin)) return '\0'; if (ch == '/') { ch = fgetc(fin); break; } } } } else if (ch == '/') { for (;;) { ch = fgetc(fin); if (feof(fin)) return '\0'; if (ch == '\n') { ch = fgetc(fin); break; } } } else { ch = fgetc(fin); return '/'; } } else ch = fgetc(fin); if (feof(fin)) return '\0'; if (ch == '\n') { ch = fgetc(fin); return '\n'; } if (isalpha(ch)) { int i = 0; for (;;) { buffer[i++] = ch; ch = fgetc(fin); if (feof(fin) || !isalnum(ch) || ch == '_') break; } buffer[i] = '\0'; return 'a'; } if (isdigit(ch)) { int i = 0; for (;;) { buffer[i++] = ch; ch = fgetc(fin); if (feof(fin) || !isdigit(ch) || ch == '.') break; } buffer[i] = '\0'; return '0'; } if (ch == '"') { int i = 0; for (;;) { buffer[i++] = ch; char prev_ch = ch; ch = fgetc(fin); if (feof(fin)) break; if (ch == '"' && prev_ch != '\\') { buffer[i++] = ch; ch = fgetc(fin); break; } } buffer[i] = '\0'; return '"'; } char result = ch; ch = fgetc(fin); return result; } int main(int argc, char* argv[]) { FILE* fin = stdin; //fopen("PeterStruyckenSrc.js", "rt"); FILE* fout = stdout; //fopen("PeterStruycken.js", "wt"); char ch = fgetc(fin); char buffer[400]; char prev_type; char type = '\n'; bool prev_complex = false; bool skip_scan = false; for (;;) { if (skip_scan) skip_scan = false; else { prev_type = type; type = scan(fin, ch, buffer); } if (type == '\0') break; if (type == 'a' || type == '0') { if (prev_complex) fprintf(fout, " %s", buffer); else { fprintf(fout, "%s", buffer); prev_complex = true; } } else if (type == '"') { fprintf(fout, "%s", buffer); prev_complex = false; } else if (type == '\n' && prev_type == '\n') { // ignore repeated newlines } else if (type == ',') { bool push_prev_complex = prev_complex; type = scan(fin, ch, buffer); if (type != 'a' || strcmp(buffer, "src") != 0) { fprintf(fout, ","); prev_complex = false; skip_scan = true; } else { type = scan(fin, ch, buffer); if (type != ':') { fprintf(fout, ",src"); prev_complex = true; skip_scan = true; } else { type = scan(fin, ch, buffer); if (type == '[') { int depth = 1; while (depth > 0 && type != '\0') { type = scan(fin, ch, buffer); if (type == '[') depth++; else if (type == ']') depth--; } } prev_complex = push_prev_complex; } } } else { fprintf(fout, "%c", type); prev_complex = false; } } }