1 // bitmapcode.cpp - Bitmap code helper.
3 // (c) 2012-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
7 // Xestia Calendar 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 Calendar 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 Calendar. If not, see <http://www.gnu.org/licenses/>
25 #include <wx/tokenzr.h>
30 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
32 //-------------------------------------------------------------------
33 // CreateCPPFile: Create a CPP file from the filename given.
35 // dirfilename wxString of the directory name.
36 // filename wxString of the filename.
37 // counter Pointer to a integer counter.
38 //-------------------------------------------------------------------
44 CPPFilename = PNGFilename;
45 CPPFilename.RemoveLast(4);
46 CPPFilename.Append(wxT(".cpp"));
48 wxString DirFilenameOnly;
49 wxString CPPFilenameOnly;
51 #if defined(__HAIKU__)
53 #elif defined(__WIN32__)
55 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
56 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
60 wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
61 wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
65 while (CPPFilenameTokens.HasMoreTokens()){
67 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
71 while (DirFilenameTokens.HasMoreTokens()){
73 DirFilenameOnly = DirFilenameTokens.GetNextToken();
77 // Setup the PNG file reading and cpp file.
79 fstream cppfile, pngfile;
81 #if defined(__WIN32__)
83 pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
84 cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );
88 pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
89 cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
93 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
94 outname.Append(wxT("_"));
95 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
97 outname.RemoveLast(4);
99 // Setup the inclusion guard.
101 cppfile << "#include <iostream>" << endl << endl;
102 cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
103 cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
106 outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
107 outname.Append(wxT("_"));
108 outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
109 outname.RemoveLast(4);
110 outname.Append(wxT("_png"));
113 // Convert the PNG file into an unsigned char array.
115 cppfile << "static unsigned char " << outname.c_str() <<
119 pngfile.get(pngfile_char);
122 if ((unsigned short)pngfile_char > 255){
124 cppfile << hex << setw(2) << setfill('0') <<
125 ((unsigned short)pngfile_char - 65280);
129 cppfile << hex << setw(2) << setfill('0') <<
130 (unsigned short)pngfile_char;
144 // End the file, close it and increment the counter.
146 cppfile << "};" << endl << endl;
147 cppfile << "#endif" << endl << endl;
150 cout << "CPP\t" << CPPFilename.c_str() << 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.c_str();
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.c_str() << "_H" << endl;
227 hppfile << "#define " << finaldirname.c_str() << "_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.c_str() <<
258 "/" << CPPFileOnly.c_str() << "\"" << 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.c_str() << 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;
299 fstream finalhppfile;
302 std::cout << "Error: No directory name given!" << std::endl;
306 // Look in the subdirectories of the bitmaps directory and
307 // collect the names of the directories.
309 std::cout << "Working out directories in bitmaps directory..." <<
312 const char *dirarg = argv[1];
314 wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
316 #if defined(__HAIKU__)
318 #elif defined(__WIN32__)
320 BitmapHeaderFilename.Append(BitmapDirName);
321 BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
325 BitmapHeaderFilename.Append(BitmapDirName);
326 BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
330 if ( wxDirExists(BitmapDirName) ){
332 wxDir BitmapDir(BitmapDirName);
334 wxString BitmapSubDir;
335 wxString BitmapSubDirFull;
337 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
339 while (ContinueProcess){
341 #if defined(__HAIKU__)
343 BitmapSubDirFull.Append(BitmapSubDir);
344 BitmapSubDirFull.Append();
346 #elif defined(__WIN32__)
348 BitmapSubDirFull.Append(BitmapDirName);
349 BitmapSubDirFull.Append(wxT("\\"));
350 BitmapSubDirFull.Append(BitmapSubDir);
354 BitmapSubDirFull.Append(BitmapDirName);
355 BitmapSubDirFull.Append(wxT("/"));
356 BitmapSubDirFull.Append(BitmapSubDir);
360 if (wxDirExists(BitmapSubDirFull)){
361 dirlist.Add(BitmapSubDirFull);
364 ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
366 BitmapSubDirFull.Clear();
371 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
375 // Process each directory, generating a .cpp and .hpp file
376 // for each image and then a final .hpp for the directory
377 // containing the .hpp's for the directories.
379 if (dirlist.GetCount() == 0){
380 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
384 std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
386 DirFilenameWxS.Empty();
388 std::cout << BitmapHeaderFilename.c_str() << std::endl;
390 #if defined(__WIN32__)
392 finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
396 finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
400 finalhppfile << "#include <iostream>" << endl << endl;
401 finalhppfile << "#ifndef BITMAPS_H" << endl;
402 finalhppfile << "#define BITMAPS_H" << endl << endl;
404 for (int bi = 0; bi < dirlist.GetCount(); bi++)
407 wxString BitmapSubDirName = dirlist[bi];
408 wxString BitmapFilename;
409 wxDir BitmapSubDir(BitmapSubDirName);
411 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
413 while (ContinueProcess){
415 if (BitmapFilename.Right(4) == wxT(".PNG") ||
417 BitmapFilename.Right(4) == wxT(".png")){
419 #if defined(__HAIKU__)
421 BitmapSubDirFull.Append(BitmapSubDir);
422 BitmapSubDirFull.Append();
424 #elif defined(__WIN32__)
426 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
427 DirFilenameWxS.Append(wxT("\\"));
428 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
432 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
433 DirFilenameWxS.Append(wxString::FromUTF8("/"));
434 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
438 filelist.Add(DirFilenameWxS);
442 DirFilenameWxS.Clear();
443 ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
447 for (int fi = 0; fi < filelist.GetCount(); fi++)
450 CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
455 if (filelist.GetCount() > 0)
458 CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
464 wxString DirNameSplit;
466 #if defined(__HAIKU__)
468 #elif defined(__WIN32__)
470 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
474 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
479 while (DirListFile.HasMoreTokens()){
481 DirNameSplit = DirListFile.GetNextToken();
485 #if defined(__HAIKU__)
487 #elif defined(__WIN32__)
489 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
494 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
501 finalhppfile << endl << "#endif" << endl;
502 finalhppfile.close();
505 // Print out the results.
507 std::cout << "Finished processing PNGs into code." << std::endl;
508 std::cout << fp << " files processed." << std::endl;
509 std::cout << cppg << " .cpp files generated." << std::endl;
510 std::cout << hppg << " .hpp files generated." << std::endl;
512 // Write a success flag so that future runs won't take
513 // place thus speeding up the compilation process as required.
517 bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
518 bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;