1 // bitmapcode.cpp - Bitmap code helper.
3 // (c) 2012-2015 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.c_str() << "_CPP" << endl;
104 cppfile << "#define " << outname.c_str() << "_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.c_str() <<
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.c_str() << endl;
157 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist,
160 //-------------------------------------------------------------------
161 // CreateHPPFileDir: Create a HPP Directory with the directory
162 // filename and list of files in the directory
165 // dirfilename wxString of the directory name.
166 // filelist wxArrayString of the directory filelist.
167 // counter Pointer to a integer counter.
168 //-------------------------------------------------------------------
173 wxString finaldirname;
174 wxString HPPFilename = dirfilename.c_str();
176 wxString DirFilenameOnly;
177 wxString HPPFilenameOnly;
178 wxString CPPFilename;
180 #if defined(__WIN32__)
182 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
183 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
187 wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
188 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
192 while (HPPFilenameTokens.HasMoreTokens()){
194 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
198 while (DirFilenameTokens.HasMoreTokens()){
200 DirFilenameOnly = DirFilenameTokens.GetNextToken();
204 // Make the directory filename upper case for writing to the
207 wxString CPPFileOnly;
209 finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
210 finaldirname.MakeUpper();
212 // Write out the header file inclusion guard.
214 HPPFilename.Append(wxT(".h"));
216 #if defined(__WIN32__)
218 hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
222 hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
226 hppfile << "#include <iostream>" << endl << endl;
227 hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
228 hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
229 hppfile << "// List all CPP files in the directory." << endl << endl;
231 // Write each CPP file into the header file.
233 for (int f = 0; f < filelist.GetCount() ; f++){
235 CPPFilename = filelist[f];
237 #if defined(__HAIKU__)
239 #elif defined(__WIN32__)
241 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
245 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
249 while (CPPFilenameTokenizer.HasMoreTokens()){
251 CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
255 CPPFileOnly.RemoveLast(4);
256 CPPFileOnly.Append(wxT(".cpp"));
258 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
259 "/" << CPPFileOnly.c_str() << "\"" << endl;
263 // Write the end if and close the file.
265 hppfile << endl << "#endif" << endl << endl;
268 // Increment the HPP file counter.
270 cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
275 int main(int argc, char *argv[])
282 wxArrayString dirlist;
283 wxArrayString filelist;
284 wxString BitmapHeaderFilename;
285 wxString DirFilenameWxS;
287 // Check if completed file exists before doing anything
288 // else and write an error message if it does exist.
290 if (wxFileExists(wxT("bitmapsdone"))){
292 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
293 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
300 fstream finalhppfile;
303 std::cout << "Error: No directory name given!" << std::endl;
307 // Look in the subdirectories of the bitmaps directory and
308 // collect the names of the directories.
310 std::cout << "Working out directories in bitmaps directory..." <<
313 const char *dirarg = argv[1];
315 wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
317 #if defined(__HAIKU__)
319 #elif defined(__WIN32__)
321 BitmapHeaderFilename.Append(BitmapDirName);
322 BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
326 BitmapHeaderFilename.Append(BitmapDirName);
327 BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
331 if ( wxDirExists(BitmapDirName) ){
333 wxDir BitmapDir(BitmapDirName);
335 wxString BitmapSubDir;
336 wxString BitmapSubDirFull;
338 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
340 while (ContinueProcess){
342 #if defined(__HAIKU__)
344 BitmapSubDirFull.Append(BitmapSubDir);
345 BitmapSubDirFull.Append();
347 #elif defined(__WIN32__)
349 BitmapSubDirFull.Append(BitmapDirName);
350 BitmapSubDirFull.Append(wxT("\\"));
351 BitmapSubDirFull.Append(BitmapSubDir);
355 BitmapSubDirFull.Append(BitmapDirName);
356 BitmapSubDirFull.Append(wxT("/"));
357 BitmapSubDirFull.Append(BitmapSubDir);
361 if (wxDirExists(BitmapSubDirFull)){
362 dirlist.Add(BitmapSubDirFull);
365 ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
367 BitmapSubDirFull.Clear();
372 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
376 // Process each directory, generating a .cpp and .hpp file
377 // for each image and then a final .hpp for the directory
378 // containing the .hpp's for the directories.
380 if (dirlist.GetCount() == 0){
381 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
385 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
387 DirFilenameWxS.Empty();
389 std::cout << BitmapHeaderFilename.c_str() << std::endl;
391 #if defined(__WIN32__)
393 finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
397 finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
401 finalhppfile << "#include <iostream>" << endl << endl;
402 finalhppfile << "#ifndef BITMAPS_H" << endl;
403 finalhppfile << "#define BITMAPS_H" << endl << endl;
405 for (int bi = 0; bi < dirlist.GetCount(); bi++)
408 wxString BitmapSubDirName = dirlist[bi];
409 wxString BitmapFilename;
410 wxDir BitmapSubDir(BitmapSubDirName);
412 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
414 while (ContinueProcess){
416 if (BitmapFilename.Right(4) == wxT(".PNG") ||
418 BitmapFilename.Right(4) == wxT(".png")){
420 #if defined(__HAIKU__)
422 BitmapSubDirFull.Append(BitmapSubDir);
423 BitmapSubDirFull.Append();
425 #elif defined(__WIN32__)
427 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
428 DirFilenameWxS.Append(wxT("\\"));
429 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
433 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
434 DirFilenameWxS.Append(wxString::FromUTF8("/"));
435 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
439 filelist.Add(DirFilenameWxS);
443 DirFilenameWxS.Clear();
444 ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
448 for (int fi = 0; fi < filelist.GetCount(); fi++)
451 CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
456 if (filelist.GetCount() > 0)
459 CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
465 wxString DirNameSplit;
467 #if defined(__HAIKU__)
469 #elif defined(__WIN32__)
471 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
475 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
480 while (DirListFile.HasMoreTokens()){
482 DirNameSplit = DirListFile.GetNextToken();
486 #if defined(__HAIKU__)
488 #elif defined(__WIN32__)
490 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
495 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
502 finalhppfile << endl << "#endif" << endl;
503 finalhppfile.close();
506 // Print out the results.
508 std::cout << "Finished processing PNGs into code." << std::endl;
509 std::cout << fp << " files processed." << std::endl;
510 std::cout << cppg << " .cpp files generated." << std::endl;
511 std::cout << hppg << " .hpp files generated." << std::endl;
513 // Write a success flag so that future runs won't take
514 // place thus speeding up the compilation process as required.
518 bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
519 bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;