static void hb_inkeySetTextKeys( const char * pszText, HB_SIZE nSize, HB_BOOL fInsert ) { PHB_CODEPAGE cdp = hb_vmCDP(); HB_SIZE nIndex = 0; HB_WCHAR wc; if( fInsert ) { HB_WCHAR buffer[ 32 ], * keys; HB_SIZE n = 0; keys = nSize <= HB_SIZEOFARRAY( buffer ) ? buffer : ( HB_WCHAR * ) hb_xgrab( nSize * sizeof( HB_WCHAR ) ); while( HB_CDPCHAR_GET( cdp, pszText, nSize, &nIndex, &wc ) ) keys[ n++ ] = wc; while( n-- ) { int iKey = keys[ n ] >= 128 ? HB_INKEY_NEW_UNICODE( keys[ n ] ) : keys[ n ]; hb_inkeyIns( iKey ); } if( nSize > HB_SIZEOFARRAY( buffer ) ) hb_xfree( keys ); } else { while( HB_CDPCHAR_GET( cdp, pszText, nSize, &nIndex, &wc ) ) { int iKey = wc >= 128 ? HB_INKEY_NEW_UNICODE( wc ) : wc; hb_inkeyPut( iKey ); } } }
static HB_CDP_CMP_FUNC( UTF8_cmpi ) { int iRet = 0; #ifdef HB_UTF8EX_SORT HB_SIZE nPos1 = 0, nPos2 = 0; HB_WCHAR wc1, wc2; for( ;; ) { if( ! HB_CDPCHAR_GET( cdp, szSecond, nLenSecond, &nPos2, &wc2 ) ) { if( fExact && HB_CDPCHAR_GET( cdp, szFirst, nLenFirst, &nPos1, &wc1 ) ) iRet = 1; break; } if( ! HB_CDPCHAR_GET( cdp, szFirst, nLenFirst, &nPos1, &wc1 ) ) { iRet = -1; break; } if( wc1 != wc2 ) { HB_USHORT us1 = s_uniSort[ HB_CDPCHAR_UPPER( cdp, wc1 ) ], us2 = s_uniSort[ HB_CDPCHAR_UPPER( cdp, wc2 ) ]; if( us1 != us2 ) { iRet = us1 < us2 ? -1 : 1; break; } } } #else HB_SIZE nLen = nLenFirst < nLenSecond ? nLenFirst : nLenSecond; while( nLen-- ) { HB_UCHAR u1 = cdp->upper[ ( HB_UCHAR ) *szFirst++ ], u2 = cdp->upper[ ( HB_UCHAR ) *szSecond++ ]; if( u1 != u2 ) { iRet = ( u1 < u2 ) ? -1 : 1; break; } } if( iRet == 0 ) { if( nLenSecond > nLenFirst ) iRet = -1; else if( fExact && nLenSecond < nLenFirst ) iRet = 1; } #endif return iRet; }
static HB_CDP_CMP_FUNC( UTF8_cmp ) { int iRet; #ifdef HB_UTF8EX_SORT HB_SIZE nPos1 = 0, nPos2 = 0; HB_WCHAR wc1, wc2; iRet = 0; for( ;; ) { if( ! HB_CDPCHAR_GET( cdp, szSecond, nLenSecond, &nPos2, &wc2 ) ) { if( fExact && HB_CDPCHAR_GET( cdp, szFirst, nLenFirst, &nPos1, &wc1 ) ) iRet = 1; break; } if( ! HB_CDPCHAR_GET( cdp, szFirst, nLenFirst, &nPos1, &wc1 ) ) { iRet = -1; break; } if( wc1 != wc2 ) { HB_USHORT us1 = s_uniSort[ wc1 ], us2 = s_uniSort[ wc2 ]; if( us1 != us2 ) { iRet = us1 < us2 ? -1 : 1; break; } } } #else HB_SIZE nLen = nLenFirst < nLenSecond ? nLenFirst : nLenSecond; HB_SYMBOL_UNUSED( cdp ); iRet = memcmp( szFirst, szSecond, nLen ); if( iRet == 0 ) { if( nLenSecond > nLenFirst ) iRet = -1; else if( fExact && nLenSecond < nLenFirst ) iRet = 1; } else if( iRet > 0 ) iRet = 1; else iRet = -1; #endif return iRet; }
static char * hb_strHardCR( char * pszString, HB_SIZE nStringLen ) { HB_SIZE nStringPos; PHB_CODEPAGE cdp; HB_TRACE( HB_TR_DEBUG, ( "hb_strHardCR(%s, %" HB_PFS "u)", pszString, nStringLen ) ); cdp = hb_vmCDP(); if( HB_CDP_ISCUSTOM( cdp ) ) { HB_WCHAR wc; nStringPos = 0; while( nStringPos < nStringLen ) { if( pszString[ nStringPos ] == HB_CHAR_SOFT1 && pszString[ nStringPos + 1 ] == HB_CHAR_SOFT2 ) { pszString[ nStringPos ] = HB_CHAR_HARD1; nStringPos += 2; } else if( ! HB_CDPCHAR_GET( cdp, pszString, nStringLen, &nStringPos, &wc ) ) break; } } else { for( nStringPos = 0; nStringPos < nStringLen; nStringPos++ ) { if( pszString[ nStringPos ] == HB_CHAR_SOFT1 && pszString[ nStringPos + 1 ] == HB_CHAR_SOFT2 ) { pszString[ nStringPos++ ] = HB_CHAR_HARD1; } } } return pszString; }
static HB_SIZE hb_mlGetLine( PHB_MLC_INFO pMLC ) { HB_SIZE nBlankCol = 0, nBlankPos = 0, nLastCol = 0, nLastPos; int i; pMLC->nCol = 0; if( pMLC->nOffset >= pMLC->nLen ) return HB_FALSE; while( pMLC->nOffset < pMLC->nLen ) { HB_WCHAR ch; if( pMLC->pszString[ pMLC->nOffset ] == HB_CHAR_SOFT1 && pMLC->pszString[ pMLC->nOffset + 1 ] == HB_CHAR_SOFT2 ) { if( pMLC->nMaxCol && pMLC->nCol ) break; if( pMLC->nOffset < pMLC->nMaxPos ) nLastCol = pMLC->nCol; pMLC->nOffset += 2; if( ! pMLC->fWordWrap ) break; else if( nBlankPos + 2 == pMLC->nOffset ) nBlankPos += 2; continue; } i = hb_mlEol( pMLC ); if( i >= 0 ) { if( pMLC->nOffset < pMLC->nMaxPos ) nLastCol = pMLC->nCol; if( pMLC->nMaxCol == 0 ) pMLC->nOffset += pMLC->pEOLs[ i ].nLen; break; } else if( ! pMLC->fWordWrap && pMLC->nCol >= pMLC->nLineLength ) break; nLastPos = pMLC->nOffset; if( pMLC->cdp ) { if( ! HB_CDPCHAR_GET( pMLC->cdp, pMLC->pszString, pMLC->nLen, &pMLC->nOffset, &ch ) ) break; } else ch = pMLC->pszString[ pMLC->nOffset++ ]; if( pMLC->nOffset <= pMLC->nMaxPos ) nLastCol = pMLC->nCol; pMLC->nCol += ch == HB_CHAR_HT ? pMLC->nTabSize - ( pMLC->nCol % pMLC->nTabSize ) : 1; if( pMLC->nMaxCol && pMLC->nCol >= pMLC->nMaxCol ) { if( pMLC->nCol > pMLC->nMaxCol ) pMLC->nOffset = nLastPos; break; } else if( pMLC->nCol > pMLC->nLineLength ) { if( pMLC->fWordWrap ) { if( ch == ' ' || ch == HB_CHAR_HT ) break; else if( nBlankCol != 0 ) { pMLC->nCol = nBlankCol; pMLC->nOffset = nBlankPos; } else pMLC->nOffset = nLastPos; } else pMLC->nOffset = nLastPos; break; } if( pMLC->nCol > 1 && ( ch == ' ' || ch == HB_CHAR_HT ) ) { nBlankCol = pMLC->nCol; nBlankPos = pMLC->nOffset; } } if( pMLC->nMaxPos && pMLC->nCol > nLastCol ) pMLC->nCol = nLastCol; else if( pMLC->nCol > pMLC->nLineLength ) pMLC->nCol = pMLC->nLineLength; return HB_TRUE; }
static void _hb_jsonEncode( PHB_ITEM pValue, PHB_JSON_ENCODE_CTX pCtx, HB_SIZE nLevel, HB_BOOL fEOL, PHB_CODEPAGE cdp ) { /* Protection against recursive structures */ if( ( HB_IS_ARRAY( pValue ) || HB_IS_HASH( pValue ) ) && hb_itemSize( pValue ) > 0 ) { void * id = HB_IS_HASH( pValue ) ? hb_hashId( pValue ) : hb_arrayId( pValue ); HB_SIZE nIndex; for( nIndex = 0; nIndex < nLevel; nIndex++ ) { if( pCtx->pId[ nIndex ] == id ) { if( ! fEOL && pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "null", 4 ); return; } } if( nLevel >= pCtx->nAllocId ) { pCtx->nAllocId += 8; pCtx->pId = ( void ** ) hb_xrealloc( pCtx->pId, sizeof( void * ) * pCtx->nAllocId ); } pCtx->pId[ nLevel ] = id; } if( fEOL ) { --pCtx->pHead; _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); } if( HB_IS_STRING( pValue ) ) { HB_SIZE nPos, nLen = hb_itemGetCLen( pValue ); const char * szString = hb_itemGetCPtr( pValue ); char buf[ 8 ]; _hb_jsonCtxAdd( pCtx, "\"", 1 ); if( cdp ) { HB_WCHAR wc; nPos = 0; while( HB_CDPCHAR_GET( cdp, szString, nLen, &nPos, &wc ) ) { if( wc >= ' ' && wc < 0x7F && wc != '\\' && wc != '\"' ) { buf[ 0 ] = ( char ) wc; _hb_jsonCtxAdd( pCtx, buf, 1 ); continue; } switch( wc ) { case '\\': _hb_jsonCtxAdd( pCtx, "\\\\", 2 ); break; case '\"': _hb_jsonCtxAdd( pCtx, "\\\"", 2 ); break; case '\b': _hb_jsonCtxAdd( pCtx, "\\b", 2 ); break; case '\f': _hb_jsonCtxAdd( pCtx, "\\f", 2 ); break; case '\n': _hb_jsonCtxAdd( pCtx, "\\n", 2 ); break; case '\r': _hb_jsonCtxAdd( pCtx, "\\r", 2 ); break; case '\t': _hb_jsonCtxAdd( pCtx, "\\t", 2 ); break; default: hb_snprintf( buf, sizeof( buf ), "\\u%04X", wc ); _hb_jsonCtxAdd( pCtx, buf, 6 ); break; } } } else { nPos = 0; while( nPos < nLen ) { unsigned char uch = szString[ nPos ]; HB_SIZE nPos2 = nPos; while( uch >= ' ' && uch != '\\' && uch != '\"' ) uch = szString[ ++nPos2 ]; if( nPos2 > nPos ) { _hb_jsonCtxAdd( pCtx, szString + nPos, nPos2 - nPos ); if( nPos2 >= nLen ) break; nPos = nPos2; } switch( uch ) { case '\\': _hb_jsonCtxAdd( pCtx, "\\\\", 2 ); break; case '\"': _hb_jsonCtxAdd( pCtx, "\\\"", 2 ); break; case '\b': _hb_jsonCtxAdd( pCtx, "\\b", 2 ); break; case '\f': _hb_jsonCtxAdd( pCtx, "\\f", 2 ); break; case '\n': _hb_jsonCtxAdd( pCtx, "\\n", 2 ); break; case '\r': _hb_jsonCtxAdd( pCtx, "\\r", 2 ); break; case '\t': _hb_jsonCtxAdd( pCtx, "\\t", 2 ); break; default: hb_snprintf( buf, sizeof( buf ), "\\u00%02X", uch ); _hb_jsonCtxAdd( pCtx, buf, 6 ); break; } nPos++; } } _hb_jsonCtxAdd( pCtx, "\"", 1 ); } else if( HB_IS_NUMINT( pValue ) ) { char buf[ 24 ]; HB_MAXINT nVal = hb_itemGetNInt( pValue ); HB_BOOL fNeg = nVal < 0; int i = 0; if( fNeg ) nVal = -nVal; do buf[ sizeof( buf ) - ++i ] = ( nVal % 10 ) + '0'; while( ( nVal /= 10 ) != 0 ); if( fNeg ) buf[ sizeof( buf ) - ++i ] = '-'; _hb_jsonCtxAdd( pCtx, &buf[ sizeof( buf ) - i ], i ); } else if( HB_IS_NUMERIC( pValue ) ) { char buf[ 64 ]; int iDec; double dblValue = hb_itemGetNDDec( pValue, &iDec ); hb_snprintf( buf, sizeof( buf ), "%.*f", iDec, dblValue ); _hb_jsonCtxAdd( pCtx, buf, strlen( buf ) ); } else if( HB_IS_NIL( pValue ) ) { _hb_jsonCtxAdd( pCtx, "null", 4 ); } else if( HB_IS_LOGICAL( pValue ) ) { if( hb_itemGetL( pValue ) ) _hb_jsonCtxAdd( pCtx, "true", 4 ); else _hb_jsonCtxAdd( pCtx, "false", 5 ); } else if( HB_IS_DATE( pValue ) ) { char szBuffer[ 10 ]; hb_itemGetDS( pValue, szBuffer + 1 ); szBuffer[ 0 ] = '\"'; szBuffer[ 9 ] = '\"'; _hb_jsonCtxAdd( pCtx, szBuffer, 10 ); } else if( HB_IS_TIMESTAMP( pValue ) ) { char szBuffer[ 19 ]; hb_itemGetTS( pValue, szBuffer + 1 ); szBuffer[ 0 ] = '\"'; szBuffer[ 18 ] = '\"'; _hb_jsonCtxAdd( pCtx, szBuffer, 19 ); } else if( HB_IS_ARRAY( pValue ) ) { HB_SIZE nLen = hb_itemSize( pValue ); if( nLen ) { HB_SIZE nIndex; if( pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "[", 1 ); for( nIndex = 1; nIndex <= nLen; nIndex++ ) { PHB_ITEM pItem = hb_arrayGetItemPtr( pValue, nIndex ); if( nIndex > 1 ) _hb_jsonCtxAdd( pCtx, ",", 1 ); if( pCtx->fHuman ) _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); if( pCtx->fHuman && ! ( ( HB_IS_ARRAY( pItem ) || HB_IS_HASH( pItem ) ) && hb_itemSize( pItem ) > 0 ) ) _hb_jsonCtxAddIndent( pCtx, ( nLevel + 1 ) * INDENT_SIZE ); _hb_jsonEncode( pItem, pCtx, nLevel + 1, HB_FALSE, cdp ); } if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); } _hb_jsonCtxAdd( pCtx, "]", 1 ); } else _hb_jsonCtxAdd( pCtx, "[]", 2 ); } else if( HB_IS_HASH( pValue ) ) { HB_SIZE nLen = hb_hashLen( pValue ); if( nLen ) { HB_SIZE nIndex; if( pCtx->fHuman ) _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); _hb_jsonCtxAdd( pCtx, "{", 1 ); for( nIndex = 1; nIndex <= nLen; nIndex++ ) { PHB_ITEM pKey = hb_hashGetKeyAt( pValue, nIndex ); if( HB_IS_STRING( pKey ) ) { PHB_ITEM pItem = hb_hashGetValueAt( pValue, nIndex ); if( nIndex > 1 ) _hb_jsonCtxAdd( pCtx, ",", 1 ); if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, ( nLevel + 1 ) * INDENT_SIZE ); } _hb_jsonEncode( pKey, pCtx, nLevel + 1, HB_FALSE, cdp ); if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, ": ", 2 ); fEOL = ( HB_IS_ARRAY( pItem ) || HB_IS_HASH( pItem ) ) && hb_itemSize( pItem ) > 0; } else { _hb_jsonCtxAdd( pCtx, ":", 1 ); fEOL = HB_FALSE; } _hb_jsonEncode( pItem, pCtx, nLevel + 1, fEOL, cdp ); } } if( pCtx->fHuman ) { _hb_jsonCtxAdd( pCtx, pCtx->szEol, pCtx->iEolLen ); _hb_jsonCtxAddIndent( pCtx, nLevel * INDENT_SIZE ); } _hb_jsonCtxAdd( pCtx, "}", 1 ); } else _hb_jsonCtxAdd( pCtx, "{}", 2 ); } else { /* All unsupported types are replacd by null */ _hb_jsonCtxAdd( pCtx, "null", 4 ); } }