/* RemoveT - removes _T macro from C source files This program removes the _T macro from all files listed on the command line. Each occurence of _T("somestring") will be changed into "somestring", whereby 'somestring' stands for an arbitrary C string. The program does take into account escape sequences. This program is mentioned at: http://www.iwriteiam.nl/D1502.html#25 Copyright (C) 2015 Frans Faase http://www.iwriteiam.nl/ 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 */ #include #include #include #include int main(int argc, char *argv[]) { // Process all files on the input: for (int i = 1; i < argc; i++) { char *arg = argv[i]; FILE *fin = fopen(arg, "rb"); if (fin == 0) { fprintf(stderr, "File '%s' could not be opened\n", arg); } else { // Read file into buffer: char* buffer; { int fh = fileno(fin); long file_len = lseek(fh, 0L, SEEK_END); lseek(fh, 0L, SEEK_SET); buffer = new char[file_len+2]; file_len = read(fh, buffer, file_len); buffer[file_len] = '\0'; } fclose(fin); // Check if it does contain a _T macro: bool found = false; char *s = buffer; for (;;) { // Find the following occurence of the _T macro: char *sf = strstr(s, "_T("); if (sf == 0 ) break; if (!isalnum(sf[-1]) && sf[-1] != '_') { found = true; break; } s = sf + 3; } if (found) { FILE *fout = fopen(arg, "wb"); if (fout == 0) { fprintf(stderr, "File '%s' could not be opened for writing\n", arg); } else { int nr_removed = 0; char *s = buffer; for (;;) { // Find the following occurence of the _T macro: char *sf = strstr(s, "_T("); if (sf == 0 ) { // Process remainder of the file for(; *s != '\0'; s++) fprintf(fout, "%c", *s); break; } if (isalnum(sf[-1]) || sf[-1] == '_') { // Some identifier character found for _T; skip and copy: sf += 3; for ( ; s < sf; s++) fprintf(fout, "%c", *s); } else { nr_removed++; // Copy till start of macro: for (; s < sf; s++) fprintf(fout, "%c", *s); s += 3; // Copy spaces before the string for (; isspace(*s); s++) fprintf(fout, "%c", *s); // Copy string or character (if found) while (*s == '"' || *s == '\'') { char quote = *s; s++; fprintf(fout, "%c", quote); for (; *s != '\0' && *s != quote; s++) { fprintf(fout, "%c", *s); if (*s == '\\') { s++; fprintf(fout, "%c", *s); } } if (*s == quote) { fprintf(fout, "%c", quote); // Copy spaces following the string for (s++; isspace(*s); s++) fprintf(fout, "%c", *s); } } // Skip closing bracket if (*s == ')') s++; } } fprintf(stderr, "File '%s': removed %d _T macros\n", arg, nr_removed); fclose(fout); } } delete buffer; } } return 0; }