genericopenlibs/openenvcore/backend/src/StdioRedir/Server/inidata.cpp
branchRCL_3
changeset 29 3520f1ef6bf6
parent 0 e4d67989cc36
equal deleted inserted replaced
28:7cd994b812ca 29:3520f1ef6bf6
   154  //
   154  //
   155  // Find a key's value given a section name and a key name
   155  // Find a key's value given a section name and a key name
   156  //
   156  //
   157 
   157 
   158  TBool CIniData::FindVar(const TDesC &aSectName,const TDesC &aKeyName,TPtrC &aResult)
   158  TBool CIniData::FindVar(const TDesC &aSectName,const TDesC &aKeyName,TPtrC &aResult)
   159  /* Find a text value from given aKeyName and aSecName in the ini data file
   159  	{
   160 
   160  	TInt posStartOfSection(0);
   161  @param aSectName Section containing key
   161  	TInt posEndOfSection(iPtr.Length()); // Default to the entire length of the ini data
   162  @param aKeyName Key being searched for in aSectName
   162  	TPtrC SearchBuf;
   163  @param aResult On return, contains the text result
   163 
   164 
   164  	// If we have a section, set pos to section start
   165  @return ETrue if found, otherwise EFalse
   165  	TInt posI(0);	// Position in internal data Buffer
   166 
   166  	if( aSectName.Length() > 0 )
   167  */
   167  		{
   168      {
   168  		TBool FoundSection(false);
   169 
   169  		while ( ! FoundSection )
   170      __ASSERT_DEBUG(aSectName.Length()<=KTokenSize,Panic(ESectionNameTooBig));
   170  			{
   171      __ASSERT_DEBUG(aKeyName.Length()<=KTokenSize,Panic(EKeyNameTooBig));
   171  			// Move search buffer to next area of interest
   172 
   172  			SearchBuf.Set(iPtr.Mid(posI));
   173      TInt posStartOfSection(0);
   173 
   174      TInt posEndOfSection(iPtr.Length()); // Default to the entire length of the ini data
   174  			// Make up token "[SECTIONNAME]", to search for
   175      TPtrC SearchBuf;
   175  			TPtr sectionToken=iToken->Des();
   176 
   176  			_LIT(sectionTokenFmtString,"[%S]");
   177      // If we have a section, set pos to section start
   177  			sectionToken.Format(sectionTokenFmtString,&aSectName);
   178      TInt posI(0);   // Position in internal data Buffer
   178 
   179      if( aSectName.Length() > 0 )
   179  			// Search for next occurrence of aSectName
   180          {
   180  			TInt posSB = SearchBuf.Find(sectionToken);
   181          TBool FoundSection(false);
   181 
   182          while ( ! FoundSection )
   182  			if (posSB==KErrNotFound)
   183              {
   183  				return(EFalse);
   184              // Move search buffer to next area of interest
   184 
   185              SearchBuf.Set(iPtr.Mid(posI));
   185  			// Check this is at beginning of line (ie. non-commented)
   186 
   186  			// ie. Check preceding char was LF
   187              // Make up token "[SECTIONNAME]", to search for
   187  			if(posSB>0)
   188              TPtr sectionToken=iToken->Des();
   188  				{
   189              _LIT(sectionTokenFmtString,"[%S]");
   189  				// Create a Buffer, starting one char before found subBuf
   190              sectionToken.Format(sectionTokenFmtString,&aSectName);
   190  				TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
   191 
   191  				// Check first char is end of prev
   192              // Search for next occurrence of aSectName
   192  				if(CharBefore[0] == '\n')
   193              TInt posSB = SearchBuf.Find(sectionToken);
   193  					{
   194 
   194  					FoundSection = ETrue;		// found
   195              if (posSB==KErrNotFound)
   195  					posI = posI + posSB;
   196                  return(EFalse);
   196  					}
   197 
   197  				else
   198              // Check this is at beginning of line (ie. non-commented)
   198  					{
   199              // ie. Check preceding char was LF
   199  					posI = posI + posSB + 1;	// try again
   200              if(posSB>0)
   200  					}
   201                  {
   201  				}
   202                  // Create a Buffer, starting one char before found subBuf
   202  			else
   203                  TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
   203  				{
   204                  // Check first char is end of prev
   204  				FoundSection = ETrue;
   205                  if(CharBefore[0] == '\n')
   205  				}
   206                      {
   206 
   207                      FoundSection = ETrue;       // found
   207  			}	// while ( ! FoundSection ) 
   208                      posI = posI + posSB;
   208 
   209                      }
   209  		// Set start of section, after section name, (incl '[' and ']')
   210                  else
   210  		posStartOfSection = posI + aSectName.Length() + 2;
   211                      {
   211 
   212                      posI = posI + posSB + 1;    // try again
   212  		// Set end of section, by finding begin of next section or end
   213                      }
   213  		SearchBuf.Set(iPtr.Mid(posI));
   214                  }
   214  		_LIT(nextSectionBuf,"\n[");
   215              else
   215  		TInt posSB = SearchBuf.Find(nextSectionBuf);
   216                  {
   216  		if(posSB != KErrNotFound)
   217                  FoundSection = ETrue;
   217  			{
   218                  }
   218  			posEndOfSection = posI + posSB;
   219 
   219  			}
   220              }   // while ( ! FoundSection )
   220  		else
   221 
   221  			{
   222          // Set start of section, after section name, (incl '[' and ']')
   222  			posEndOfSection = iPtr.Length();
   223          posStartOfSection = posI + aSectName.Length() + 2;
   223  			}
   224 
   224 
   225          // Set end of section, by finding begin of next section or end
   225  		}	// if( aSectName.Length() > 0 )
   226          SearchBuf.Set(iPtr.Mid(posI));
   226 
   227          _LIT(nextSectionBuf,"\n[");
   227  	// Look for key in ini file data Buffer
   228          TInt posSB = SearchBuf.Find(nextSectionBuf);
   228  	posI = posStartOfSection;
   229          if(posSB != KErrNotFound)
   229  	TBool FoundKey(false);
   230              {
   230  	while ( ! FoundKey )
   231              posEndOfSection = posI + posSB;
   231  		{
   232              }
   232  		// Search for next occurrence of aKeyName
   233          else
   233  		SearchBuf.Set(iPtr.Mid(posI,posEndOfSection-posI));
   234              {
   234  		TInt posSB = SearchBuf.Find(aKeyName);
   235              posEndOfSection = iPtr.Length();
   235 
   236              }
   236  		// If not found, return
   237 
   237  		if (posSB==KErrNotFound)
   238          }   // if( aSectName.Length() > 0 )
   238  			return(EFalse);
   239 
   239 
   240      // Look for key in ini file data Buffer
   240  		// Check this is at beginning of line (ie. non-commented)
   241      posI = posStartOfSection;
   241  		// ie. Check preceding char was CR or LF
   242      TBool FoundKey(false);
   242  		if(posSB>0)
   243      while ( ! FoundKey )
   243  			{
   244          {
   244  			// Create a Buffer, starting one char before found subBuf
   245          // Search for next occurrence of aKeyName
   245  			TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
   246          SearchBuf.Set(iPtr.Mid(posI,posEndOfSection-posI));
   246  			// Check if the first char is end of prev and also check 
   247          TInt posSB = SearchBuf.Find(aKeyName);
   247  			// if the token found is not a substring of another string  
   248 
   248  			TBool beginningOK = ((CharBefore[0] == '\n') || (CharBefore[0] == ' ') || (CharBefore[0] == '\t'));
   249          // If not found, return
   249  			TBool endingOK = ((CharBefore[aKeyName.Length()+1] == '=') || (CharBefore[aKeyName.Length()+1] == ' ') || (CharBefore[aKeyName.Length()+1] == '\t'));
   250          if (posSB==KErrNotFound)
   250  			if (beginningOK && endingOK)
   251              return(EFalse);
   251  				{
   252 
   252  				FoundKey = ETrue;
   253          // Check this is at beginning of line (ie. non-commented)
   253  				posI = posI + posSB;
   254          // ie. Check preceding char was CR or LF
   254  				}
   255          if(posSB>0)
   255  			else
   256              {
   256  				{
   257              // Create a Buffer, starting one char before found subBuf
   257  				posI = posI + posSB + 1;
   258              TPtrC CharBefore(SearchBuf.Right(SearchBuf.Length()-posSB+1));
   258  				}
   259              // Check if the first char is end of prev and also check
   259  			}
   260              // if the token found is not a substring of another string
   260  		else
   261              TBool beginningOK = ((CharBefore[0] == '\n') || (CharBefore[0] == ' ') || (CharBefore[0] == '\t'));
   261  			{
   262              TBool endingOK = ((CharBefore[aKeyName.Length()+1] == '=') || (CharBefore[aKeyName.Length()+1] == ' ') ||
   262  			FoundKey = ETrue;
   263 (CharBefore[aKeyName.Length()+1] == '\t'));
   263  			}
   264              if (beginningOK && endingOK)
   264  		}	// while ( ! FoundKey )
   265                  {
   265 
   266                  FoundKey = ETrue;
   266  	// Set pos, to just after '=' sign
   267                  posI = posI + posSB;
   267  	SearchBuf.Set(iPtr.Mid(posI));
   268                  }
   268  	TInt posSB = SearchBuf.Locate('=');
   269              else
   269  	if(posSB==KErrNotFound)		// Illegal format, should flag this...
   270                  {
   270  		return(EFalse);
   271                  posI = posI + posSB + 1;
   271 
   272                  }
   272  	// Identify start and end of data (EOL or EOF)
   273              }
   273  	posI = posI + posSB + 1;	// 1 char after '='
   274          else
   274  	TInt posValStart = posI;
   275              {
   275  	TInt posValEnd;
   276              FoundKey = ETrue;
   276  	SearchBuf.Set(iPtr.Mid(posI));
   277              }
   277  	posSB = SearchBuf.Locate('\n');
   278          }   // while ( ! FoundKey )
   278  	if(posSB!=KErrNotFound)
   279 
   279  		{
   280 // Set pos, to just after '=' sign
   280       		// ini file uses LF for EOL 
   281 SearchBuf.Set(iPtr.Mid(posI));
   281              posValEnd = posI + posSB; 
   282 TInt posSB = SearchBuf.Locate('=');
   282  		}
   283 if(posSB==KErrNotFound)     // Illegal format, should flag this...
   283  	else
   284     return(EFalse);
   284  		{
   285 
   285  		posValEnd = iPtr.Length();
   286 // Identify start and end of data (EOL or EOF)
   286  		}
   287 posI = posI + posSB + 1;    // 1 char after '='
   287 
   288 TInt posValStart = posI;
   288  	// Check we are still in the section requested
   289 TInt posValEnd;
   289  	if( aSectName.Length() > 0 )
   290 SearchBuf.Set(iPtr.Mid(posI));
   290  		{
   291 posSB = SearchBuf.Locate('\r');
   291  		if( posValEnd > posEndOfSection )
   292 if(posSB!=KErrNotFound)
   292  			{
   293     {
   293  			return(EFalse);
   294     posValEnd = posI + posSB;
   294  			}
   295     }
   295  		}
   296 else
   296  	// Parse Buffer from posn of key
   297     {
   297  	// Start one space after '='
   298     posValEnd = iPtr.Length();
   298  	TLex lex(iPtr.Mid(posValStart, posValEnd-posValStart));
   299     }
   299  	lex.SkipSpaceAndMark();		// Should be at the start of the data
   300 
   300  	aResult.Set(lex.MarkedToken().Ptr(),posValEnd-posValStart - lex.Offset() );
   301 // Check we are still in the section requested
   301 
   302 if( aSectName.Length() > 0 )
   302  	TInt filterOffset;
   303     {
   303  	
   304     if( posValEnd > posEndOfSection )
   304  	// Mark the offset value to the end of the value string
   305         {
   305  	filterOffset = aResult.Length() - 1;
   306         return(EFalse);
   306 
   307         }
   307  	// Loop from end of the value string marked by the offset fetched by above process
   308     }
   308      // And check to see if there is spaces, tabs or carriage returns
   309 // Parse Buffer from posn of key
   309  	while(filterOffset >= 0 &&
   310 // Start one space after '='
   310  		(aResult[filterOffset] == '\t' ||
   311 TLex lex(iPtr.Mid(posValStart, posValEnd-posValStart));
   311           aResult[filterOffset] == ' ' ||
   312 lex.SkipSpaceAndMark();     // Should be at the start of the data
   312           aResult[filterOffset] == '\r'))
   313 aResult.Set(lex.MarkedToken().Ptr(),posValEnd-posValStart - lex.Offset() );
   313  		{
   314 return(ETrue);
   314  		// If found, reduce the offset by 1 for every space and tab during the while loop
   315 }
   315  		filterOffset--;
       
   316  		}
       
   317 
       
   318  	// The final offset value indicating end of actual value
       
   319  	// within the ini data is set to the result string reference passed in
       
   320  	aResult.Set(aResult.Mid(0,filterOffset + 1));
       
   321 
       
   322  	return(ETrue);
       
   323  	}
   316 
   324 
   317 ////////////////////////////////////////////////////////////////////////////////
   325 ////////////////////////////////////////////////////////////////////////////////
   318 TBool CIniData::FindVar(const TDesC &aSection,const TDesC &aKeyName,TInt &aResult)
   326 TBool CIniData::FindVar(const TDesC &aSection,const TDesC &aKeyName,TInt &aResult)
   319 /* Find an integer value from given aKeyName and aSecName in the ini data file
   327 /* Find an integer value from given aKeyName and aSecName in the ini data file
   320 
   328