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 <iomanip>
14 #include <ios>
16 #include <boost/filesystem.hpp>
17 #include <boost/filesystem/path.hpp>
18 #include <boost/filesystem/fstream.hpp>
19 #include <wx/wx.h>
20 #include <string.h>
22 using namespace std;
23 namespace boostfs = boost::filesystem;
25 void CreateCPPFile(wxString dirfilename, wxString filename, int* counter)
26 {
27 //-------------------------------------------------------------------
28 // CreateCPPFile: Create a CPP file from the filename given.
29 //
30 // dirfilename  wxString of the directory name.
31 // filename     wxString of the filename.
32 // counter      Pointer to a integer counter.
33 //-------------------------------------------------------------------
34     char pngfile_char;
35     int pageseek = 0;
36     boostfs::path PNGFilename(filename.c_str());
37     boostfs::path CPPFilename = PNGFilename;
38     boostfs::path CPPFilenameOnly(CPPFilename.filename());
39     boostfs::path DirFilename(dirfilename.c_str());
40     boostfs::path DirFilenameOnly(DirFilename.filename());
41     CPPFilename.replace_extension(".cpp");
42     wxString outname;
43     
44     // Setup the PNG file reading and cpp file.
45     
46     fstream cppfile, pngfile;
47     pngfile.open(PNGFilename.c_str(), ios::in | ios::binary );
48     cppfile.open(CPPFilename.c_str(), ios::out | ios::trunc );
49     
50     CPPFilenameOnly.replace_extension();
51     
52     outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
53     outname.Append(wxT("_"));
54     outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
55     outname.MakeUpper();
56     
57     // Setup the inclusion guard.
58     
59     cppfile << "#include <iostream>" << endl << endl;    
60     cppfile << "#ifndef " << outname.mb_str() << "_CPP" << endl;
61     cppfile << "#define " << outname.mb_str() << "_CPP" << endl << endl;
62     
63     outname.Clear();
64     outname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
65     outname.Append(wxT("_"));
66     outname.Append(wxString::FromUTF8(CPPFilenameOnly.c_str()));
67     outname.Append(wxT("_png"));
68     outname.MakeLower();
69     
70     // Convert the PNG file into an unsigned char array.
71     
72     cppfile << "static unsigned char " << outname.mb_str() <<
73         "[] = {" << endl;
74     
75     while(pngfile){
76         pngfile.get(pngfile_char);
77         cppfile << "0x";
78            
79         if ((unsigned short)pngfile_char > 255){
80         
81             cppfile << hex << setw(2) << setfill('0')  << 
82                 ((unsigned short)pngfile_char - 65280);
83             
84         } else {
85          
86             cppfile << hex << setw(2) << setfill('0')  << 
87                 (unsigned short)pngfile_char;     
88           
89         }
90             
91         cppfile << ", ";
92         pageseek++;
93         
94         if (pageseek == 8){
95             cppfile << endl;
96             pageseek = 0;
97         }
98     }
99     
100     // End the file, close it and increment the counter.
101     
102     cppfile << "};" << endl << endl;
103     cppfile << "#endif" << endl << endl;
104     cppfile.close();
105     
106     cout << "CPP\t" << CPPFilename.c_str() << endl;    
107     
108     ++*counter;
109     
112 int CreateHPPFileDir(wxString dirfilename, wxArrayString filelist, 
113                         int* counter)
115 //-------------------------------------------------------------------
116 // CreateHPPFileDir:    Create a HPP Directory with the directory
117 //                      filename and list of files in the directory
118 //                      given.
119 //
120 // dirfilename  wxString of the directory name.
121 // filelist     wxArrayString of the directory filelist.
122 // counter      Pointer to a integer counter.
123 //-------------------------------------------------------------------  
124     boostfs::path HPPFilename(dirfilename.c_str());
125     boostfs::path CPPFile;
126     boostfs::path CPPFileOnly(CPPFile.filename());
127     boostfs::path DirFilename(dirfilename.c_str());
128     boostfs::path DirFilenameOnly(DirFilename.filename());
129     HPPFilename.replace_extension(".h");
130     fstream hppfile;
131     
132     bool fmatch = FALSE;
133     wxString finaldirname;
134     
135     // Make the directory filename upper case for writing to the
136     // header file.
137     
138     finaldirname.Append(wxString::FromUTF8(DirFilenameOnly.c_str()));
139     finaldirname.MakeUpper();
140     
141     // Write out the header file inclusion guard.
142     
143     hppfile.open(HPPFilename.c_str(), ios::out | ios::trunc );
144     
145     hppfile << "#include <iostream>" << endl << endl;
146     hppfile << "#ifndef " << finaldirname.mb_str() << "_H" << endl;
147     hppfile << "#define " << finaldirname.mb_str() << "_H" << endl << endl;
148     hppfile << "// List all CPP files in the directory." << endl << endl;
149     
150     // Write each CPP file into the header file.
151     
152     for (int f = 0; f < filelist.GetCount() ; f++){
153         boostfs::path CPPFile(filelist[f].c_str());
154         boostfs::path CPPFileOnly(CPPFile.filename());
155         CPPFileOnly.replace_extension(".cpp");
156         
157         for (boostfs::path::iterator filet=CPPFile.begin(); 
158              filet != CPPFile.end(); ++filet){
159             if (*filet == DirFilename.filename()){
160                 fmatch = TRUE;
161                 
162             }
163             if (fmatch == TRUE){
164                 
165             }
166         }
167         
168         hppfile << "#include \"" << DirFilenameOnly.c_str() << 
169             "/" << CPPFileOnly.c_str() << "\"" << endl;
170     }
171     
172     // Write the end if and close the file.
173     
174     hppfile << endl << "#endif" << endl << endl;
175     hppfile.close();
176     
177     // Increment the HPP file counter.
179     cout << "HPPDIR\t" << HPPFilename.c_str() << endl;
180     ++*counter;
181     
184 int main()
186     int fp = 0;
187     int cppg = 0;
188     int hppg = 0;
189     
190     wxArrayString dirlist;
191     wxArrayString filelist;
192     wxString DirFilenameWxS;
193     
194     fstream finalhppfile;
195   
196     // Look in the subdirectories of the bitmaps directory and
197     // collect the names of the directories.
198     
199     std::cout << "Working out directories in bitmaps directory..." << 
200         std::endl;
201     
202     boostfs::path BitmapsDir("../bitmaps");
203     boostfs::path BitmapsDirSubName;
204     boostfs::path BitmapsFilename;
205     boostfs::directory_iterator dir_end;
206     
207     if ( boostfs::exists(BitmapsDir) ){
208     
209         for (boostfs::directory_iterator bitmapsidr_iter(BitmapsDir); 
210             bitmapsidr_iter != dir_end ; ++bitmapsidr_iter){
211             
212             if (boostfs::is_directory(bitmapsidr_iter->status())){
213                 
214                 BitmapsDirSubName = boostfs::path(bitmapsidr_iter->path()).filename();
215                 DirFilenameWxS.Append(wxString::FromUTF8("../bitmaps/"));               
216                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsDirSubName.c_str()));
217                 dirlist.Add(DirFilenameWxS, 1);
218                 DirFilenameWxS = wxT("");
220             }
221             
222         }
223       
224     } else {
225         std::cout << "Error: Bitmaps Directory doesn't exist!" << std::endl;
226         return 1;
227     }
228     
229     // Process each directory, generating a .cpp and .hpp file 
230     // for each image and then a final .hpp for the directory
231     // containing the .hpp's for the directories.
232     
233     if (dirlist.GetCount() == 0){
234         cout << "Error: No directories in the bitmaps folder. Unexpected behaviour!" << endl;
235         return 1;
236     }
237     
238     std::cout << "Looking in bitmaps folder for PNGs..." << std::endl;
239     
240     DirFilenameWxS.Empty();
241     
242     finalhppfile.open("../bitmaps.h", ios::out | ios::trunc );
243     finalhppfile << "#include <iostream>" << endl << endl;
244     finalhppfile << "#ifndef BITMAPS_H" << endl;
245     finalhppfile << "#define BITMAPS_H" << endl << endl;
246     
247     for (int bi = 0; bi<dirlist.GetCount() ; bi++)
248     {
249         boostfs::path BitmapsSubDir(dirlist[bi].c_str());
250         for (boostfs::directory_iterator bitmapsidr_iter(BitmapsSubDir); 
251                 bitmapsidr_iter != dir_end ;
252                 ++bitmapsidr_iter){
253             
254             if (boostfs::path(bitmapsidr_iter->path()).extension() == ".png" ||
255               boostfs::path(bitmapsidr_iter->path()).extension() == ".PNG" ){
256                               
257                 BitmapsFilename = boostfs::path(bitmapsidr_iter->path()).filename();
258                 DirFilenameWxS.Append(wxString::FromUTF8(dirlist[bi].mb_str()));
259                 DirFilenameWxS.Append(wxString::FromUTF8("/"));
260                 DirFilenameWxS.Append(wxString::FromUTF8(BitmapsFilename.c_str()));
261                 
262                 filelist.Add(DirFilenameWxS, 1);
263                 DirFilenameWxS = wxT("");
264                 
266                 
267             }
268          
269         }
270         
271         for (int fi = 0; fi < filelist.GetCount() ; fi++)
272         {
273             CreateCPPFile(dirlist[bi].c_str(), filelist[fi], &cppg);        
274             fp++;
275         }
276         
277         if (filelist.GetCount() > 0)
278         {
279             CreateHPPFileDir(dirlist[bi].c_str(), filelist, &hppg);
280         }       
281                 
282         filelist.Clear();
284         finalhppfile << "#include \"bitmaps/"<< BitmapsSubDir.filename().c_str() <<
285             ".h\"" << endl;
286       
287     }
288     
289     finalhppfile << endl << "#endif" << endl;
290     finalhppfile.close();
291     ++hppg;
292     
293     // Print out the results.
295     std::cout << "Finished processing PNGs into code." << std::endl;
296     std::cout << fp << " files processed." << std::endl;
297     std::cout << cppg << " .cpp files generated." << std::endl;
298     std::cout << hppg << " .hpp files generated." << std::endl;
299     
300     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