Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
gencat.cpp
Go to the documentation of this file.
1
27#include <cstdlib> // for system(), getenv()
28#include <cstdio> // for printf()
29#include <cstring> // for strcmp(), strrchr()
30#include <cstddef> // for size_t
31#include <cassert> // for assert()
32
33#include <string>
34
35static const char
36usage_text[] = {
37 "Usage: %s OUTPUT-FILE INPUT-FILE\n"
38 "Generate message catalog.\n"
39 "\n"
40 " -?, --help Give this help list\n"
41};
42
43#ifdef _WIN32
44# define SLASH '\\'
45
46// replace file extension in str by new extension ext
47static void change_ext (std::string& str, const char* ext)
48{
49 const std::string::size_type npos = std::string::npos;
50 std::string::size_type dot_pos = str.find_last_of ('.');
51 std::string::size_type quote_pos =
52 npos == dot_pos ? str.find_last_of ('\"')
53 : str.find_first_of ('\"', dot_pos);
54 if (npos == quote_pos)
55 quote_pos = str.size ();
56 if (npos == dot_pos)
57 dot_pos = quote_pos;
58 str.replace (dot_pos, quote_pos - dot_pos, ext);
59}
60
61#else // !_WIN32
62# define SLASH '/'
63#endif // _WIN32
64
65int main (int argc, char *argv[])
66{
67 const char* exe_name = "gencat";
68
69 if (argv [0]) {
70 if (const char* slash = std::strrchr (argv [0], SLASH))
71 exe_name = slash + 1;
72 else
73 exe_name = argv [0];
74 }
75
77
78 if (1 == argc) {
79 std::printf (usage_text, exe_name);
80 return 0;
81 }
82
83 --argc;
84
85 while (0 != *++argv && 0 < argc-- && '-' == **argv) {
86
87 switch (*++*argv) {
88
89 case '?':
90 std::printf (usage_text, exe_name);
91 return 0;
92
93 case '-':
94 if (0 == std::strcmp (*argv, "-help")) {
95 std::printf (usage_text, exe_name);
96 return 0;
97 }
98
99 // fall through...
100 default:
101 std::printf ("%s: invalid option -%s\n",
102 exe_name, *argv);
103 return 1;
104 }
105 }
106
107 if (1 > argc) {
108 std::printf ("%s: missing arguments\n Try '%s --help'\n",
110 return 1;
111 }
112
113 std::string cmd;
114
115#ifdef _WIN32
116
117# ifndef __MINGW32__
118
119 const char* const env_vars [] = {
120 "VS90COMNTOOLS", "VS80COMNTOOLS",
121 "VS71COMNTOOLS", "VSCOMNTOOLS"
122 };
123
124 for (size_t i = 0; i < sizeof (env_vars) / sizeof (*env_vars); ++i) {
125 if (const char* vcvarspath = std::getenv (env_vars [i])) {
126 cmd = vcvarspath;
127 cmd += "vsvars32.bat";
128 break;
129 }
130 }
131
132 if (std::string::npos != cmd.find (' ')) {
133 cmd.insert (cmd.begin (), 1, '\"');
134 cmd.push_back ('\"');
135 }
136
137 if (!cmd.empty ())
138 cmd += " && ";
139
140# endif // !__MINGW32__
141
142 const char* const dll_name = argv [0];
143 const char* const rc_name = argv [1];
144
145 std::string res_name (rc_name);
146 change_ext (res_name, ".res");
147
148# ifdef __MINGW32__
149
150 cmd += "windres -O coff -i ";
151 cmd += rc_name;
152 cmd += " -o ";
153 cmd += res_name;
154 cmd += " && gcc -shared -mno-cygwin -o ";
155 cmd += dll_name;
156 cmd += ' ';
157 cmd += res_name;
158
159# else
160
161# ifndef _WIN64
162# define PLATFORM "X86"
163# else // _WIN64
164# define PLATFORM "X64"
165# endif // _WIN64
166
167 cmd += "rc ";
168 cmd += rc_name;
169 cmd += " && link /NOLOGO /DLL /NOENTRY /MACHINE:" PLATFORM " /OUT:";
170 cmd += dll_name;
171 cmd += ' ';
172 cmd += res_name;
173
174# endif // __MINGW32__
175
176 const int ret = std::system (cmd.c_str ());
177
178 std::remove (res_name.c_str ());
179
180#else // !_WIN32
181
182 const char* const cat_name = argv [0];
183 const char* const msg_name = argv [1];
184
185 cmd = "/usr/bin/gencat ";
186 cmd += cat_name;
187 cmd += ' ';
188 cmd += msg_name;
189
190 const int ret = std::system (cmd.c_str ());
191
192#endif // _WIN32
193
194 return ret;
195}
#define i
Definition 17.names.cpp:126
#define main
#define assert(expr)
Definition _cassert.h:40
const char * str
Definition exec.cpp:170
#define SLASH
Definition gencat.cpp:62
const char * exe_name
Definition cmdopt.cpp:55