Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Initial import of code already done for Xestia Address Book
[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;
83     pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
84     cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );
86     outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
87     outname.Append(wxT("_"));
88     outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
89     outname.MakeUpper();
90         outname.RemoveLast(4);
92     // Setup the inclusion guard.
93     
94     cppfile << "#include <iostream>" << endl << endl;    
95     cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
96     cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
97     
98     outname.Clear();
99     outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
100     outname.Append(wxT("_"));
101     outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
102         outname.RemoveLast(4);
103         outname.Append(wxT("_png"));
104     outname.MakeLower();
105     
106     // Convert the PNG file into an unsigned char array.
107     
108     cppfile << "static unsigned char " << outname.c_str() <<
109         "[] = {" << endl;
110     
111         while (pngfile){
112                 pngfile.get(pngfile_char);
113                 cppfile << "0x";
115                 if ((unsigned short)pngfile_char > 255){
117                         cppfile << hex << setw(2) << setfill('0') <<
118                                 ((unsigned short)pngfile_char - 65280);
120                 } else {
122                         cppfile << hex << setw(2) << setfill('0') <<
123                                 (unsigned short)pngfile_char;
125                 }
127                 cppfile << ", ";
128                 pageseek++;
130                 if (pageseek == 8){
131                         cppfile << endl;
132                         pageseek = 0;
133                 }
135         }
136     
137     // End the file, close it and increment the counter.
138     
139     cppfile << "};" << endl << endl;
140     cppfile << "#endif" << endl << endl;
141     cppfile.close();
142     
143     cout << "CPP\t" << CPPFilename.c_str() << endl;    
144     
145     ++*counter;
146     
149 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, 
150                         int* counter)
152 //-------------------------------------------------------------------
153 // CreateHPPFileDir:    Create a HPP Directory with the directory
154 //                      filename and list of files in the directory
155 //                      given.
156 //
157 // dirfilename  wxString of the directory name.
158 // filelist     wxArrayString of the directory filelist.
159 // counter      Pointer to a integer counter.
160 //-------------------------------------------------------------------  
161    /* boostfs::path HPPFilename(dirfilename.c_str());
162     boostfs::path CPPFile;
163     boostfs::path CPPFileOnly(CPPFile.filename());
164     boostfs::path DirFilename(dirfilename.c_str());
165     boostfs::path DirFilenameOnly(DirFilename.filename());
166     HPPFilename.replace_extension(".h");*/
167     fstream hppfile;
168     
169     bool fmatch = FALSE;
170     wxString finaldirname;
171         wxString HPPFilename = dirfilename.c_str();
173         wxString DirFilenameOnly;
174         wxString HPPFilenameOnly;
175         wxString CPPFilename;
177 #if defined(__HAIKU__)
179 #elif defined(__WIN32__)
181         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("\\"));
182         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
184 #else
186         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
187         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
189 #endif
191         while (HPPFilenameTokens.HasMoreTokens()){
193                 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
195         }
197         while (DirFilenameTokens.HasMoreTokens()){
199                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
201         }
203     // Make the directory filename upper case for writing to the
204     // header file.
205     
206         wxString CPPFileOnly;
208     finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
209     finaldirname.MakeUpper();
210     
211     // Write out the header file inclusion guard.
213         HPPFilename.Append(wxT(".h"));
215     hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
216     
217     hppfile << "#include <iostream>" << endl << endl;
218     hppfile << "#ifndef " << finaldirname.c_str() << "_H" << endl;
219     hppfile << "#define " << finaldirname.c_str() << "_H" << endl << endl;
220     hppfile << "// List all CPP files in the directory." << endl << endl;
221     
222     // Write each CPP file into the header file.
223     
224         for (int f = 0; f < filelist.GetCount() ; f++){
226                 CPPFilename = filelist[f];
228 #if defined(__HAIKU__)
230 #elif defined(__WIN32__)
232                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("\\"));
234 #else
236                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
238 #endif
240                 while (CPPFilenameTokenizer.HasMoreTokens()){
242                         CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
244                 }
246                 CPPFileOnly.RemoveLast(4);
247                 CPPFileOnly.Append(wxT(".cpp"));
249                 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
250                         "/" << CPPFileOnly.c_str() << "\"" << endl;
252         }
254         /*for (int f = 0; f < filelist.GetCount(); f++){
255                 boostfs::path CPPFile(filelist[f].c_str());
256                 boostfs::path CPPFileOnly(CPPFile.filename());
257                 CPPFileOnly.replace_extension(".cpp");
259                 for (boostfs::path::iterator filet = CPPFile.begin();
260                         filet != CPPFile.end(); ++filet){
261                         if (*filet == DirFilename.filename()){
262                                 fmatch = TRUE;
264                         }
265                         if (fmatch == TRUE){
267                         }
268                 }
270                 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
271                         "/" << CPPFileOnly.c_str() << "\"" << endl;
272         }*/
273     
274     // Write the end if and close the file.
275     
276     hppfile << endl << "#endif" << endl << endl;
277     hppfile.close();
278     
279     // Increment the HPP file counter.
281     cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
282     ++*counter;
283     
286 int main(int argc, char *argv[])
288     int fp = 0;
289     int cppg = 0;
290     int hppg = 0;
291     
292     wxArrayString dirlist;
293     wxArrayString filelist;
294         wxString BitmapHeaderFilename;
295     wxString DirFilenameWxS;
296     
297         // Check if completed file exists before doing anything
298         // else and write an error message if it does exist.
300         if (wxFileExists(wxT("bitmapsdone"))){
302                 std::cout << "Bitmap files have already been generated!" << std::endl << std::endl;
303                 std::cout << "To regenerate the files simply delete the bitmapsdone file where this helper application is run." << std::endl;
306                 return 0;
308         }
310     fstream finalhppfile;
312         if (!argv[1]){
313                 std::cout << "Error: No directory name given!" << std::endl;
314                 return -1;
315         }
316   
317     // Look in the subdirectories of the bitmaps directory and
318     // collect the names of the directories.
319     
320     std::cout << "Working out directories in bitmaps directory..." << 
321         std::endl;
323         const char *dirarg = argv[1];
325         wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
327 #if defined(__HAIKU__)
329 #elif defined(__WIN32__)
331         BitmapHeaderFilename.Append(BitmapDirName);
332         BitmapHeaderFilename.Append(wxT("\\..\\bitmaps.h"));
334 #else
336         BitmapFilename.Append(BitmapDirName);
337         BitmapFilename.Append(wxT("/../bitmaps.h"));
339 #endif
341     /*boostfs::path BitmapsDir(argv[1]);
342     boostfs::path BitmapsDirSubName;
343     boostfs::path BitmapsFilename;
344     boostfs::directory_iterator dir_end;*/
346     if ( wxDirExists(BitmapDirName) ){
347     
348                 wxDir BitmapDir(BitmapDirName);
350                 wxString BitmapSubDir;
351                 wxString BitmapSubDirFull;
353                 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
354                 
355                 while (ContinueProcess){
357 #if defined(__HAIKU__)
359                         BitmapSubDirFull.Append(BitmapSubDir);
360                         BitmapSubDirFull.Append();
362 #elif defined(__WIN32__)
364                         BitmapSubDirFull.Append(BitmapDirName);
365                         BitmapSubDirFull.Append(wxT("\\"));
366                         BitmapSubDirFull.Append(BitmapSubDir);
368 #else
370                         BitmapSubDirFull.Append(BitmapDirName);
371                         BitmapSubDirFull.Append(wxT("/"));
372                         BitmapSubDirFull.Append(BitmapSubDirFull);
374 #endif
376                         if (wxDirExists(BitmapSubDirFull)){
377                                 dirlist.Add(BitmapSubDirFull);
378                         }
380                         ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
382                         BitmapSubDirFull.Clear();
384                 }
386                 /*for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir);
387                         bitmapsidr_iter != dir_end; ++bitmapsidr_iter){
389                         if (boostfs::is_directory(bitmapsidr_iter->status())){
391                                 BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename();
392                                 DirFilenameWxS.Append(wxString::FromUTF8(argv[1]));
393                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str()));
394                                 dirlist.Add(DirFilenameWxS, 1);
395                                 DirFilenameWxS = wxT("");
397                         }
399                 }*/
400       
401         } else {
402                 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
403                 return 1;
404         }
405     
406     // Process each directory, generating a .cpp and .hpp file 
407     // for each image and then a final .hpp for the directory
408     // containing the .hpp's for the directories.
409     
410         if (dirlist.GetCount() == 0){
411                 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
412                 return 1;
413         }
414     
415     std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
416     
417     DirFilenameWxS.Empty();
419         std::cout << BitmapHeaderFilename.c_str() << std::endl;
421     finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
422     finalhppfile << "#include <iostream>" << endl << endl;
423     finalhppfile << "#ifndef BITMAPS_H" << endl;
424     finalhppfile << "#define BITMAPS_H" << endl << endl;
425     
426         for (int bi = 0; bi < dirlist.GetCount(); bi++)
427         {
429                 wxString BitmapSubDirName = wxString::FromUTF8(dirlist[bi]);
430                 wxString BitmapFilename;
431                 wxDir BitmapSubDir(BitmapSubDirName);
433                 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
435                 while (ContinueProcess){
437                         if (BitmapFilename.Right(4) == wxT(".PNG") ||
438                                 BitmapFilename.Right(4) == wxT(".png")){
440 #if defined(__HAIKU__)
442                                 BitmapSubDirFull.Append(BitmapSubDir);
443                                 BitmapSubDirFull.Append();
445 #elif defined(__WIN32__)
447                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
448                                 DirFilenameWxS.Append(wxT("\\"));
449                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
451 #else
453                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
454                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
455                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
457 #endif
459                                 filelist.Add(DirFilenameWxS);
461                         }
463                         DirFilenameWxS.Clear();
464                         ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
466                 }
468                 /*boostfs::path BitmapsSubDir(dirlist[bi].c_str());
469                 for (boostfs::directory_iterator bitmapsidr_iter(BitmapsSubDir);
470                         bitmapsidr_iter != dir_end;
471                         ++bitmapsidr_iter){
473                         if (boostfs::path(bitmapsidr_iter->path()).extension() == ".png" ||
474                                 boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG"){
476                                 BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename();
477                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str()));
478                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
479                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str()));
481                                 filelist.Add(DirFilenameWxS, 1);
482                                 DirFilenameWxS = wxT("");
486                         }
488                 }*/
490                 for (int fi = 0; fi < filelist.GetCount(); fi++)
491                 {
492                         CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
493                         fp++;
494                 }
496                 if (filelist.GetCount() > 0)
497                 {
498                         CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
499                 }
501                 filelist.Clear();
503                 wxString DirNameSplit;
505 #if defined(__HAIKU__)
507 #elif defined(__WIN32__)
509                 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
511 #else
513                 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
516 #endif
518                 while (DirListFile.HasMoreTokens()){
520                         DirNameSplit = DirListFile.GetNextToken();
522                 }
524 #if defined(__HAIKU__)
526 #elif defined(__WIN32__)
528                 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
529                         ".h\"" << endl;
531 #else
533                 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
534                         ".h\"" << endl;
536 #endif
538         }
539     
540     finalhppfile << endl << "#endif" << endl;
541     finalhppfile.close();
542     ++hppg;
543     
544     // Print out the results.
546     std::cout << "Finished processing PNGs into code." << std::endl;
547     std::cout << fp << " files processed." << std::endl;
548     std::cout << cppg << " .cpp files generated." << std::endl;
549     std::cout << hppg << " .hpp files generated." << std::endl;
550     
551         // Write a success flag so that future runs won't take
552         // place thus speeding up the compilation process as
553         // required.
555         fstream bitmapflag;
557         bitmapflag.open("bitmapsdone", ios::out | ios::trunc);
558         bitmapflag << "Bitmaps as code generated. To recreate, simply delete this file and run the bitmap code generation tool again." << endl;
559         bitmapflag.close();
561     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