00001 #include <stdio.h> 00002 #include <qpe/qpeapplication.h> 00003 #include <qfileinfo.h> 00004 #include <qmessagebox.h> 00005 #include <qfile.h> 00006 #include <qtextstream.h> 00007 00008 #include "resources.h" 00009 #include "systemfile.h" 00010 00011 #define TEMPLATEDIR "NS2templates/" 00012 QString TemplDir; 00013 00014 SystemFile::SystemFile( const QString & N, 00015 const QString & P, 00016 bool KDI ){ 00017 Name = N; 00018 Path = P; 00019 InAppend = 0; 00020 F = 0; 00021 // get template info 00022 { QString S; 00023 QFileInfo FI; 00024 00025 // find location of templates 00026 TemplDir = QPEApplication::qpeDir() + "etc/" + TEMPLATEDIR; 00027 FI.setFile( TemplDir ); 00028 if( ! FI.isDir() ) { 00029 // try current dir 00030 TemplDir = "./" TEMPLATEDIR; 00031 FI.setFile( TemplDir ); 00032 if( ! FI.isDir() ) { 00033 hasPreSection = 00034 hasPostSection = 00035 hasPreNodeSection = 00036 hasPostNodeSection = 00037 hasPreDeviceSection = 00038 hasPostDeviceSection = 0; 00039 return; 00040 } 00041 } 00042 00043 // have found location 00044 S = TemplDir + Name + "/presection"; 00045 FI.setFile( S ); 00046 hasPreSection = ( FI.exists() && FI.isReadable() ); 00047 S = TemplDir + Name + "/postsection"; 00048 FI.setFile( S ); 00049 hasPostSection = ( FI.exists() && FI.isReadable() ); 00050 S = TemplDir + Name + "/prenodesection"; 00051 FI.setFile( S ); 00052 hasPreNodeSection = ( FI.exists() && FI.isReadable() ); 00053 S = TemplDir + Name + "/postnodesection"; 00054 FI.setFile( S ); 00055 hasPostNodeSection = ( FI.exists() && FI.isReadable() ); 00056 S = TemplDir + Name + "/predevicesection"; 00057 FI.setFile( S ); 00058 hasPreDeviceSection = ( FI.exists() && FI.isReadable() ); 00059 S = TemplDir + Name + "/postdevicesection"; 00060 FI.setFile( S ); 00061 hasPostDeviceSection = ( FI.exists() && FI.isReadable() ); 00062 } 00063 00064 KnowsDeviceInstances = KDI; 00065 } 00066 00067 SystemFile::SystemFile( const QString & N, bool KDI ){ 00068 Name = N; 00069 Path = ""; 00070 InAppend = 0; 00071 F =0; 00072 KnowsDeviceInstances = KDI; 00073 hasPreSection = 00074 hasPostSection = 00075 hasPreNodeSection = 00076 hasPostNodeSection = 00077 hasPreDeviceSection = 00078 hasPostDeviceSection = 0; 00079 } 00080 00081 SystemFile::~SystemFile( void ) { 00082 close(); 00083 } 00084 00085 bool SystemFile::open( void ) { 00086 QString Prefix = getenv( "NS2OUTPUTTO" ); 00087 00088 if( Prefix != "stderr" /* && Name != "interfaces" */ ) { 00089 // generate files where the need to be 00090 if( F ) { 00091 F->close(); 00092 delete F; 00093 } 00094 00095 F = new QFile( Prefix + Path + ((InAppend)?"":"bup") ); 00096 Log(( "Open systemfile %s\n", F->name().latin1() )); 00097 if( ! F->open( ((InAppend)?IO_Append : 0 ) | IO_WriteOnly ) ) { 00098 return 0; 00099 } 00100 } else { 00101 if( ! F ) { 00102 odebug << "!!!!!!!!!!!!!!!!!! " << oendl; 00103 odebug << "!!!! TESTMODE !!!!" << oendl; 00104 odebug << "!!!!!!!!!!!!!!!!!! " << oendl; 00105 odebug << "!!!!" << oendl; 00106 odebug << "!!!! GENERATE " << Path << oendl; 00107 if( InAppend ) { 00108 odebug << "!!!! In APPEND mode" << oendl; 00109 } 00110 odebug << "!!!!" << oendl; 00111 odebug << "!!!!!!!!!!!!!!!!!!" << oendl; 00112 00113 F = new QFile(); 00114 F->open( IO_WriteOnly, stderr ); 00115 } 00116 } 00117 setDevice( F ); 00118 return 1; 00119 } 00120 00121 bool SystemFile::close( void ) { 00122 if( ! F || ! F->isOpen() ) { 00123 return 1 ; 00124 } 00125 00126 QString Prefix = getenv( "NS2OUTPUTTO" ); 00127 00128 if( Prefix == "stderr" ) { 00129 return 1; 00130 } 00131 00132 QString OldP = Prefix + Path + "bup"; 00133 00134 F->close(); 00135 delete F; 00136 F = 0; 00137 00138 if( ! InAppend ) { 00139 return ( rename( OldP.latin1(), Path.latin1() ) >= 0 ); 00140 } 00141 return 1; 00142 } 00143 00144 bool SystemFile::preSection( void ) { 00145 if( hasPreSection ) { 00146 QFile Fl( TemplDir + Name + "/presection" ); 00147 if( ! Fl.open( IO_ReadOnly ) ) 00148 return 0; // error 00149 // copy file to this file 00150 F->writeBlock( Fl.readAll() ); 00151 } 00152 return 1; 00153 } 00154 00155 bool SystemFile::postSection( void ) { 00156 if( hasPostSection ) { 00157 QFile Fl( TemplDir + Name + "/postsection" ); 00158 if( ! Fl.open( IO_ReadOnly ) ) 00159 return 0; // error 00160 // copy file to this file 00161 F->writeBlock( Fl.readAll() ); 00162 } 00163 return 1; 00164 } 00165 00166 bool SystemFile::preNodeSection( ANetNodeInstance * NNI, long ) { 00167 if( hasPreNodeSection ) { 00168 QFile Fl( TemplDir + Name + "/prenodesection" ); 00169 if( ! Fl.open( IO_ReadOnly ) ) 00170 return 0; // error 00171 QTextStream TX( &Fl ); 00172 QString Out; 00173 QString S = TX.readLine(); 00174 while( ! TX.eof() ) { 00175 Out = S. 00176 arg(NNI->nodeClass()->name()); 00177 (*this) << Out << endl; 00178 S = TX.readLine(); 00179 } 00180 } 00181 return 1; 00182 } 00183 00184 bool SystemFile::postNodeSection( ANetNodeInstance * NNI, long ) { 00185 if( hasPostNodeSection ) { 00186 QFile Fl( TemplDir + Name + "/postnodesection" ); 00187 if( ! Fl.open( IO_ReadOnly ) ) 00188 return 0; // error 00189 QTextStream TX( &Fl ); 00190 QString Out; 00191 QString S = TX.readLine(); 00192 while( ! TX.eof() ) { 00193 Out = S. 00194 arg(NNI->name()); 00195 (*this) << Out << endl; 00196 S = TX.readLine(); 00197 } 00198 } 00199 return 1; 00200 } 00201 00202 bool SystemFile::preDeviceSection( ANetNode * NN ) { 00203 if( hasPreDeviceSection ) { 00204 QFile Fl( TemplDir + Name + "/predevicesection" ); 00205 if( ! Fl.open( IO_ReadOnly ) ) 00206 return 0; // error 00207 QTextStream TX( &Fl ); 00208 QString Out; 00209 QString S = TX.readLine(); 00210 while( ! TX.eof() ) { 00211 Out = S.arg(NN->name()); 00212 (*this) << Out << endl; 00213 S = TX.readLine(); 00214 } 00215 } 00216 return 1; 00217 } 00218 00219 bool SystemFile::postDeviceSection( ANetNode * NN ) { 00220 if( hasPostDeviceSection ) { 00221 QFile Fl( TemplDir + Name + "/postdevicesection" ); 00222 if( ! Fl.open( IO_ReadOnly ) ) 00223 return 0; // error 00224 QTextStream TX( &Fl ); 00225 QString Out; 00226 QString S = TX.readLine(); 00227 while( ! TX.eof() ) { 00228 Out = S.arg(NN->name()); 00229 (*this) << Out << endl; 00230 S = TX.readLine(); 00231 } 00232 } 00233 return 1; 00234 }
1.4.2