Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / tools / leveldesign / uni_conv / uni_conv.cpp
blobc43265a7fda68d3b3cffc4e6c253c0f10cf238e9
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <nel/misc/types_nl.h>
22 #include <nel/misc/ucstring.h>
23 #include <nel/misc/utf_string_view.h>
24 #include <nel/misc/common.h>
25 #include <nel/misc/sstring.h>
26 #include <nel/misc/i18n.h>
28 using namespace std;
29 using namespace NLMISC;
31 void usage()
33 printf("uni_conv [option] <output_mode> <input_file> <output_file>\n");
34 printf("output_mode is one of:\n");
35 printf("\t-8 output in UTF-8\n");
36 printf("\t-16 output in UTF-16\n");
37 printf("\t-a output in ascii, non ascii char are silently removed\n");
38 printf("option:\n");
39 printf("\t-x minimal support for XML : encode &, < and > in XML chat tag\n");
42 int main(int argc, char *argv[])
44 new NLMISC::CApplicationContext;
46 bool xmlSupport = false;
47 enum TOutMode {UNDEF, UTF8, UTF16, ASCII};
48 CSString inputFile;
49 CSString outputFile;
50 TOutMode outMode = UNDEF;
52 if (argc < 4)
54 usage();
55 return -1;
57 for (int i=1; i<argc; ++i)
59 CSString arg = argv[i];
60 if (arg == "-x")
61 xmlSupport = true;
62 else if (arg == "-8")
63 outMode = UTF8;
64 else if (arg == "-16")
65 outMode = UTF16;
66 else if (arg == "-a")
67 outMode = ASCII;
68 else if (i == argc-1)
69 outputFile = arg;
70 else if (i == argc-2)
71 inputFile = arg;
74 if (outputFile.empty() || inputFile.empty() || outMode == UNDEF)
76 usage();
77 return -1;
80 ucstring str;
81 CI18N::readTextFile(inputFile, str, false, false);
83 if (xmlSupport)
85 ucstring temp;
86 temp.reserve(str.size());
87 for (uint i=0; i<str.size(); ++i)
89 switch(str[i])
91 case '&':
92 temp += "&amp;";
93 break;
94 case '<':
95 temp += "&gt;";
96 break;
97 case '>':
98 temp += "&lt;";
99 break;
100 default:
101 temp += str[i];
104 str = temp;
107 switch(outMode)
109 case UTF8:
110 CI18N::writeTextFile(outputFile, str, true);
111 break;
112 case UTF16:
113 CI18N::writeTextFile(outputFile, str, false);
114 break;
115 case ASCII:
117 std::string res;
118 res.reserve(str.size());
119 for (ucstring::const_iterator it(str.begin()), end(str.end()); it != end; ++it)
121 ucchar c = *it;
122 if (c < 0x80)
123 res += (char)c;
125 FILE *fp = nlfopen(outputFile, "wt");
126 fwrite(res.data(), res.size(), 1, fp);
127 fclose(fp);
129 break;
130 default:
131 break;