1 // bitmapcode.cpp - Bitmap code helper.
3 // (c) 2012-2021 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
27 #include <wx/tokenzr.h>
31 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
33 //-------------------------------------------------------------------
34 // CreateCPPFile: Create a CPP file from the filename given.
36 // dirfilename wxString of the directory name.
37 // filename wxString of the filename.
38 // counter Pointer to a integer counter.
39 //-------------------------------------------------------------------
45 CPPFilename = PNGFilename;
46 CPPFilename.RemoveLast(4);
47 CPPFilename.Append(wxT(".cpp"));
49 wxString DirFilenameOnly;
50 wxString CPPFilenameOnly;
52 #if defined(__HAIKU__)
54 #elif defined(__WIN32__)
56 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
57 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
61 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
62 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
66 while (CPPFilenameTokens.HasMoreTokens()){
68 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
72 while (DirFilenameTokens.HasMoreTokens()){
74 DirFilenameOnly = DirFilenameTokens.GetNextToken();
78 // Setup the PNG file reading and cpp file.
80 fstream cppfile, pngfile;
82 #if defined(__WIN32__)
84 pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
85 cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );
89 pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
90 cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
94 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
95 outname.Append(wxT("_"));
96 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
98 outname.RemoveLast(4);
100 // Setup the inclusion guard.
102 cppfile << "#include <iostream>" << endl << endl;
103 cppfile << "#ifndef " << outname.ToStdString() << "_CPP" << endl;
104 cppfile << "#define " << outname.ToStdString() << "_CPP" << endl << endl;
107 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
108 outname.Append(wxT("_"));
109 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
110 outname.RemoveLast(4);
111 outname.Append(wxT("_png"));
114 // Convert the PNG file into an unsigned char array.
116 cppfile << "static unsigned char " << outname.ToStdString() <<
120 pngfile.get(pngfile_char);
123 if ((unsigned short)pngfile_char > 255){
125 cppfile << hex << setw(2) << setfill('0') <<
126 ((unsigned short)pngfile_char - 65280);
130 cppfile << hex << setw(2) << setfill('0') <<
131 (unsigned short)pngfile_char;
145 // End the file, close it and increment the counter.
147 cppfile << "};" << endl << endl;
148 cppfile << "#endif" << endl << endl;
151 cout << "CPP\t" << CPPFilename.ToStdString() << endl;
156 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist,
159 //-------------------------------------------------------------------
160 // CreateHPPFileDir: Create a HPP Directory with the directory
161 // filename and list of files in the directory
164 // dirfilename wxString of the directory name.
165 // filelist wxArrayString of the directory filelist.
166 // counter Pointer to a integer counter.
167 //-------------------------------------------------------------------
172 wxString finaldirname;
173 wxString HPPFilename = dirfilename.ToStdString();
175 wxString DirFilenameOnly;
176 wxString HPPFilenameOnly;
177 wxString CPPFilename;
179 #if defined(__WIN32__)
181 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
182 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
186 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
187 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
191 while (HPPFilenameTokens.HasMoreTokens()){
193 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
197 while (DirFilenameTokens.HasMoreTokens()){
199 DirFilenameOnly = DirFilenameTokens.GetNextToken();
203 // Make the directory filename upper case for writing to the
206 wxString CPPFileOnly;
208 finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
209 finaldirname.MakeUpper();
211 // Write out the header file inclusion guard.
213 HPPFilename.Append(wxT(".h"));
215 #if defined(__WIN32__)
217 hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
221 hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
225 hppfile << "#include <iostream>" << endl << endl;
226 hppfile << "#ifndef " << finaldirname.ToStdString() << "_H" << endl;
227 hppfile << "#define " << finaldirname.ToStdString() << "_H" << endl << endl;
228 hppfile << "// List all CPP files in the directory." << endl << endl;
230 // Write each CPP file into the header file.
232 for (int f = 0; f < filelist.GetCount() ; f++){
234 CPPFilename = filelist[f];
236 #if defined(__HAIKU__)
238 #elif defined(__WIN32__)
240 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
244 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
248 while (CPPFilenameTokenizer.HasMoreTokens()){
250 CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
254 CPPFileOnly.RemoveLast(4);
255 CPPFileOnly.Append(wxT(".cpp"));
257 hppfile << "#include \"" << DirFilenameOnly.ToStdString() <<
258 "/" << CPPFileOnly.ToStdString() << "\"" << endl;
262 // Write the end if and close the file.
264 hppfile << endl << "#endif" << endl << endl;
267 // Increment the HPP file counter.
269 cout << "HPPDIR\t" << HPPFilename.ToStdString() << endl;
274 int main(int argc, char *argv[])
281 wxArrayString dirlist;
282 wxArrayString filelist;
283 wxString BitmapHeaderFilename;
284 wxString DirFilenameWxS;
286 // Check if completed file exists before doing anything
287 // else and write an error message if it does exist.
289 if (wxFileExists(wxT("BitmapsDone"))){
291 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
292 std::cout << "To regenerate the files simply delete the BitmapsDone file where this helper application is run." << std::endl;
298 fstream finalhppfile;
301 std::cout << "Error: No directory name given!" << std::endl;
305 // Look in the subdirectories of the bitmaps directory and
306 // collect the names of the directories.
308 std::cout << "Working out directories in bitmaps directory..." <<
311 const char *dirarg = argv[1];
313 wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
315 #if defined(__HAIKU__)
317 #elif defined(__WIN32__)
319 BitmapHeaderFilename.Append(BitmapDirName);
320 BitmapHeaderFilename.Append(wxT("\\..\\Bitmaps.h"));
324 BitmapHeaderFilename.Append(BitmapDirName);
325 BitmapHeaderFilename.Append(wxT("/../Bitmaps.h"));
329 if ( wxDirExists(BitmapDirName) ){
331 wxDir BitmapDir(BitmapDirName);
333 wxString BitmapSubDir;
334 wxString BitmapSubDirFull;
336 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
338 while (ContinueProcess){
340 #if defined(__HAIKU__)
342 BitmapSubDirFull.Append(BitmapSubDir);
343 BitmapSubDirFull.Append();
345 #elif defined(__WIN32__)
347 BitmapSubDirFull.Append(BitmapDirName);
348 BitmapSubDirFull.Append(wxT("\\"));
349 BitmapSubDirFull.Append(BitmapSubDir);
353 BitmapSubDirFull.Append(BitmapDirName);
354 BitmapSubDirFull.Append(wxT("/"));
355 BitmapSubDirFull.Append(BitmapSubDir);
359 if (wxDirExists(BitmapSubDirFull)){
360 dirlist.Add(BitmapSubDirFull);
363 ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
365 BitmapSubDirFull.Clear();
370 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
374 // Process each directory, generating a .cpp and .hpp file
375 // for each image and then a final .hpp for the directory
376 // containing the .hpp's for the directories.
378 if (dirlist.GetCount() == 0){
379 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
383 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
385 DirFilenameWxS.Empty();
387 std::cout << BitmapHeaderFilename.ToStdString() << std::endl;
389 #if defined(__WIN32__)
391 finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
395 finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
399 finalhppfile << "#include <iostream>" << endl << endl;
400 finalhppfile << "#ifndef BITMAPS_H" << endl;
401 finalhppfile << "#define BITMAPS_H" << endl << endl;
403 for (int bi = 0; bi < dirlist.GetCount(); bi++)
406 wxString BitmapSubDirName = dirlist[bi];
407 wxString BitmapFilename;
408 wxDir BitmapSubDir(BitmapSubDirName);
410 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
412 while (ContinueProcess){
414 if (BitmapFilename.Right(4) == wxT(".PNG") ||
416 BitmapFilename.Right(4) == wxT(".png")){
418 #if defined(__HAIKU__)
420 BitmapSubDirFull.Append(BitmapSubDir);
421 BitmapSubDirFull.Append();
423 #elif defined(__WIN32__)
425 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
426 DirFilenameWxS.Append(wxT("\\"));
427 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
431 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
432 DirFilenameWxS.Append(wxString::FromUTF8("/"));
433 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
437 filelist.Add(DirFilenameWxS);
441 DirFilenameWxS.Clear();
442 ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
446 for (int fi = 0; fi < filelist.GetCount(); fi++)
449 CreateCPPFile(dirlist[bi].ToStdString(), filelist[fi].ToStdString(), &cppg);
454 if (filelist.GetCount() > 0)
457 CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
463 wxString DirNameSplit;
465 #if defined(__HAIKU__)
467 #elif defined(__WIN32__)
469 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
473 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
478 while (DirListFile.HasMoreTokens()){
480 DirNameSplit = DirListFile.GetNextToken();
484 #if defined(__HAIKU__)
486 #elif defined(__WIN32__)
488 finalhppfile << "#include \"Bitmaps\\" << DirNameSplit.ToStdString() <<
493 finalhppfile << "#include \"Bitmaps/" << DirNameSplit.ToStdString() <<
500 finalhppfile << endl << "#endif" << endl;
501 finalhppfile.close();
504 // Print out the results.
506 std::cout << "Finished processing PNGs into code." << std::endl;
507 std::cout << fp << " files processed." << std::endl;
508 std::cout << cppg << " .cpp files generated." << std::endl;
509 std::cout << hppg << " .hpp files generated." << std::endl;
511 // Write a success flag so that future runs won't take
512 // place thus speeding up the compilation process as required.
516 bitmapflag.open("BitmapsDone", ios::out | ios::trunc);
517 bitmapflag << "Bitmaps as code generated. To recreate, simply delete the BitmapsDone file and run the bitmap code generation tool again." << endl;