Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Ignore build* directories
[xestiaab/.git] / source / tools / bitmapcode.cpp
1 // bitmapcode.cpp - Bitmap code helper.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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/>
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <ios>
23 #include <string>
25 #include <wx/wx.h>
26 #include <wx/dir.h>
27 #include <wx/tokenzr.h>
29 using namespace std;
31 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
32 {
33 //-------------------------------------------------------------------
34 // CreateCPPFile: Create a CPP file from the filename given.
35 //
36 // dirfilename  wxString of the directory name.
37 // filename     wxString of the filename.
38 // counter      Pointer to a integer counter.
39 //-------------------------------------------------------------------
40         
41         char pngfile_char;
42         int pageseek = 0;
43         wxString outname;
44         wxString CPPFilename;
45         CPPFilename = PNGFilename;
46         CPPFilename.RemoveLast(4);
47         CPPFilename.Append(wxT(".cpp"));
48     
49         wxString DirFilenameOnly;
50         wxString CPPFilenameOnly;
52 #if defined(__HAIKU__)
54 #elif defined(__WIN32__)
56         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
57         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
59 #else
61         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
62         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
64 #endif
66         while (CPPFilenameTokens.HasMoreTokens()){
68                 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
70         }
72         while (DirFilenameTokens.HasMoreTokens()){
74                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
76         }
78         // Setup the PNG file reading and cpp file.
79     
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 );     
87 # else
89         pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
90         cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
92 #endif
94         outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
95         outname.Append(wxT("_"));
96         outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
97         outname.MakeUpper();
98         outname.RemoveLast(4);
100         // Setup the inclusion guard.
101     
102         cppfile << "#include <iostream>" << endl << endl;    
103         cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
104         cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
105     
106         outname.Clear();
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"));
112         outname.MakeLower();
113     
114         // Convert the PNG file into an unsigned char array.
115     
116         cppfile << "static unsigned char " << outname.c_str() <<
117                 "[] = {" << endl;
118     
119         while (pngfile){
120                 pngfile.get(pngfile_char);
121                 cppfile << "0x";
123                 if ((unsigned short)pngfile_char > 255){
125                         cppfile << hex << setw(2) << setfill('0') <<
126                                 ((unsigned short)pngfile_char - 65280);
128                 } else {
130                         cppfile << hex << setw(2) << setfill('0') <<
131                                 (unsigned short)pngfile_char;
133                 }
135                 cppfile << ", ";
136                 pageseek++;
138                 if (pageseek == 8){
139                         cppfile << endl;
140                         pageseek = 0;
141                 }
143         }
144     
145         // End the file, close it and increment the counter.
146     
147         cppfile << "};" << endl << endl;
148         cppfile << "#endif" << endl << endl;
149         cppfile.close();
150     
151         cout << "CPP\t" << CPPFilename.c_str() << endl;    
152     
153         ++*counter;
154     
157 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, 
158                         int* counter)
160 //-------------------------------------------------------------------
161 // CreateHPPFileDir:    Create a HPP Directory with the directory
162 //                      filename and list of files in the directory
163 //                      given.
164 //
165 // dirfilename  wxString of the directory name.
166 // filelist     wxArrayString of the directory filelist.
167 // counter      Pointer to a integer counter.
168 //-------------------------------------------------------------------  
170         fstream hppfile;
171     
172         bool fmatch = FALSE;
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("\\"));
185 #else
187         wxStringTokenizer HPPFilenameTokens(HPPFilename, wxT("/"));
188         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
190 #endif
192         while (HPPFilenameTokens.HasMoreTokens()){
194                 HPPFilenameOnly = HPPFilenameTokens.GetNextToken();
196         }
198         while (DirFilenameTokens.HasMoreTokens()){
200                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
202         }
204         // Make the directory filename upper case for writing to the
205         // header file.
206     
207         wxString CPPFileOnly;
209         finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
210         finaldirname.MakeUpper();
211     
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 );
220 #else
222         hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
224 #endif
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;
230     
231         // Write each CPP file into the header file.
232     
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("\\"));
243 #else
245                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
247 #endif
249                 while (CPPFilenameTokenizer.HasMoreTokens()){
251                         CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
253                 }
255                 CPPFileOnly.RemoveLast(4);
256                 CPPFileOnly.Append(wxT(".cpp"));
258                 hppfile << "#include \"" << DirFilenameOnly.c_str() <<
259                         "/" << CPPFileOnly.c_str() << "\"" << endl;
261         }
262     
263         // Write the end if and close the file.
264     
265         hppfile << endl << "#endif" << endl << endl;
266         hppfile.close();
267     
268         // Increment the HPP file counter.
270         cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
271         ++*counter;
272     
275 int main(int argc, char *argv[])
278         int fp = 0;
279         int cppg = 0;
280         int hppg = 0;
281     
282         wxArrayString dirlist;
283         wxArrayString filelist;
284         wxString BitmapHeaderFilename;
285         wxString DirFilenameWxS;
286     
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;
296                 return 0;
298         }
300         fstream finalhppfile;
302         if (!argv[1]){
303                 std::cout << "Error: No directory name given!" << std::endl;
304                 return -1;
305         }
306   
307         // Look in the subdirectories of the bitmaps directory and
308         // collect the names of the directories.
309     
310         std::cout << "Working out directories in bitmaps directory..." << 
311         std::endl;
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"));
324 #else
326         BitmapHeaderFilename.Append(BitmapDirName);
327         BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
329 #endif
331         if ( wxDirExists(BitmapDirName) ){
332     
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);
353 #else
355                         BitmapSubDirFull.Append(BitmapDirName);
356                         BitmapSubDirFull.Append(wxT("/"));
357                         BitmapSubDirFull.Append(BitmapSubDir);
359 #endif
361                         if (wxDirExists(BitmapSubDirFull)){
362                                 dirlist.Add(BitmapSubDirFull);
363                         }
365                         ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
367                         BitmapSubDirFull.Clear();
369                 }
370       
371         } else {
372                 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
373                 return 1;
374         }
375     
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.
379     
380         if (dirlist.GetCount() == 0){
381                 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
382                 return 1;
383         }
384     
385         std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
386     
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);
395 #else
397         finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
399 #endif
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++)
406         {
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") ||
417                                 
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()));
431 #else
433                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
434                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
435                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
437 #endif
439                                 filelist.Add(DirFilenameWxS);
441                         }
443                         DirFilenameWxS.Clear();
444                         ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
446                 }
448                 for (int fi = 0; fi < filelist.GetCount(); fi++)
449                 {
450                 
451                         CreateCPPFile(dirlist[bi].wc_str(), filelist[fi].wc_str(), &cppg);
452                         fp++;
453                         
454                 }
456                 if (filelist.GetCount() > 0)
457                 {
458                 
459                         CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
460                         
461                 }
463                 filelist.Clear();
465                 wxString DirNameSplit;
467 #if defined(__HAIKU__)
469 #elif defined(__WIN32__)
471                 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
473 #else
475                 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
478 #endif
480                 while (DirListFile.HasMoreTokens()){
482                         DirNameSplit = DirListFile.GetNextToken();
484                 }
486 #if defined(__HAIKU__)
488 #elif defined(__WIN32__)
490                 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.c_str() <<
491                         ".h\"" << endl;
493 #else
495                 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
496                         ".h\"" << endl;
498 #endif
500         }
501     
502         finalhppfile << endl << "#endif" << endl;
503         finalhppfile.close();
504         ++hppg;
505     
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;
512     
513         // Write a success flag so that future runs won't take
514         // place thus speeding up the compilation process as required.
516         fstream bitmapflag;
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;
520         bitmapflag.close();
522         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