예제 #1
0
void convertFloat(T& n, const std::string& str, const char* typeto)
{
    bool ok = false;
    std::string::const_iterator r = getFloat(str.begin(), str.end(), ok, n);

    if (ok)
        _skipws(r, str.end());

    if(r != str.end() || ! ok)
        ConversionError::doThrow(typeto, "string", str.c_str());
}
예제 #2
0
void convertFloat(T& n, const String& str, const char* typeto)
{
    bool ok = false;
    String::const_iterator r = getFloat(str.begin(), str.end(), ok, n, FloatFormat<Char>() );

    if (ok)
        _skipws(r, str.end());

    if(r != str.end() || ! ok)
        ConversionError::doThrow(typeto, "String", str.narrow().c_str());
}
예제 #3
0
파일: hbjson.c 프로젝트: AugustoAbacus/core
HB_SIZE hb_jsonDecode( const char * szSource, PHB_ITEM pValue )
{
   PHB_ITEM pItem = pValue ? pValue : hb_itemNew( NULL );
   const char * sz;

   sz = szSource ? _hb_jsonDecode( _skipws( szSource ), pItem ) : NULL;
   if( ! pValue )
      hb_itemRelease( pItem );
   if( sz )
      return sz - szSource;
   return 0;
}
예제 #4
0
void convertFloat(T& n, const char* str, const char* typeto)
{
    bool ok = false;
    nullterm_array_iterator<char> it(str);
    nullterm_array_iterator<char> end;
    it = getFloat( it, end, ok, n );

    if (ok)
        _skipws(it, end);

    if( it != end || ! ok )
        ConversionError::doThrow(typeto, "char*", str);
}
예제 #5
0
파일: hbjson.c 프로젝트: AugustoAbacus/core
static const char * _hb_jsonDecode( const char * szSource, PHB_ITEM pValue )
{
   if( *szSource == '\"' )
   {
      char * szDest, * szHead;
      HB_SIZE nAlloc = 16;

      szHead = szDest = ( char * ) hb_xgrab( nAlloc );
      szSource++;
      while( *szSource != '\"' )
      {
         if( szHead + 6 >= szDest + nAlloc )
         {
            HB_SIZE nLen = szHead - szDest;
            nAlloc += nAlloc << 1;
            szDest = ( char * ) hb_xrealloc( szDest, nAlloc );
            szHead = szDest + nLen;
         }
         if( *szSource == '\\' )
         {
            szSource++;
            switch( *szSource )
            {
               case '\"':
                  *szHead++ = '\"';
                  break;
               case '\\':
                  *szHead++ = '\\';
                  break;
               case '/':
                  *szHead++ = '/';
                  break;
               case 'b':
                  *szHead++ = '\b';
                  break;
               case 'f':
                  *szHead++ = '\f';
                  break;
               case 'n':
                  *szHead++ = '\n';
                  break;
               case 'r':
                  *szHead++ = '\r';
                  break;
               case 't':
                  *szHead++ = '\t';
                  break;
               case 'u':
               {
                  HB_WCHAR wc = 0;
                  int i;

                  for( i = 0; i < 4; i++ )
                  {
                     char c = *++szSource;
                     wc <<= 4;
                     if( c >= '0' && c <= '9' )
                        wc += c - '0';
                     else if( c >= 'A' && c <= 'F' )
                        wc += c - 'A' + 10;
                     else if( c >= 'a' && c <= 'f' )
                        wc += c - 'a' + 10;
                     else
                     {
                        hb_xfree( szDest );
                        return NULL;
                     }
                  }
                  szHead += hb_cdpU16ToStr( hb_vmCDP(), HB_CDP_ENDIAN_NATIVE,
                                            &wc, 1,
                                            szHead, szDest + nAlloc - szHead );
                  break;
               }
               default:
                  hb_xfree( szDest );
                  return NULL;
            }
            szSource++;
         }
         else if( *( const unsigned char * ) szSource >= ' ' )
            *szHead++ = *szSource++;
         else
         {
            hb_xfree( szDest );
            return NULL;
         }
      }
      hb_itemPutCL( pValue, szDest, szHead - szDest );
      hb_xfree( szDest );
      return szSource + 1;
   }
   else if( *szSource == '-' || ( *szSource >= '0' && *szSource <= '9' ) )
   {
      /* NOTE: this function is much less strict to number format than
               JSON syntax definition. This is allowed behaviour [Mindaugas] */
      HB_MAXINT nValue = 0;
      double dblValue = 0;
      HB_BOOL fNeg, fDbl = HB_FALSE;
      int iDec = 0;

      fNeg = *szSource == '-';
      if( fNeg )
         szSource++;

      while( *szSource >= '0' && *szSource <= '9' )
      {
         nValue = nValue * 10 + *szSource - '0';
         szSource++;
      }
      if( *szSource == '.' )
      {
         double mult = 1;

         dblValue = ( double ) nValue;
         fDbl = HB_TRUE;
         szSource++;
         while( *szSource >= '0' && *szSource <= '9' )
         {
            mult /= 10;
            dblValue += ( ( double ) ( *szSource - '0' ) ) * mult;
            szSource++;
            iDec++;
         }
      }
      if( *szSource == 'e' || *szSource == 'E' )
      {
         HB_BOOL fNegExp;
         int iExp = 0;

         szSource++;
         fNegExp = *szSource == '-';
         if( fNegExp )
            szSource++;

         while( *szSource >= '0' && *szSource <= '9' )
         {
            iExp = iExp * 10 + *szSource - '0';
            szSource++;
         }
         if( ! fDbl )
         {
            dblValue = ( double ) nValue;
            fDbl = HB_TRUE;
         }
         if( fNegExp )
            iDec += iExp;
         dblValue = hb_numExpConv( dblValue, fNegExp ? iExp : -iExp );
      }

      if( fDbl )
         hb_itemPutNDDec( pValue, hb_numRound( fNeg ? -dblValue : dblValue, iDec ), iDec );
      else
         hb_itemPutNInt( pValue, fNeg ? -nValue : nValue );
      return szSource;
   }
   else if( ! strncmp( szSource, "null", 4 ) )
   {
      hb_itemClear( pValue );
      return szSource + 4;
   }
   else if( ! strncmp( szSource, "true", 4 ) )
   {
      hb_itemPutL( pValue, HB_TRUE );
      return szSource + 4;
   }
   else if( ! strncmp( szSource, "false", 5 ) )
   {
      hb_itemPutL( pValue, HB_FALSE );
      return szSource + 5;
   }
   else if( *szSource == '[' )
   {
      hb_arrayNew( pValue, 0 );
      szSource = _skipws( szSource + 1 );
      if( *szSource != ']' )
      {
         PHB_ITEM pItem = hb_itemNew( NULL );

         for( ;; )
         {
            szSource = _hb_jsonDecode( szSource, pItem );
            if( ! szSource )
            {
               hb_itemRelease( pItem );
               return NULL;
            }
            hb_arrayAddForward( pValue, pItem );

            szSource = _skipws( szSource );
            if( *szSource == ',' )
            {
               szSource = _skipws( szSource + 1 );
               continue;
            }
            else if( *szSource == ']' )
               break;
            else
            {
               hb_itemRelease( pItem );
               return NULL;
            }
         }
         hb_itemRelease( pItem );
      }
      return szSource + 1;
   }
   else if( *szSource == '{' )
   {
      hb_hashNew( pValue );
      szSource = _skipws( szSource + 1 );
      if( *szSource != '}' )
      {
         PHB_ITEM pItemKey = hb_itemNew( NULL );
         PHB_ITEM pItemValue = hb_itemNew( NULL );

         for( ;; )
         {
            /* Do we need to check if key does not exist yet? */
            if( ( szSource = _hb_jsonDecode( szSource, pItemKey ) ) == NULL ||
                ! HB_IS_STRING( pItemKey ) ||
                * ( szSource = _skipws( szSource ) ) != ':' ||
                ( szSource = _hb_jsonDecode( _skipws( szSource + 1 ), pItemValue ) ) == NULL)
            {
               hb_itemRelease( pItemKey );
               hb_itemRelease( pItemValue );
               return NULL;
            }

            hb_hashAdd( pValue, pItemKey, pItemValue );
            szSource = _skipws( szSource );
            if( *szSource == ',' )
            {
               szSource = _skipws( szSource + 1 );
               continue;
            }
            else if( *szSource == '}' )
               break;
            else
            {
               hb_itemRelease( pItemKey );
               hb_itemRelease( pItemValue );
               return NULL;
            }
         }
         hb_itemRelease( pItemKey );
         hb_itemRelease( pItemValue );
      }
      return szSource + 1;
   }
   return NULL;
}
예제 #6
0
int ParsedJson::_rec_parse(int v, int cur) {
    cur = _skipws(cur);
    if(cur >= _json.length()) return cur;
    _values[v].start = cur;
    if(_json[cur] == '{') {
        cur++;
        cur = _skipws(cur);
        while(_json[cur] != '}') {
            cur = _skipws(cur);
            if(_json[cur] != '\"') _parse_error(cur, "string expected");
            auto n = _add_value();
            _add_child(v, n);
            cur = _rec_parse(n,cur);
            cur = _skipws(cur);
            if(_json[cur] != ':') _parse_error(cur, ": expected");
            cur++;
            cur = _skipws(cur);
            auto m = _add_value();
            _add_child(v, m);
            cur = _rec_parse(m,cur);
            cur = _skipws(cur);
            if(_json[cur] != '}' and _json[cur] != ',') _parse_error(cur,"} or , expected");
            if(_json[cur] == ',') cur++;
            cur = _skipws(cur);            
        }
        _values[v].end = cur;
    } else if(_json[cur] == '[') {
        cur++;
        cur = _skipws(cur);
        while(_json[cur] != ']') {
            cur = _skipws(cur);
            auto m = _add_value();
            _add_child(v, m);
            cur = _rec_parse(m,cur);
            cur = _skipws(cur);
            if(_json[cur] != ']' and _json[cur] != ',') _parse_error(cur,"] or , expected");
            if(_json[cur] == ',') cur++;
            cur = _skipws(cur);
        }
        cur = _skipws(cur);
    } else if(_json[cur] == 't') {
        _check(cur,"true",4);
        cur += 3;
    } else if(_json[cur] == 'f') {
        _check(cur,"false",5);
        cur += 4;
    } else if(_json[cur] == 'n') {
        _check(cur,"null",4);
        cur += 3;
    } else if(_json[cur] == '"') {
        cur++;
        while(_json[cur] != '"') {
            if(_json[cur] == '\\') {
                cur++;
                if(_json[cur] == 'u') {
                    _parse_error(cur, "unsupported unicode strings");
                } else if(_json[cur] != '"' and _json[cur] != '\\' and _json[cur] != '/' and _json[cur] != 't' and
                   _json[cur] != 'n' and _json[cur] != 'r' and _json[cur] != 'b' and _json[cur] != 'f') {
                     _parse_error(cur, "unsupported string escapes");
                } else {
                    _parse_error(cur, "unknown string escape");
                }
            }
            cur++;
        }
    } else if(_json[cur] == '+' or _json[cur] == '-' or isdigit(_json[cur])) {
        if(_json[cur] == '+' or _json[cur] == '-') cur++;
        while(isdigit(_json[cur])) cur++;
        if(_json[cur] == '.') {
            cur++;
            while(isdigit(_json[cur])) cur++;
        }
        if(_json[cur] == 'e' or _json[cur] == 'E') {
            cur++;
            if(_json[cur] == '+' or _json[cur] == '-') cur++;
            while(isdigit(_json[cur])) cur++;
        }
        cur--;
    } else _parse_error(cur, "unknown token type");
    _values[v].end = cur;
    cur++;
    cur = _skipws(cur);
    return cur;
}