int CIniFile::Split( const CStr &source, LPCTSTR sep, CStrArray &dest, BOOL trim /* = TRUE */ ) { int pos = source.Find( sep ); int startPos = 0; CStr elem; int sepLen = wcslen( sep ); dest.RemoveAll(); while( pos != -1 ) { elem = source.Mid( startPos, pos-startPos ); if ( trim ) { elem.TrimLeft(); elem.TrimRight(); } dest.Add( elem ); startPos = pos+sepLen; pos = source.Find( sep, startPos ); } elem = source.Mid( startPos ); if ( trim ) { elem.TrimLeft(); elem.TrimRight(); } dest.Add( elem ); return dest.GetSize(); }
CValue *GetVariable( CStr &var, BOOL create, int *position, BOOL local ) { int pos, startPos; bool oldStyle = false, reference = false, isPtr = false; CStr entryName; CMapStrToValue *map = NULL; CValue *value; CValue *dummy; pos = 0; if ( !local ) { if ( LocalVariables != NULL && LocalVariables->Lookup( L"%AUTOLOCAL%", dummy ) ) local = TRUE; } // Skip spaces while ( pos < var.GetLength() && GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; if ( pos < var.GetLength() && var[pos] == L'%' ) { oldStyle = true; pos++; while ( pos < var.GetLength() && GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; } if ( create && pos < var.GetLength() && var[pos] == L'[' ) { reference = true; pos++; while ( pos < var.GetLength() && GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; } startPos = pos; // Skip all valid variable name characters while ( pos < var.GetLength() && ( GetCharacterType( var[pos] ) == CInterpreter::CHAR_ALPHA || GetCharacterType( var[pos] ) == CInterpreter::CHAR_DIGIT || GetCharacterType( var[pos] ) == CInterpreter::CHAR_UNDERSCORE || (var[pos] == L':' && (pos == startPos || pos == startPos + 1 )) ) ) pos++; // Skip Regex variable name characters if ((var[pos]==L'$') && (pos+1<var.GetLength())){ if ( var[pos+1] == L'&' || var[pos+1] == L'`' || var[pos+1] == L'\'' || var[pos+1] == L'+' || var[pos+1] == L'_' )pos +=2; else{ int i=0; for (i=1;i<=3;i++){ if ( pos+i<var.GetLength() && var[pos+i] >=L'0' && var[pos+i]<=L'9') { /* do nothing */ }else break; } pos+=i; } } // Skip spaces while ( pos < var.GetLength() && GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; //jwz:add for Global Variable if (var[startPos]==L':' && var[startPos+1]==L':'){ local = FALSE; startPos +=2; } // No array index? if ( pos == -1 || pos >= var.GetLength() || var[pos] != L'[' ) { entryName = var; if ( pos != -1 && pos <= var.GetLength() ) entryName = var.Mid( startPos, pos-startPos ); entryName.TrimLeft(); entryName.TrimRight(); if ( position != NULL ) *position = pos; if ( entryName.IsEmpty()) { VarError = L"Empty variable name not allowed"; if ( position != NULL ) *position = -1; return NULL; } entryName.MakeUpper(); if ( local && LocalVariables != NULL && LocalVariables->Lookup( L"%GL_" + entryName + L"%", dummy ) ) //predefined GLOBAL declare variable { local = FALSE; } if ( local && LocalVariables != NULL && LocalVariables->Lookup( entryName, value ) ) {} else if ( local == FALSE && Variables.Lookup( entryName, value ) ) {} else { if ( create ) { // MessageBox( NULL, L"%GL_" + entryName + L"%", L"Debug", MB_OK ); if ( LocalVariables != NULL && local ) { LocalVariables->SetAt( entryName, CValue() ); LocalVariables->Lookup( entryName, value ); } else { Variables.SetAt( entryName, CValue() ); Variables.Lookup( entryName, value ); } } else value = NULL; } } else { entryName = var.Mid( startPos, pos-startPos ); entryName.TrimLeft(); entryName.TrimRight(); entryName.MakeUpper(); if ( entryName.IsEmpty() ) { VarError = L"'[' without array name"; if ( position != NULL ) *position = -1; value = NULL; } else { if ( local && LocalVariables != NULL && LocalVariables->Lookup( L"%GL_" + entryName + L"%", dummy ) ) { local = FALSE; } if ( local && LocalVariables != NULL && LocalVariables->Lookup( entryName, value ) ) {} else if ( local == FALSE && Variables.Lookup( entryName, value ) ) {} else { if ( create ) { if ( LocalVariables != NULL && local ) { LocalVariables->SetAt( entryName, CValue() ); LocalVariables->Lookup( entryName, value ); } else { Variables.SetAt( entryName, CValue() ); Variables.Lookup( entryName, value ); } } else value = NULL; } if ( value != NULL ) map = value->GetMap(); while ( pos != -1 && pos < var.GetLength() && var[pos] == L'[' ) { startPos = pos+1; CStr arrIdxExpr = var.Mid( startPos ); CInterpreter parser; entryName = (LPCTSTR)parser.EvaluateExpression( arrIdxExpr, FALSE ); if ( parser.GetError() != 0 ) { VarError = parser.GetErrorMessage(); if ( position != NULL ) *position = -1; value = NULL; break; } if ( entryName.IsEmpty() != 0 ) { VarError = L"Array index is empty"; if ( position != NULL ) *position = -1; value = NULL; break; } pos = startPos + parser.GetErrorPosition(); if ( var[pos] != L']' ) { VarError = L"Missing ']'"; if ( position != NULL ) *position = -1; value = NULL; break; } if ( position != NULL ) *position = pos+1; while ( pos < var.GetLength() && parser.GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; entryName.MakeUpper(); BOOL isArray = FALSE; if ( pos <= var.GetLength() && var[pos+1] == L'[' ) { isArray = TRUE; pos++; } if ( value != NULL ) { if ( map != NULL ) { if ( map->Lookup( entryName, value ) == FALSE ) { if ( create ) { map->SetAt( entryName, CValue() ); map->Lookup( entryName, value ); if ( isArray ) map = value->GetMap(); } else value = NULL; } else if ( isArray ) map = value->GetMap(); } } } } } if ( create && reference && value != NULL ) { while ( pos < var.GetLength() && GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; if ( pos < var.GetLength() && var[pos] == L']' ) { pos++; CStr referredVar = (CStr)*value; value = GetVariable( referredVar, create ); } else { VarError = L"Missing ']'"; pos = -1; if ( position != NULL ) *position = -1; value = NULL; } } if ( oldStyle && value != NULL ) { while ( pos < var.GetLength() && GetCharacterType( var[pos] ) == CInterpreter::CHAR_WHITESPACE ) pos++; if ( pos < var.GetLength() && var[pos] == L'%' ) { pos++; //CStr referredVar = (LPCTSTR)value; //value = GetVariable( referredVar, create ); } else { VarError = L"Missing '%'"; pos = -1; if ( position != NULL ) *position = -1; value = NULL; } } return value; }
void CIniFile::Parse( LPCTSTR cont ) { CStr content = cont; CStr sectionName, key, value; CMapStrToString *section = NULL; BOOL hasEmptySection = FALSE; Sections.RemoveAll(); while ( content.GetLength() > 0 ) { CStr line; int pos = content.Find( _T("\n") ); if ( pos != -1 ) { line = content.Left( pos ); content = content.Mid( pos+1 ); } else { line = content; content = _T(""); } line.TrimLeft(); line.TrimRight(); if ( line.GetLength() > 0 && line.GetAt(0) != '#' && line.GetAt(0) != ';' ) { if ( line.GetAt(0) == '[' && line.Right(1) == L"]" ) { sectionName = line.Mid( 1, line.GetLength()-2 ); sectionName.MakeLower(); section = new CMapStrToString(); Sections.SetAt( sectionName, section ); } else { int eqPos = line.Find( '=' ); if ( eqPos != -1 ) { key = line.Left( eqPos ); key.TrimLeft(); key.TrimRight(); value = line.Mid( eqPos+1 ); value.TrimLeft(); value.TrimRight(); if ( value.Left(1) == L"\"" && value.Right(1) == L"\"" ) { value = value.Mid( 1, value.GetLength()-2 ); } key.MakeLower(); if ( section == NULL && hasEmptySection == FALSE ) { section = new CMapStrToString(); Sections.SetAt( L"", section ); hasEmptySection = TRUE; } section->SetAt( key, value ); } } } } }