Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added Win32 support for a future release.
[xestiaab/.git] / source / tools / bitmapcode.cpp
1 //-------------------------------------------------------------------
2 // bitmapcode: Helper application which coverts PNG files into
3 // C++ code outputting binary code.
4 //
5 // This application also is a simple test if the wxWidgets & Boost
6 // header files and library files are there.
7 //
8 // This file is licenced under the GNU General Public License
9 // version 3 only. (GPLv3 only).
10 //-------------------------------------------------------------------
12 #include <iostream>
13 #include <fstream>
14 #include <iomanip>
15 #include <ios>
17 /*#include <boost/filesystem.hpp>
18 #include <boost/filesystem/path.hpp>
19 #include <boost/filesystem/fstream.hpp>*/
20 #include <wx/wx.h>
21 #include <wx/dir.h>
22 #include <wx/tokenzr.h>
23 #include <string.h>
25 using namespace std;
26 //namespace boostfs = boost::filesystem;
28 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
29 {
30 //-------------------------------------------------------------------
31 // CreateCPPFile: Create a CPP file from the filename given.
32 //
33 // dirfilename  wxString of the directory name.
34 // filename     wxString of the filename.
35 // counter      Pointer to a integer counter.
36 //-------------------------------------------------------------------
37     char pngfile_char;
38     int pageseek = 0;
39     /*boostfs::path PNGFilename(filename.c_str());
40     boostfs::path CPPFilename = PNGFilename;
41     boostfs::path CPPFilenameOnly(CPPFilename.filename());
42     boostfs::path DirFilename(dirfilename.c_str());
43     boostfs::path DirFilenameOnly(DirFilename.filename());
44     CPPFilename.replace_extension(".cpp");*/
45     wxString outname;
46         wxString CPPFilename;
47         CPPFilename = PNGFilename;
48         CPPFilename.RemoveLast(4);
49         CPPFilename.Append(wxT(".cpp"));
50     
51         wxString DirFilenameOnly;
52         wxString CPPFilenameOnly;
54 #if defined(__HAIKU__)
56 #elif defined(__WIN32__)
58         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
59         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
61 #else
63         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
64         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
66 #endif
68         while (CPPFilenameTokens.HasMoreTokens()){
70                 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
72         }
74         while (DirFilenameTokens.HasMoreTokens()){
76                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
78         }
80     // Setup the PNG file reading and cpp file.
81     
82     fstream cppfile, pngfile;
84 #if defined(__WIN32__)
86     pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
87     cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc ); 
89 # else
91     pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
92     cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
94 #endif
96     outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
97     outname.Append(wxT("_"));
98     outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
99     outname.MakeUpper();
100         outname.RemoveLast(4);
102     // Setup the inclusion guard.
103     
104     cppfile << "#include <iostream>" << endl << endl;    
105     cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
106     cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
107     
108     outname.Clear();
109     outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
110     outname.Append(wxT("_"));
111     outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
112         outname.RemoveLast(4);
113         outname.Append(wxT("_png"));
114     outname.MakeLower();
115     
116     // Convert the PNG file into an unsigned char array.
117     
118     cppfile << "static unsigned char " << outname.c_str() <<
119         "[] = {" << endl;
120     
121         while (pngfile){
122                 pngfile.get(pngfile_char);
123                 cppfile << "0x";
125                 if ((unsigned short)pngfile_char > 255){
127                         cppfile << hex << setw(2) << setfill('0') <<
128                                 ((unsigned short)pngfile_char - 65280);
130                 } else {
132                         cppfile << hex << setw(2) << setfill('0') <<
133                                 (unsigned short)pngfile_char;
135                 }
137                 cppfile << ", ";
138                 pageseek++;
140                 if (pageseek == 8){
141                         cppfile << endl;
142                         pageseek = 0;
143                 }
145         }
146     
147     // End the file, close it and increment the counter.
148     
149     cppfile << "};" << endl << endl;
150     cppfile << "#endif" << endl << endl;
151     cppfile.close();
152     
153     cout << "CPP\t" << CPPFilename.c_str() << endl;    
154     
155     ++*counter;
156     
159 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, 
160                         int* counter)
162 //-------------------------------------------------------------------
163 // CreateHPPFileDir:    Create a HPP Directory with the directory
164 //                      filename and list of files in the directory
165 //                      given.
166 //
167 // dirfilename  wxString of the directory name.
168 // filelist     wxArrayString of the directory filelist.
169 // counter      Pointer to a integer counter.
170 //-------------------------------------------------------------------  
171    /* boostfs::path HPPFilename(dirfilename.c_str());
172     boostfs::path CPPFile;
173     boostfs::path CPPFileOnly(CPPFile.filename());
174     boostfs::path DirFilename(dirfilename.c_str());
175     boostfs::path DirFilenameOnly(DirFilename.filename());
176     HPPFilename.replace_extension(".h");*/
177     fstream hppfile;
178     
179     bool fmatch = FALSE;
180     wxString finaldirname;
181         wxString HPPFilename = dirfilename.c_str();
183         wxString DirFilenameOnly;
184         wxString HPPFilenameOnly;
185         wxString CPPFilename;
187 #if defined(__WIN32__)
189         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
190         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
192 #else
194         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
195         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
197 #endif
199         while (HPPFilenameTokens.HasMoreTokens()){
201                 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
203         }
205         while (DirFilenameTokens.HasMoreTokens()){
207                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
209         }
211     // Make the directory filename upper case for writing to the
212     // header file.
213     
214         wxString CPPFileOnly;
216     finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
217     finaldirname.MakeUpper();
218     
219     // Write out the header file inclusion guard.
221         HPPFilename.Append(wxT(".h"));
223 #if defined(__WIN32__)
225     hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
227 #else
229     hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
231 #endif
233     hppfile << "#include <iostream>" << endl << endl;
234     hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
235     hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
236     hppfile << "// List all CPP files in the directory." << endl << endl;
237     
238     // Write each CPP file into the header file.
239     
240         for (int f = 0; f < filelist.GetCount() ; f++){
242                 CPPFilename = filelist[f];
244 #if defined(__HAIKU__)
246 #elif defined(__WIN32__)
248                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
250 #else
252                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
254 #endif
256                 while (CPPFilenameTokenizer.HasMoreTokens()){
258                         CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
260                 }
262                 CPPFileOnly.RemoveLast(4);
263                 CPPFileOnly.Append(wxT(".cpp"));
265                 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
266                         "/" << CPPFileOnly.c_str() << "\"" << endl;
268         }
270         /*for (int f = 0; f < filelist.GetCount(); f++){
271                 boostfs::path CPPFile(filelist[f].c_str());
272                 boostfs::path CPPFileOnly(CPPFile.filename());
273                 CPPFileOnly.replace_extension(".cpp");
275                 for (boostfs::path::iterator filet = CPPFile.begin();
276                         filet != CPPFile.end(); ++filet){
277                         if (*filet == DirFilename.filename()){
278                                 fmatch = TRUE;
280                         }
281                         if (fmatch == TRUE){
283                         }
284                 }
286                 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
287                         "/" << CPPFileOnly.c_str() << "\"" << endl;
288         }*/
289     
290     // Write the end if and close the file.
291     
292     hppfile << endl << "#endif" << endl << endl;
293     hppfile.close();
294     
295     // Increment the HPP file counter.
297     cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
298     ++*counter;
299     
302 int main(int argc, char *argv[])
304     int fp = 0;
305     int cppg = 0;
306     int hppg = 0;
307     
308     wxArrayString dirlist;
309     wxArrayString filelist;
310         wxString BitmapHeaderFilename;
311     wxString DirFilenameWxS;
312     
313         // Check if completed file exists before doing anything
314         // else and write an error message if it does exist.
316         if (wxFileExists(wxT("bitmapsdone"))){
318                 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
319                 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
322                 return 0;
324         }
326     fstream finalhppfile;
328         if (!argv[1]){
329                 std::cout << "Error: No directory name given!" << std::endl;
330                 return -1;
331         }
332   
333     // Look in the subdirectories of the bitmaps directory and
334     // collect the names of the directories.
335     
336     std::cout << "Working out directories in bitmaps directory..." << 
337         std::endl;
339         const char *dirarg = argv[1];
341         wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
343 #if defined(__HAIKU__)
345 #elif defined(__WIN32__)
347         BitmapHeaderFilename.Append(BitmapDirName);
348         BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
350 #else
352         BitmapHeaderFilename.Append(BitmapDirName);
353         BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
355 #endif
357     /*boostfs::path BitmapsDir(argv[1]);
358     boostfs::path BitmapsDirSubName;
359     boostfs::path BitmapsFilename;
360     boostfs::directory_iterator dir_end;*/
362     if ( wxDirExists(BitmapDirName) ){
363     
364                 wxDir BitmapDir(BitmapDirName);
366                 wxString BitmapSubDir;
367                 wxString BitmapSubDirFull;
369                 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
371                 while (ContinueProcess){
373 #if defined(__HAIKU__)
375                         BitmapSubDirFull.Append(BitmapSubDir);
376                         BitmapSubDirFull.Append();
378 #elif defined(__WIN32__)
380                         BitmapSubDirFull.Append(BitmapDirName);
381                         BitmapSubDirFull.Append(wxT("\\"));
382                         BitmapSubDirFull.Append(BitmapSubDir);
384 #else
386                         BitmapSubDirFull.Append(BitmapDirName);
387                         BitmapSubDirFull.Append(wxT("/"));
388                         BitmapSubDirFull.Append(BitmapSubDir);
390 #endif
392                         if (wxDirExists(BitmapSubDirFull)){
393                                 dirlist.Add(BitmapSubDirFull);
394                         }
396                         ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
398                         BitmapSubDirFull.Clear();
400                 }
402                 /*for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir);
403                         bitmapsidr_iter != dir_end; ++bitmapsidr_iter){
405                         if (boostfs::is_directory(bitmapsidr_iter->status())){
407                                 BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename();
408                                 DirFilenameWxS.Append(wxString::FromUTF8(argv[1]));
409                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str()));
410                                 dirlist.Add(DirFilenameWxS, 1);
411                                 DirFilenameWxS = wxT("");
413                         }
415                 }*/
416       
417         } else {
418                 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
419                 return 1;
420         }
421     
422     // Process each directory, generating a .cpp and .hpp file 
423     // for each image and then a final .hpp for the directory
424     // containing the .hpp's for the directories.
425     
426         if (dirlist.GetCount() == 0){
427                 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
428                 return 1;
429         }
430     
431     std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
432     
433     DirFilenameWxS.Empty();
435         std::cout << BitmapHeaderFilename.c_str() << std::endl;
437 #if defined(__WIN32__)
439     finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
441 #else
443     finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
445 #endif
447     finalhppfile << "#include <iostream>" << endl << endl;
448     finalhppfile << "#ifndef BITMAPS_H" << endl;
449     finalhppfile << "#define BITMAPS_H" << endl << endl;
451         for (int bi = 0; bi < dirlist.GetCount(); bi++)
452         {
454                 wxString BitmapSubDirName = dirlist[bi];
455                 wxString BitmapFilename;
456                 wxDir BitmapSubDir(BitmapSubDirName);
458                 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
460                 while (ContinueProcess){
462                         if (BitmapFilename.Right(4) == wxT(".PNG") ||
463                                 BitmapFilename.Right(4) == wxT(".png")){
465 #if defined(__HAIKU__)
467                                 BitmapSubDirFull.Append(BitmapSubDir);
468                                 BitmapSubDirFull.Append();
470 #elif defined(__WIN32__)
472                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
473                                 DirFilenameWxS.Append(wxT("\\"));
474                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
476 #else
478                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
479                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
480                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
482 #endif
484                                 filelist.Add(DirFilenameWxS);
486                         }
488                         DirFilenameWxS.Clear();
489                         ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
491                 }
493                 /*boostfs::path BitmapsSubDir(dirlist[bi].c_str());
494                 for (boostfs::directory_iterator bitmapsidr_iter(BitmapsSubDir);
495                         bitmapsidr_iter != dir_end;
496                         ++bitmapsidr_iter){
498                         if (boostfs::path(bitmapsidr_iter->path()).extension() == ".png" ||
499                                 boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG"){
501                                 BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename();
502                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str()));
503                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
504                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str()));
506                                 filelist.Add(DirFilenameWxS, 1);
507                                 DirFilenameWxS = wxT("");
511                         }
513                 }*/
515                 for (int fi = 0; fi < filelist.GetCount(); fi++)
516                 {
517                         CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
518                         fp++;
519                 }
521                 if (filelist.GetCount() > 0)
522                 {
523                         CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
524                 }
526                 filelist.Clear();
528                 wxString DirNameSplit;
530 #if defined(__HAIKU__)
532 #elif defined(__WIN32__)
534                 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
536 #else
538                 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
541 #endif
543                 while (DirListFile.HasMoreTokens()){
545                         DirNameSplit = DirListFile.GetNextToken();
547                 }
549 #if defined(__HAIKU__)
551 #elif defined(__WIN32__)
553                 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
554                         ".h\"" << endl;
556 #else
558                 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
559                         ".h\"" << endl;
561 #endif
563         }
564     
565     finalhppfile << endl << "#endif" << endl;
566     finalhppfile.close();
567     ++hppg;
568     
569     // Print out the results.
571     std::cout << "Finished processing PNGs into code." << std::endl;
572     std::cout << fp << " files processed." << std::endl;
573     std::cout << cppg << " .cpp files generated." << std::endl;
574     std::cout << hppg << " .hpp files generated." << std::endl;
575     
576         // Write a success flag so that future runs won't take
577         // place thus speeding up the compilation process as
578         // required.
580         fstream bitmapflag;
582         bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
583         bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;
584         bitmapflag.close();
586     return 0;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy