// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html #include #include #include #include #include #include #include // We only use U8_* macros, which are entirely inline. #include "unicode/utf8.h" // This contains a codepage and ISO 14882:1998 illegality table. // Use "make gen-table" to rebuild it. #include "cptbl.h" /** * What is this? * * "This" is a preprocessor that makes an attempt to convert fully valid C++11 source code * in utf-8 into something consumable by certain compilers (Solaris, xlC) * which aren't quite standards compliant. * * - u"" or u'' gets converted to u"\uNNNN" or u'\uNNNN' * - u8"" gets converted to "\xAA\xBB\xCC\xDD" etc. * (some compilers do not support the u8 prefix correctly.) * - if the system is EBCDIC-based, that is used to correct the input characters. * * Usage: * escapesrc infile.cpp outfile.cpp * Normally this is invoked by the build stage, with a rule such as: * * _%.cpp: $(srcdir)/%.cpp * @$(BINDIR)/escapesrc$(EXEEXT) $< $@ * %.o: _%.cpp * $(COMPILE.cc) ... $@ $< * * In the Makefiles, SKIP_ESCAPING=YES is used to prevent escapesrc.cpp * from being itself escaped. */ static const char kSPACE = 0x20, kTAB = 0x09, kLF = 0x0A, kCR = 0x0D; // For convenience # define cp1047_to_8859(c) cp1047_8859_1[c] // Our app's name std::string prog; /** * Give the usual 1-line documentation and exit */ void usage() { fprintf(stderr, "%s: usage: %s infile.cpp outfile.cpp\n", prog.c_str(), prog.c_str()); } /** * Delete the output file (if any) * We want to delete even if we didn't generate, because it might be stale. */ int cleanup(const std::string &outfile) { const char *outstr = outfile.c_str(); if(outstr && *outstr) { int rc = std::remove(outstr); if(rc == 0) { fprintf(stderr, "%s: deleted %s\n", prog.c_str(), outstr); return 0; } else { if( errno == ENOENT ) { return 0; // File did not exist - no error. } else { perror("std::remove"); return 1; } } } return 0; } /** * Skip across any known whitespace. * @param p startpoint * @param e limit * @return first non-whitespace char */ inline const char *skipws(const char *p, const char *e) { for(;p0; pos2++,trail--) { linestr[pos2] = cp1047_to_8859(linestr[pos2]); if(linestr[pos2] == 0x0A) { linestr[pos2] = 0x85; // NL is ambiguous here } } #endif // Proceed to decode utf-8 const uint8_t *s = (const uint8_t*) (linestr.c_str()); int32_t length = linestr.size(); UChar32 c; if(U8_IS_SINGLE((uint8_t)s[i]) && oldIllegal[s[i]]) { #if (U_CHARSET_FAMILY == U_EBCDIC_FAMILY) linestr[pos] = old_byte; // put it back #endif continue; // single code point not previously legal for \u escaping } // otherwise, convert it to \u / \U { U8_NEXT(s, i, length, c); } if(c<0) { fprintf(stderr, "Illegal utf-8 sequence at Column: %d\n", (int)old_pos); fprintf(stderr, "Line: >>%s<<\n", linestr.c_str()); return true; } size_t seqLen = (i-pos); //printf("U+%04X pos %d [len %d]\n", c, pos, seqLen);fflush(stdout); char newSeq[20]; if( c <= 0xFFFF) { sprintf(newSeq, "\\u%04X", c); } else { sprintf(newSeq, "\\U%08X", c); } linestr.replace(pos, seqLen, newSeq); pos += strlen(newSeq) - 1; } } return false; } /** * Fixup an entire line * false = no err * true = had err * @param no the line number (not used) * @param linestr the string to fix * @return true if any err, else false */ bool fixLine(int /*no*/, std::string &linestr) { const char *line = linestr.c_str(); size_t len = linestr.size(); // no u' in the line? if(!strstr(line, "u'") && !strstr(line, "u\"") && !strstr(line, "u8\"")) { return false; // Nothing to do. No u' or u" detected } // start from the end and find all u" cases size_t pos = len = linestr.size(); while((pos>0) && (pos = linestr.rfind("u\"", pos)) != std::string::npos) { //printf("found doublequote at %d\n", pos); if(fixAt(linestr, pos)) return true; if(pos == 0) break; pos--; } // reset and find all u' cases pos = len = linestr.size(); while((pos>0) && (pos = linestr.rfind("u'", pos)) != std::string::npos) { //printf("found singlequote at %d\n", pos); if(fixAt(linestr, pos)) return true; if(pos == 0) break; pos--; } // reset and find all u8" cases pos = len = linestr.size(); while((pos>0) && (pos = linestr.rfind("u8\"", pos)) != std::string::npos) { if(fixAt(linestr, pos)) return true; if(pos == 0) break; pos--; } //fprintf(stderr, "%d - fixed\n", no); return false; } /** * Convert a whole file * @param infile * @param outfile * @return 1 on err, 0 otherwise */ int convert(const std::string &infile, const std::string &outfile) { fprintf(stderr, "escapesrc: %s -> %s\n", infile.c_str(), outfile.c_str()); std::ifstream inf; inf.open(infile.c_str(), std::ios::in); if(!inf.is_open()) { fprintf(stderr, "%s: could not open input file %s\n", prog.c_str(), infile.c_str()); cleanup(outfile); return 1; } std::ofstream outf; outf.open(outfile.c_str(), std::ios::out); if(!outf.is_open()) { fprintf(stderr, "%s: could not open output file %s\n", prog.c_str(), outfile.c_str()); return 1; } // TODO: any platform variations of #line? outf << "#line 1 \"" << infile << "\"" << '\n'; int no = 0; std::string linestr; while( getline( inf, linestr)) { no++; if(fixLine(no, linestr)) { outf.close(); fprintf(stderr, "%s:%d: Fixup failed by %s\n", infile.c_str(), no, prog.c_str()); cleanup(outfile); return 1; } outf << linestr << '\n'; } return 0; } /** * Main function */ int main(int argc, const char *argv[]) { prog = argv[0]; if(argc != 3) { usage(); return 1; } std::string infile = argv[1]; std::string outfile = argv[2]; return convert(infile, outfile); }