const char *getUnits(const char *string, S32 startIndex, S32 endIndex, const char *set)
   {
      if( startIndex > endIndex )
         return "";

      S32 sz;
      S32 index = startIndex;
      while(index--)
      {
         if(!*string)
            return "";
         sz = dStrcspn(string, set);
         if (string[sz] == 0)
            return "";
         string += (sz + 1);
      }
      const char *startString = string;

      for( U32 i = startIndex; i <= endIndex && *string; i ++ )
      {
         sz = dStrcspn(string, set);
         string += sz;

         if( i < endIndex )
            string ++;
      }
      
      S32 totalSize = ( S32 )( string - startString );
      AssertWarn( totalSize + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );
      totalSize = getMin( totalSize, S32( sizeof( _returnBuffer ) - 1 ) );

      if( totalSize > 0 )
      {
         char *ret = &_returnBuffer[0];
         dStrncpy( ret, startString, totalSize );
         ret[ totalSize ] = '\0';
         
         return ret;
      }

      return "";
   }
   const char* setUnit(const char *string, U32 index, const char *replace, const char *set)
   {
      U32 sz;
      const char *start = string;

      AssertFatal( dStrlen(string) + dStrlen(replace) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );

      char *ret = &_returnBuffer[0];
      ret[0] = '\0';
      U32 padCount = 0;

      while(index--)
      {
         sz = dStrcspn(string, set);
         if(string[sz] == 0)
         {
            string += sz;
            padCount = index + 1;
            break;
         }
         else
            string += (sz + 1);
      }
      // copy first chunk
      sz = string-start;
      dStrncpy(ret, start, sz);
      for(U32 i = 0; i < padCount; i++)
         ret[sz++] = set[0];

      // replace this unit
      ret[sz] = '\0';
      dStrcat(ret, replace);

      // copy remaining chunks
      sz = dStrcspn(string, set);         // skip chunk we're replacing
      if(!sz && !string[sz])
         return ret;

      string += sz;
      dStrcat(ret, string);
      return ret;
   }
示例#3
0
// Function to return the 'index' column from 'string' given delimeters in 'set'
static const char *getColumn(const char *string, char* returnbuff, U32 index, const char *set)
{
   U32 sz;
   while(index--)
   {
      if(!*string)
         return "";
      sz = dStrcspn(string, set);
      if (string[sz] == 0)
         return "";
      string += (sz + 1);    
   }
   sz = dStrcspn(string, set);
   if (sz == 0)
      return "";
   char *ret = returnbuff;
   dStrncpy(ret, string, sz);
   ret[sz] = '\0';
   return ret;
}   
   const char* removeUnit(const char *string, U32 index, const char *set)
   {
      U32 sz;
      const char *start = string;
      AssertFatal( dStrlen(string) + 1 < sizeof( _returnBuffer ), "Size of returned string too large for return buffer" );

      char *ret = &_returnBuffer[0];
      ret[0] = '\0';

      while(index--)
      {
         sz = dStrcspn(string, set);
         // if there was no unit out there... return the original string
         if(string[sz] == 0)
            return start;
         else
            string += (sz + 1);
      }
      // copy first chunk
      sz = string-start;
      dStrncpy(ret, start, sz);
      ret[sz] = 0;

      // copy remaining chunks
      sz = dStrcspn(string, set);         // skip chunk we're removing

      if(string[sz] == 0) {               // if that was the last...
         if(string != start) {
            ret[string - start - 1] = 0;  // then kill any trailing delimiter
         }
         return ret;                      // and bail
      }

      string += sz + 1; // skip the extra field delimiter
      dStrcat(ret, string);
      return ret;
   }
   const char *getUnit(const char *string, U32 index, const char *set, char* buffer, U32 bufferSize)
   {
      if( !buffer )
      {
         buffer = _returnBuffer;
         bufferSize = sizeof( _returnBuffer );
      }

      AssertFatal( bufferSize, "StringUnit::getUnit - bufferSize cannot be zero!" );
      if( !bufferSize )
         return "";
         
      buffer[0] = 0;
      
      U32 sz;
      while(index--)
      {
         if(!*string)
            return buffer;

         sz = dStrcspn(string, set);
         if (string[sz] == 0)
            return buffer;
            
         string += (sz + 1);
      }
      sz = dStrcspn(string, set);
      if (sz == 0)
         return buffer;

      AssertWarn( sz + 1 < bufferSize, "Size of returned string too large for return buffer" );
      sz = getMin( sz, bufferSize - 1 );

      dStrncpy(buffer, string, sz);
      buffer[sz] = '\0';
      return buffer;
   }