Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Setup CMake to build for macOS
[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.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;
295                 return 0;
297         }
299         fstream finalhppfile;
301         if (!argv[1]){
302                 std::cout << "Error: No directory name given!" << std::endl;
303                 return -1;
304         }
305   
306         // Look in the subdirectories of the bitmaps directory and
307         // collect the names of the directories.
308     
309         std::cout << "Working out directories in bitmaps directory..." << 
310         std::endl;
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"));
323 #else
325         BitmapHeaderFilename.Append(BitmapDirName);
326         BitmapHeaderFilename.Append(wxT("/../bitmaps.h"));
328 #endif
330         if ( wxDirExists(BitmapDirName) ){
331     
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);
352 #else
354                         BitmapSubDirFull.Append(BitmapDirName);
355                         BitmapSubDirFull.Append(wxT("/"));
356                         BitmapSubDirFull.Append(BitmapSubDir);
358 #endif
360                         if (wxDirExists(BitmapSubDirFull)){
361                                 dirlist.Add(BitmapSubDirFull);
362                         }
364                         ContinueProcess = BitmapDir.GetNext(&BitmapSubDir);
366                         BitmapSubDirFull.Clear();
368                 }
369       
370         } else {
371                 std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
372                 return 1;
373         }
374     
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.
378     
379         if (dirlist.GetCount() == 0){
380                 cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
381                 return 1;
382         }
383     
384         std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
385     
386         DirFilenameWxS.Empty();
388         std::cout << BitmapHeaderFilename.ToStdString() << std::endl;
390 #if defined(__WIN32__)
392         finalhppfile.open(BitmapHeaderFilename.wc_str(), ios::out | ios::trunc);
394 #else
396         finalhppfile.open(BitmapHeaderFilename.c_str(), ios::out | ios::trunc);
398 #endif
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++)
405         {
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") ||
416                                 
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()));
430 #else
432                                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].c_str()));
433                                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
434                                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapFilename.c_str()));
436 #endif
438                                 filelist.Add(DirFilenameWxS);
440                         }
442                         DirFilenameWxS.Clear();
443                         ContinueProcess = BitmapSubDir.GetNext(&BitmapFilename);
445                 }
447                 for (int fi = 0; fi < filelist.GetCount(); fi++)
448                 {
449                 
450                         CreateCPPFile(dirlist[bi].ToStdString(), filelist[fi].ToStdString(), &cppg);
451                         fp++;
452                         
453                 }
455                 if (filelist.GetCount() > 0)
456                 {
457                 
458                         CreateHPPFileDir(dirlist[bi].wc_str(), filelist, &hppg);
459                         
460                 }
462                 filelist.Clear();
464                 wxString DirNameSplit;
466 #if defined(__HAIKU__)
468 #elif defined(__WIN32__)
470                 wxStringTokenizer DirListFile(dirlist[bi], wxT("\\"));
472 #else
474                 wxStringTokenizer DirListFile(dirlist[bi], wxT("/"));
477 #endif
479                 while (DirListFile.HasMoreTokens()){
481                         DirNameSplit = DirListFile.GetNextToken();
483                 }
485 #if defined(__HAIKU__)
487 #elif defined(__WIN32__)
489                 finalhppfile << "#include \"bitmaps\\" << DirNameSplit.ToStdString() <<
490                         ".h\"" << endl;
492 #else
494                 finalhppfile << "#include \"bitmaps/" << DirNameSplit.ToStdString() <<
495                         ".h\"" << endl;
497 #endif
499         }
500     
501         finalhppfile << endl << "#endif" << endl;
502         finalhppfile.close();
503         ++hppg;
504     
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;
511     
512         // Write a success flag so that future runs won't take
513         // place thus speeding up the compilation process as required.
515         fstream bitmapflag;
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;
519         bitmapflag.close();
521         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