Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Capitalise Bitmaps and Tools directories
[xestiaab/.git] / source / Tools / bitmapcode.cpp
1 // bitmapcode.cpp - Bitmap code helper.
2 //
3 // (c) 2012-2021 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.ToStdString() << "_CPP" << endl;
104         cppfile << "#define " << outname.ToStdString() << "_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.ToStdString() <<
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.ToStdString() << endl;
152     
153         ++*counter;
156 void CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, 
157                         int* counter)
159 //-------------------------------------------------------------------
160 // CreateHPPFileDir:    Create a HPP Directory with the directory
161 //                      filename and list of files in the directory
162 //                      given.
163 //
164 // dirfilename  wxString of the directory name.
165 // filelist     wxArrayString of the directory filelist.
166 // counter      Pointer to a integer counter.
167 //-------------------------------------------------------------------  
169         fstream hppfile;
170     
171         bool fmatch = FALSE;
172         wxString finaldirname;
173         wxString HPPFilename = dirfilename.ToStdString();
175         wxString DirFilenameOnly;
176         wxString HPPFilenameOnly;
177         wxString CPPFilename;
179 #if 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 #if defined(__WIN32__)
217             hppfile.open(HPPFilename.wc_str(), ios::out | ios::trunc );
219 #else
221         hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
223 #endif
225         hppfile << "#include <iostream>" << endl << endl;
226         hppfile << "#ifndef " << finaldirname.ToStdString() << "_H" << endl;
227         hppfile << "#define " << finaldirname.ToStdString() << "_H" << endl << endl;
228         hppfile << "// List all CPP files in the directory." << endl << endl;
229     
230         // Write each CPP file into the header file.
231     
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("\\"));
242 #else
244                 wxStringTokenizer CPPFilenameTokenizer(CPPFilename, wxT("/"));
246 #endif
248                 while (CPPFilenameTokenizer.HasMoreTokens()){
250                         CPPFileOnly = CPPFilenameTokenizer.GetNextToken();
252                 }
254                 CPPFileOnly.RemoveLast(4);
255                 CPPFileOnly.Append(wxT(".cpp"));
257                 hppfile << "#include \"" << DirFilenameOnly.ToStdString() <<
258                         "/" << CPPFileOnly.ToStdString() << "\"" << endl;
260         }
261     
262         // Write the end if and close the file.
263     
264         hppfile << endl << "#endif" << endl << endl;
265         hppfile.close();
266     
267         // Increment the HPP file counter.
269         cout << "HPPDIR\t" << HPPFilename.ToStdString() << endl;
270         ++*counter;
271     
274 int main(int argc, char *argv[])
277         int fp = 0;
278         int cppg = 0;
279         int hppg = 0;
280     
281         wxArrayString dirlist;
282         wxArrayString filelist;
283         wxString BitmapHeaderFilename;
284         wxString DirFilenameWxS;
285     
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;
294                 return 0;
296         }
298         fstream finalhppfile;
300         if (!argv[1]){
301                 std::cout << "Error: No directory name given!" << std::endl;
302                 return -1;
303         }
304   
305         // Look in the subdirectories of the bitmaps directory and
306         // collect the names of the directories.
307     
308         std::cout << "Working out directories in bitmaps directory..." << 
309         std::endl;
311         const char *dirarg = argv[1];
313         wxString BitmapDirName = wxString(dirarg, wxConvUTF8);
315 #if defined(__HAIKU__)
317 #elif defined(__WIN32__)
319         BitmapHeaderFilename.Append(BitmapDirName);
320         BitmapHeaderFilename.Append(wxT("\\..\\Bitmaps.h"));
322 #else
324         BitmapHeaderFilename.Append(BitmapDirName);
325         BitmapHeaderFilename.Append(wxT("/../Bitmaps.h"));
327 #endif
329         if ( wxDirExists(BitmapDirName) ){
330     
331                 wxDir BitmapDir(BitmapDirName);
333                 wxString BitmapSubDir;
334                 wxString BitmapSubDirFull;
336                 bool ContinueProcess = BitmapDir.GetFirst(&BitmapSubDir, wxEmptyString, wxDIR_DEFAULT);
338                 while (ContinueProcess){
340 #if defined(__HAIKU__)
342                         BitmapSubDirFull.Append(BitmapSubDir);
343                         BitmapSubDirFull.Append();
345 #elif defined(__WIN32__)
347                         BitmapSubDirFull.Append(BitmapDirName);
348                         BitmapSubDirFull.Append(wxT("\\"));
349                         BitmapSubDirFull.Append(BitmapSubDir);
351 #else
353                         BitmapSubDirFull.Append(BitmapDirName);
354                         BitmapSubDirFull.Append(wxT("/"));
355                         BitmapSubDirFull.Append(BitmapSubDir);
357 #endif
359                         if (wxDirExists(BitmapSubDirFull)){
360                                 dirlist.Add(BitmapSubDirFull);
361                         }
363                         ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
365                         BitmapSubDirFull.Clear();
367                 }
368       
369         } else {
370                 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
371                 return 1;
372         }
373     
374         // Process each directory, generating a .cpp and .hpp file 
375         // for each image and then a final .hpp for the directory
376         // containing the .hpp's for the directories.
377     
378         if (dirlist.GetCount() == 0){
379                 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
380                 return 1;
381         }
382     
383         std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
384     
385         DirFilenameWxS.Empty();
387         std::cout << BitmapHeaderFilename.ToStdString() << std::endl;
389 #if defined(__WIN32__)
391         finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
393 #else
395         finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
397 #endif
399         finalhppfile << "#include <iostream>" << endl << endl;
400         finalhppfile << "#ifndef BITMAPS_H" << endl;
401         finalhppfile << "#define BITMAPS_H" << endl << endl;
403         for (int bi = 0; bi < dirlist.GetCount(); bi++)
404         {
406                 wxString BitmapSubDirName = dirlist[bi];
407                 wxString BitmapFilename;
408                 wxDir BitmapSubDir(BitmapSubDirName);
410                 bool ContinueProcess = BitmapSubDir.GetFirst(&BitmapFilename, wxEmptyString, wxDIR_DEFAULT);
412                 while (ContinueProcess){
414                         if (BitmapFilename.Right(4) == wxT(".PNG") ||
415                                 
416                                 BitmapFilename.Right(4) == wxT(".png")){
418 #if defined(__HAIKU__)
420                                 BitmapSubDirFull.Append(BitmapSubDir);
421                                 BitmapSubDirFull.Append();
423 #elif defined(__WIN32__)
425                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
426                                 DirFilenameWxS.Append(wxT("\\"));
427                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
429 #else
431                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
432                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
433                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
435 #endif
437                                 filelist.Add(DirFilenameWxS);
439                         }
441                         DirFilenameWxS.Clear();
442                         ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
444                 }
446                 for (int fi = 0; fi < filelist.GetCount(); fi++)
447                 {
448                 
449                         CreateCPPFile(dirlist[bi].ToStdString(), filelist[fi].ToStdString(), &cppg);
450                         fp++;
451                         
452                 }
454                 if (filelist.GetCount() > 0)
455                 {
456                 
457                         CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
458                         
459                 }
461                 filelist.Clear();
463                 wxString DirNameSplit;
465 #if defined(__HAIKU__)
467 #elif defined(__WIN32__)
469                 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
471 #else
473                 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
476 #endif
478                 while (DirListFile.HasMoreTokens()){
480                         DirNameSplit = DirListFile.GetNextToken();
482                 }
484 #if defined(__HAIKU__)
486 #elif defined(__WIN32__)
488                 finalhppfile << "#include \"Bitmaps\\" << DirNameSplit.ToStdString() <<
489                         ".h\"" << endl;
491 #else
493                 finalhppfile << "#include \"Bitmaps/" << DirNameSplit.ToStdString() <<
494                         ".h\"" << endl;
496 #endif
498         }
499     
500         finalhppfile << endl << "#endif" << endl;
501         finalhppfile.close();
502         ++hppg;
503     
504         // Print out the results.
506         std::cout << "Finished processing PNGs into code." << std::endl;
507         std::cout << fp << " files processed." << std::endl;
508         std::cout << cppg << " .cpp files generated." << std::endl;
509         std::cout << hppg << " .hpp files generated." << std::endl;
510     
511         // Write a success flag so that future runs won't take
512         // place thus speeding up the compilation process as required.
514         fstream bitmapflag;
516         bitmapflag.open("BitmapsDone", ios::out | ios::trunc);
517         bitmapflag << "Bitmaps as code generated. To recreate, simply delete the BitmapsDone file and run the bitmap code generation tool again." << endl;
518         bitmapflag.close();
520         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