00001 #include "SdlArs.h"
00002
00003 static deque<Ars::Form *> Stack;
00004
00005 namespace Ars
00006 {
00007
00008 icstring &strip( icstring &s )
00009 {
00010 icstring::size_type idx = s.find_first_not_of( "\t \n" );
00011 if( idx != string::npos )
00012 s = s.substr( idx );
00013 int i = (s.length() - 1);
00014 while( i > 0 && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n') ) i--;
00015 s = s.substr( 0 , i + 1 );
00016
00017 return s;
00018 }
00019
00020 SDL_mutex *rsrcMutex = 0;
00021
00022 const bool AddCallBack( const string str , void *f )
00023 {
00024 return ArsApplication::lbGetter.insert( make_pair( icstring( str.c_str() ) , f ) ).second;
00025 }
00026
00027 Rsrc::Rsrc( const string &aFilename )
00028 : buffer( 0 )
00029 {
00030 string fname = aFilename.empty() ? Cfg::GetCfg().GetCfgString( "Paths" , string( "Dlg-" ) + ArsApplication::GetCoordStr() ) : aFilename;
00031 if( fname[0] == '@' )
00032 fname = Cfg::GetCfg( aFilename ).GetFilename();
00033
00034 FILE *fd = fopen( fname.c_str() , "r" );
00035 if( fd )
00036 {
00037 fseek( fd , 0 , SEEK_END );
00038 long flen = ftell( fd );
00039 fseek( fd , 0 , SEEK_SET );
00040
00041 if( flen )
00042 {
00043 buffer = new char[ flen + 1];
00044 memset( buffer , 0 , flen + 1 );
00045 fread( buffer , flen , sizeof( char ) , fd );
00046 }
00047 fclose( fd );
00048 }
00049 }
00050
00051 Rsrc::~Rsrc( void )
00052 {
00053 if( buffer )
00054 delete buffer;
00055 buffer = 0;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 Form *Rsrc::Load( const string &aSection , const int aId )
00080 {
00081 Form *f = GetAddr( aId );
00082 if( f )
00083 return f;
00084 icstring tmp = aSection.c_str();
00085 tmp = FindCombination( tmp );
00086 icstring DlgStr = strip( tmp );
00087 f = new Form( aId );
00088
00089 while( !DlgStr.empty() )
00090 {
00091 icstring obdef = GetList( DlgStr );
00092 if( !obdef.empty() )
00093 {
00094 icstring sid = GetId( obdef );
00095 Window *wnd = 0;
00096
00097 for( vector<cbObjCreateType>::iterator it = ArsApplication::objCreate.begin() ; it != ArsApplication::objCreate.end() ; ++it )
00098 if( (wnd = (*it)( sid )) != 0 )
00099 break;
00100 if( wnd == 0 )
00101 wnd = new Window();
00102
00103 wnd->Init( obdef );
00104 f->push_back( wnd );
00105 }
00106 }
00107 Stack.push_back( f );
00108
00109 return f;
00110 }
00111
00112 Form *Rsrc::GetAddr( const int id )
00113 {
00114 deque<Form *>::iterator at = find_if( Stack.begin() , Stack.end() , bind2nd( mem_fun( &Form::HasId ) , &id ) );
00115 if( at != Stack.end() )
00116 return *at;
00117
00118 return 0;
00119 }
00120
00121 void Rsrc::SetAddr( Form *f )
00122 {
00123 Stack.push_back( f );
00124 }
00125
00126 const bool Rsrc::Free( const int id )
00127 {
00128 if( !Stack.empty() )
00129 {
00130 deque<Form *>::iterator at = find_if( Stack.begin() , Stack.end() , bind2nd( mem_fun( &Form::HasId ) , &id ) );
00131 if( at != Stack.end() )
00132 {
00133 Form *f = *at;
00134 Stack.erase( at );
00135
00136 delete f;
00137 return true;
00138 }
00139 }
00140 return false;
00141 }
00142
00143 icstring Rsrc::FindCombination( const icstring &aSection )
00144 {
00145 char *s = buffer , *s2 = 0;
00146 bool found = false;
00147 while( !found && s && *s )
00148 {
00149 while( *s && *s != '(' )
00150 {
00151 if( *s == ';' || (*s == '/' && s[1] == '/') )
00152 while( *s && *s != '\n' )
00153 s++;
00154 s++;
00155 }
00156 int cnt = 1;
00157 s2 = ++s;
00158 while( *s2 && cnt )
00159 {
00160 if( *s2 == '(' )
00161 cnt += 1;
00162 else if( *s2 == '\"' )
00163 while( *++s2 != '\"' ) ;
00164 else if( *s2 == ')' )
00165 cnt -= 1;
00166 s2 += 1;
00167 }
00168
00169 s2 -= 2;
00170 if( aSection == icstring( s , aSection.length() ) )
00171 found = true;
00172 else
00173 s = s2 + 2;
00174 }
00175
00176 if( found && s < s2 )
00177 {
00178 while( *s && *s != '(' ) s++;
00179
00180 if( s < s2 )
00181 return icstring( s , s2 - s );
00182 }
00183
00184 return "";
00185 }
00186
00188
00189 icstring Rsrc::FindCrdOf( icstring aStr , const icstring &car )
00190 {
00191 SDL_mutexP( rsrcMutex );
00192 icstring ans = GetList( aStr );
00193 while( !ans.empty() )
00194 if( GetId( ans ) == car )
00195 break;
00196 else
00197 ans = GetList( aStr );
00198
00199 ans = strip( ans );
00200 SDL_mutexV( rsrcMutex );
00201
00202 return ans;
00203 }
00204
00205 icstring Rsrc::GetId( icstring &aStr )
00206 {
00207 SDL_mutexP( rsrcMutex );
00208 int i = 0;
00209 while( aStr[i] && !isalnum( aStr[i] ) ) i += 1;
00210 icstring ans;
00211
00212 if( aStr[i] )
00213 {
00214 while( aStr[i] && (isalnum( aStr[i] ) || aStr[i] == '_') )
00215 ans += aStr[i++];
00216 }
00217 aStr = aStr.substr( i );
00218 SDL_mutexV( rsrcMutex );
00219 return ans;
00220 }
00221
00222 int Rsrc::GetInt( icstring &aStr , const int aDefault )
00223 {
00224 SDL_mutexP( rsrcMutex );
00225 int i = 0;
00226 while( aStr[i] && !(isdigit( aStr[i] ) || aStr[i] == '-') ) i += 1;
00227
00228 int ans = aDefault;
00229 if( aStr.substr( i , 2 ) == "0x" || aStr.substr( i , 2 ) == "0X" )
00230 sscanf( aStr.substr( i + 2 ).c_str () , "%x" , &ans );
00231 else
00232 sscanf( aStr.substr( i ).c_str () , "%d" , &ans );
00233
00234 while( aStr[i] && (isxdigit( aStr[i] ) || aStr[i] == '-') ) i += 1;
00235 aStr = aStr.substr( i );
00236 SDL_mutexV( rsrcMutex );
00237
00238 return ans;
00239 }
00240
00241 double Rsrc::GetDbl( icstring &aStr , const double aDefault )
00242 {
00243 SDL_mutexP( rsrcMutex );
00244 int i = 0;
00245 while( aStr[i] && !(isdigit( aStr[i] ) || aStr[i] == '-') ) i += 1;
00246
00247 double ans = aDefault;
00248 sscanf( aStr.substr( i ).c_str () , "%lf" , &ans );
00249
00250 while( aStr[i] && (isdigit( aStr[i] ) || aStr[i] == '-' || aStr[i] == '.') ) i += 1;
00251 aStr = aStr.substr( i );
00252 SDL_mutexV( rsrcMutex );
00253
00254 return ans;
00255 }
00256
00257 string Rsrc::GetStr( icstring &aStr , const std::string aDefault )
00258 {
00259 SDL_mutexP( rsrcMutex );
00260 string::size_type idx = 0;
00261 while( aStr[idx] && aStr[idx] != '\"' ) idx++;
00262
00263 if( aStr[idx] == '\"' )
00264 {
00265 string ans;
00266
00267 aStr = aStr.substr( idx + 1 );
00268 idx = 0;
00269 while( aStr[idx] && aStr[idx] != '\"' )
00270 if( aStr[idx] == '\\' )
00271 idx += 2;
00272 else
00273 idx += 1;
00274 ans = aStr.substr( 0 , idx ).c_str();
00275 aStr = aStr.substr( idx + 1 );
00276
00277 if( !ans.empty() )
00278 {
00279 while( (idx = ans.find( "\\n" )) != string::npos )
00280 {
00281 string rslt = ans.substr( 0 , idx);
00282 rslt += "\n";
00283 rslt += ans.substr( idx + 2 );
00284 ans = rslt;
00285 }
00286 while( (idx = ans.find( "\\t" )) != string::npos )
00287 {
00288 string rslt = ans.substr( 0 , idx );
00289 rslt += "\t";
00290 rslt += ans.substr( idx + 2 );
00291 ans = rslt;
00292 }
00293 while( (idx = ans.find( "\\\"" )) != string::npos )
00294 {
00295 string rslt = ans.substr( 0 , idx);
00296 rslt += "\"";
00297 rslt += ans.substr( idx + 2 );
00298 ans = rslt;
00299 }
00300 }
00301 else
00302 {
00303 SDL_mutexV( rsrcMutex );
00304 return aDefault;
00305 }
00306 SDL_mutexV( rsrcMutex );
00307 return ans;
00308 }
00309 SDL_mutexV( rsrcMutex );
00310 return aDefault;
00311 }
00312
00313 icstring Rsrc::GetList( icstring &aStr )
00314 {
00315 SDL_mutexP( rsrcMutex );
00316 icstring::size_type idx = aStr.find( "(" );
00317 icstring ans;
00318 if( idx != string::npos && aStr[idx] == '(' )
00319 {
00320 aStr = aStr.substr( idx + 1 );
00321 int cnt = 1 , i = -1;
00322 while( cnt && aStr[++i] )
00323 {
00324 if( aStr[i] == '\"' )
00325 {
00326 i += 1;
00327 while( aStr[i] && aStr[i] != '\"' ) i++;
00328 }
00329 else if( aStr[i] == ')' )
00330 cnt -= 1;
00331 else if( aStr[i] == '(' )
00332 cnt += 1;
00333 }
00334 ans = aStr.substr( 0 , i++ );
00335 aStr = aStr.substr( i );
00336 }
00337 SDL_mutexV( rsrcMutex );
00338 return strip( ans );
00339 }
00340
00341 RGBColor Rsrc::GetColor( icstring &aStr , const RGBColor &defaultColor )
00342 {
00343 SDL_mutexP( rsrcMutex );
00344 int c = GetInt( aStr , (defaultColor.alpha << 24 | defaultColor.red << 16 | defaultColor.green << 8 | defaultColor.blue) );
00345 SDL_mutexV( rsrcMutex );
00346 return RGBColor( (unsigned char)( (c >> 16) & 0xff ) , (unsigned char)( (c >> 8) & 0xff ) , (unsigned char)( c & 0xff ) , (unsigned char)( c & 0xff000000 ? (c >> 24) & 0xff : 0xff ) );
00347 }
00348
00349 int Rsrc::GetMnemo( icstring &aStr , map<icstring,unsigned long> &aMnemo , const int aDefault )
00350 {
00351 SDL_mutexP( rsrcMutex );
00352 string::size_type idx = 0;
00353
00354 while( aStr[idx] && !isalpha( aStr[idx] ) ) idx++;
00355 icstring ans = aStr.substr( idx );
00356 aStr = aStr.substr( idx );
00357
00358 int val = aDefault;
00359
00360 while( (idx = ans.find( "|" )) != string::npos )
00361 {
00362 icstring s = ans.substr( 0 , idx );
00363 if( aMnemo.find( s ) != aMnemo.end() )
00364 val |= aMnemo[strip( s )];
00365 else
00366 val |= GetInt( s );
00367 ans = ans.substr( idx + 1 );
00368 }
00369 if( aMnemo.find( ans ) != aMnemo.end() )
00370 val |= aMnemo[strip( ans )];
00371 else if( aMnemo.find( ans ) != aMnemo.end() )
00372 val |= GetInt( ans );
00373
00374 SDL_mutexV( rsrcMutex );
00375
00376 return val;
00377 }
00378
00379
00380 }