Google
Web alhem.net
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

mydp.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 #include <stdio.h>
00024 #include <string>
00025 #include <sys/types.h>
00026 #include <dirent.h>
00027 #include <vector>
00028 #include <sys/stat.h>
00029 #include <unistd.h>
00030 #include <map>
00031 #include "Parse.h"
00032 
00033 #include "Token.h"
00034 #include "MyParser.h"
00035 #include "mydp.h"
00036 #include "Scope.h"
00037 
00038 #define DEB(x)
00039 
00040 
00041 /* This is a comment */
00042 
00043 namespace ugly { 
00044         int beep; 
00045 }
00046 
00047         config_m config;
00048         std::string includes = "";
00049 
00050 void read_makefile()
00051 {
00052         FILE *fil;
00053         if ((fil = fopen("Makefile","rt")) != NULL)
00054         {
00055                 char slask[1000];
00056                 fgets(slask,1000,fil);
00057                 while (!feof(fil))
00058                 {
00059                         Parse pa(slask);
00060                         strcpy(slask,pa.getword().c_str());
00061                         while (*slask)
00062                         {
00063                                 if (!strncmp(slask,"-I",2))
00064                                 {
00065                                         if (includes.size())
00066                                                 includes += " ";
00067                                         includes += slask;
00068                                 }
00069                                 //
00070                                 strcpy(slask,pa.getword().c_str());
00071                         }
00072                         //
00073                         fgets(slask,1000,fil);
00074                 }
00075                 fclose(fil);
00076         }
00077 }
00078 
00079 
00080 void print_parent(Scope *p)
00081 {
00082         if (!p)
00083                 return;
00084         if (p -> GetParent())
00085         {
00086                 print_parent(p -> GetParent());
00087                 p -> Tab();
00088                 printf("%s\n",p -> GetExpr().c_str());
00089         }
00090 }
00091 
00092 
00093 void class_recursive(Scope *root)
00094 {
00095         sequence_v::iterator it = root -> ScopeBegin();
00096         Token *t = root -> GetFirstToken();
00097 
00098         if (t)
00099         {
00100                 if (t -> GetCode() == Class)
00101                 {
00102                         print_parent( root -> GetParent() );
00103                         root -> Tab();
00104                         printf("%s",root -> GetExpr().c_str());
00105                         if (root -> IsExtern())
00106                                 printf("  <extern>");
00107                         printf("\n");
00108                         root -> Tab();
00109                         printf("\tFile: %s:%d\n",root -> GetFile().c_str(),root -> GetLine());
00110                         printf("\n");
00111                 }
00112                 else
00113                 if (t -> GetCode() == Template)
00114                 {
00115                         token_v::iterator it = root -> Begin();
00116                         int lvl = 0;
00117                         while (it != root -> End())
00118                         {
00119                                 Token *t = *it;
00120                                 if (t)
00121                                 {
00122                                         if (t -> GetCode() == '<')
00123                                                 lvl++;
00124                                         else
00125                                         if (t -> GetCode() == '>')
00126                                                 lvl--;
00127                                         else
00128                                         {
00129                                                 if (!lvl && it != root -> Begin())
00130                                                 {
00131                                                         if (t -> GetCode() == Class)
00132                                                         {
00133                                                                 print_parent( root -> GetParent() );
00134                                                                 root -> Tab();
00135                                                                 printf("%s",root -> GetExpr().c_str());
00136                                                                 if (root -> IsExtern())
00137                                                                         printf("  <extern>");
00138                                                                 printf("\n");
00139                                                                 root -> Tab();
00140                                                                 printf("\tFile: %s:%d\n",root -> GetFile().c_str(),root -> GetLine());
00141                                                                 printf("\n");
00142                                                         }
00143                                                         else
00144                                                         {
00145                                                         }
00146                                                         break;
00147                                                 }
00148                                         }
00149                                 }
00150                                 //
00151                                 it++;
00152                         }
00153                 }
00154         }
00155 
00156         while (it != root -> ScopeEnd())
00157         {
00158                 Sequence *p = *it;
00159                 Scope *scope = dynamic_cast<Scope *>(p);
00160                 if (scope)
00161                 {
00162                         class_recursive( scope );
00163                 }
00164                 //
00165                 it++;
00166         }
00167 }
00168 
00169 
00170 void class_hierarchy(Scope *root)
00171 {
00172         class_recursive(root);
00173 }
00174 
00175 
00176 void parse_file(const std::string &path,const std::string &filename)
00177 {
00178         std::string cmd;
00179         std::string out;
00180         if (config["-cpp"] != "true")
00181         {
00182                 out = path + "/" + filename + ".preprocessed";
00183                 cmd = "/usr/bin/cpp"
00184                         " -C" 
00185                         " " + includes +
00186                         " " + path + "/" + filename + " > " + out;
00187 fprintf(stderr,"%s\n", cmd.c_str());
00188                 if (system( cmd.c_str() ) == -1)
00189                 {
00190 DEB(                    fprintf(stderr,"Command failed: '%s'...\n",cmd.c_str());)
00191                         return;
00192                 }
00193         }
00194         else
00195         {
00196                 out = path + "/" + filename + ".preprocessed";
00197                 cmd = "cat"
00198                         " " + path + "/" + filename + " > " + out;
00199 fprintf(stderr,"%s\n", cmd.c_str());
00200                 if (system( cmd.c_str() ) == -1)
00201                 {
00202 DEB(                    fprintf(stderr,"Command failed: '%s'...\n",cmd.c_str());)
00203                         return;
00204                 }
00205         }
00206 
00207         FILE *fil = fopen(out.c_str(),"rt");
00208         if (fil)
00209         {
00210                 MyParser p(path);
00211                 std::string name;
00212                 Scope *result;
00213                 name = path + "/";
00214                 name += filename;
00215                 result = p.Parser(fil,name);
00216                 fclose(fil);
00217                 unlink(out.c_str());
00218                 //
00219                 if (config["-xml"] == "true")
00220                 {
00221                         printf("<?xml version=\"1.0\"?>\n");
00222                         printf("<code>\n");
00223                         result -> Display();
00224                         printf("</code>\n");
00225                 }
00226                 else
00227                 if (config["-disp"] == "true")
00228                         result -> Display();
00229                 else
00230                         class_hierarchy( result );
00231                 delete result;
00232         }
00233         else
00234         {
00235                 fprintf(stderr,"Couldn't open '%s'...\n",filename.c_str());
00236                 return;
00237         }
00238 }
00239 
00240 
00241 int main(int argc,char *argv[])
00242 {
00243         char *args[] = {"-disp","-xml","-cpp",""};
00244         read_makefile();
00245         if (includes.size())
00246                 fprintf(stderr,"Using include path(s): %s\n",includes.c_str());
00247         if (argc == 1)
00248         {
00249                 fprintf(stderr,"Usage: %s [-cpp] [-disp] [-xml] file(s)\n",*argv);
00250                 fprintf(stderr," -cpp     Don't use cpp precompiler\n");
00251                 fprintf(stderr," -disp    View parsed and formatted source code\n");
00252                 fprintf(stderr," -xml     View parsed code as xml\n");
00253                 return 0;
00254         }
00255         for (int i = 1; i < argc; i++)
00256         {
00257                 bool ok = false;
00258                 bool more = false;
00259                 int x = 0;
00260                 while (*args[x])
00261                 {
00262                         if (!strcmp(argv[i],args[x]))
00263                         {
00264                                 ok = true;
00265                                 // check for more
00266                                 break;
00267                         }
00268                         else
00269                         {
00270                                 x++;
00271                         }
00272                 }
00273                 if (ok)
00274                 {
00275                         if (more)
00276                         {
00277                                 config[argv[i]] = argv[i + 1];
00278                                 i++;
00279                         }
00280                         else
00281                         {
00282                                 config[argv[i]] = "true";
00283                         }
00284                 }
00285                 else
00286                 {
00287                         parse_file(".", argv[i]);
00288                 }
00289         }
00290 }
00291 
00292 

Generated for My SDL C++ Gui by doxygen 1.3.6