Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
utf8: Implemented further UTF8 support
[xestiacalendar/.git] / source / tools / bitmapcode.cpp
1 // bitmapcode.cpp - Bitmap code helper.
2 //
3 // (c) 2012-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar 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 Calendar 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 Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include <iostream>
20 #include <fstream>
21 #include <iomanip>
22 #include <ios>
23 #include <wx/wx.h>
24 #include <wx/dir.h>
25 #include <wx/tokenzr.h>
26 #include <string.h>
28 using namespace std;
30 void CreateCPPFile(wxString dirfilename, wxString PNGFilename, int* counter)
31 {
32 //-------------------------------------------------------------------
33 // CreateCPPFile: Create a CPP file from the filename given.
34 //
35 // dirfilename  wxString of the directory name.
36 // filename     wxString of the filename.
37 // counter      Pointer to a integer counter.
38 //-------------------------------------------------------------------
39         
40         char pngfile_char;
41         int pageseek = 0;
42         wxString outname;
43         wxString CPPFilename;
44         CPPFilename = PNGFilename;
45         CPPFilename.RemoveLast(4);
46         CPPFilename.Append(wxT(".cpp"));
47     
48         wxString DirFilenameOnly;
49         wxString CPPFilenameOnly;
51 #if defined(__HAIKU__)
53 #elif defined(__WIN32__)
55         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("\\"));
56         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("\\"));
58 #else
60         wxStringTokenizer CPPFilenameTokens(CPPFilename, wxT("/"));
61         wxStringTokenizer DirFilenameTokens(dirfilename, wxT("/"));
63 #endif
65         while (CPPFilenameTokens.HasMoreTokens()){
67                 CPPFilenameOnly = CPPFilenameTokens.GetNextToken();
69         }
71         while (DirFilenameTokens.HasMoreTokens()){
73                 DirFilenameOnly = DirFilenameTokens.GetNextToken();
75         }
77         // Setup the PNG file reading and cpp file.
78     
79         fstream cppfile, pngfile;
81 #if defined(__WIN32__)
83         pngfile.open(PNGFilename.wc_str(), ios::in | ios::binary );
84         cppfile.open(CPPFilename.wc_str(), ios::out | ios::trunc );     
86 # else
88         pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
89         cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
91 #endif
93         outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
94         outname.Append(wxT("_"));
95         outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
96         outname.MakeUpper();
97         outname.RemoveLast(4);
99         // Setup the inclusion guard.
100     
101         cppfile << "#include <iostream>" << endl << endl;    
102         cppfile << "#ifndef " << outname.c_str() << "_CPP" << endl;
103         cppfile << "#define " << outname.c_str() << "_CPP" << endl << endl;
104     
105         outname.Clear();
106         outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
107         outname.Append(wxT("_"));
108         outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
109         outname.RemoveLast(4);
110         outname.Append(wxT("_png"));
111         outname.MakeLower();
112     
113         // Convert the PNG file into an unsigned char array.
114     
115         cppfile << "static unsigned char " << outname.c_str() <<
116                 "[] = {" << endl;
117     
118         while (pngfile){
119                 pngfile.get(pngfile_char);
120                 cppfile << "0x";
122                 if ((unsigned short)pngfile_char > 255){
124                         cppfile << hex << setw(2) << setfill('0') <<
125                                 ((unsigned short)pngfile_char - 65280);
127                 } else {
129                         cppfile << hex << setw(2) << setfill('0') <<
130                                 (unsigned short)pngfile_char;
132                 }
134                 cppfile << ", ";
135                 pageseek++;
137                 if (pageseek == 8){
138                         cppfile << endl;
139                         pageseek = 0;
140                 }
142         }
143     
144         // End the file, close it and increment the counter.
145     
146         cppfile << "};" << endl << endl;
147         cppfile << "#endif" << endl << endl;
148         cppfile.close();
149     
150         cout << "CPP\t" << CPPFilename.c_str() << endl;    
151     
152         ++*counter;
153     
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.c_str();
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.c_str() << "_H" << endl;
227         hppfile << "#define " << finaldirname.c_str() << "_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.c_str() <<
258                         "/" << CPPFileOnly.c_str() << "\"" << 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.c_str() << 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.c_str() << 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].wc_str(), filelist[fi].wc_str(), &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.c_str() <<
490                         ".h\"" << endl;
492 #else
494                 finalhppfile << "#include \"bitmaps/" << DirNameSplit.c_str() <<
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