ioJson.c 8.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
/**CFile****************************************************************

  FileName    [ioJson.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Command processing package.]

  Synopsis    [Procedures to read JSON.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: ioJson.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/

#include "ioAbc.h"
#include "misc/vec/vecWec.h"
#include "misc/util/utilNam.h"
#include "misc/extra/extra.h"

ABC_NAMESPACE_IMPL_START


////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

static inline int         Json_EntryIsName( int Fan )                     { return Abc_LitIsCompl(Fan);                                                       }
static inline char *      Json_EntryName( Abc_Nam_t * pStrs, int Fan )    { assert(Json_EntryIsName(Fan));  return Abc_NamStr( pStrs, Abc_Lit2Var(Fan) );     }
static inline Vec_Int_t * Json_EntryNode( Vec_Wec_t * vObjs, int Fan )    { assert(!Json_EntryIsName(Fan)); return Vec_WecEntry( vObjs, Abc_Lit2Var(Fan) );   }

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    [Parsing.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Json_CharIsSpace( char c )   
{ 
    return (c == ' ' || c == '\t' || c == '\r' || c == '\n');   
}
static inline char * Json_SkipSpaces( char * pCur )
{
    while ( Json_CharIsSpace(*pCur) )
        pCur++;    
    return pCur;
}
static inline char * Json_SkipNonSpaces( char * pCur )
{
    while ( !Json_CharIsSpace(*pCur) )
        pCur++;    
    return pCur;
}
static inline int Json_TokenCompare( char * pCur, char * pNext, char ** ppCur2, char ** ppNext2 )
{
//    int i;
    if ( *pCur == '\"' )
        pCur++;
    if ( *(pNext-1) == ',' )
        pNext--;
    if ( *(pNext-1) == '\"' )
        pNext--;
    *ppCur2 = pCur;
    *ppNext2 = pNext;
//    for ( i = 1; i < JSON_NUM_LINES; i++ )
//        if ( !strncmp( s_Types[i].pName, pCur, pNext - pCur ) )
//            return i;
    return 0;
}

/**Function*************************************************************

  Synopsis    [Writes JSON into a file.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Json_Write_rec( FILE * pFile, Abc_Nam_t * pStr, Vec_Wec_t * vObjs, Vec_Int_t * vArray, int Level, int fAddComma, int fSpaces )
{
    int i, Entry1, Entry2, fComma;
    if ( Vec_IntEntry(vArray, 0) ) // array
    {
        if ( Vec_IntSize(vArray) == 1 )
            fprintf( pFile, "[]" );
        else if ( Vec_IntSize(vArray) == 2 && Json_EntryIsName(Vec_IntEntry(vArray,1)) )
            fprintf( pFile, "[ \"%s\" ]", Json_EntryName(pStr, Vec_IntEntry(vArray,1)) );
        else
        {
            if ( fSpaces )
                fprintf( pFile, "%*s", 3*(Level-1), "" );
            fprintf( pFile, "[\n" );
            Vec_IntForEachEntryStart( vArray, Entry1, i, 1 )
            {
                fComma = (i < Vec_IntSize(vArray) - 1);
                if ( Json_EntryIsName(Entry1) )
                    fprintf( pFile, "%*s\"%s\"%s\n", 3*Level, "", Json_EntryName(pStr, Entry1), fComma ? ",":"" );
                else
                    Json_Write_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry1), Level+1, fComma, 1 );
            }
            fprintf( pFile, "%*s]", 3*(Level-1), "" );
        }
        fprintf( pFile, "%s\n", fAddComma ? ",":"" );
    }
    else // list of pairs
    {
        if ( fSpaces )
            fprintf( pFile, "%*s", 3*(Level-1), "" );
        fprintf( pFile, "{\n" );
        assert( Vec_IntSize(vArray) % 2 != 0 );
        Vec_IntForEachEntryDoubleStart( vArray, Entry1, Entry2, i, 1 )
        {
            fComma = (i < Vec_IntSize(vArray) - 3);
            if ( Json_EntryIsName(Entry1) )
                fprintf( pFile, "%*s\"%s\"", 3*Level, "", Json_EntryName(pStr, Entry1) );
            else
                Json_Write_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry1), Level+1, 0, 1 );
            fprintf( pFile, " : " );
            if ( Json_EntryIsName(Entry2) )
                fprintf( pFile, "\"%s\"%s\n", Json_EntryName(pStr, Entry2), fComma ? ",":"" );
            else
                Json_Write_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), Level+1, fComma, 0 );
        }
        fprintf( pFile, "%*s}%s\n", 3*(Level-1), "", fAddComma ? ",":"" );
    }
}
void Json_Write( char * pFileName, Abc_Nam_t * pStr, Vec_Wec_t * vObjs )
{
    FILE * pFile = fopen( pFileName, "wb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" for writing.\n", pFileName );
        return;
    }
    Json_Write_rec( pFile, pStr, vObjs, Vec_WecEntry(vObjs, 0), 1, 0, 1 );
    fclose( pFile );
}

/**Function*************************************************************

  Synopsis    [Reads JSON from a file.]

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
Vec_Wec_t * Json_Read( char * pFileName, Abc_Nam_t ** ppStrs )
{
    Abc_Nam_t * pStrs; 
    Vec_Wec_t * vObjs; 
    Vec_Int_t * vStack, * vTemp;
    char * pContents, * pCur, * pNext, * pCur2, * pNext2;
    int nFileSize, RetValue, iToken;
    FILE * pFile;

    // read the file into the buffer
    pFile = fopen( pFileName, "rb" );
    if ( pFile == NULL )
    {
        printf( "Cannot open file \"%s\" for reading.\n", pFileName );
        return NULL;
    }
    nFileSize = Extra_FileSize( pFileName );
    pContents = pCur = ABC_ALLOC( char, nFileSize+1 );
    RetValue = fread( pContents, nFileSize, 1, pFile );
    pContents[nFileSize] = 0;
    fclose( pFile );

    // start data-structures
    vObjs  = Vec_WecAlloc( 1000 );
    vStack = Vec_IntAlloc( 100 );
    pStrs  = Abc_NamStart( 1000, 24 );
    //Json_AddTypes( pStrs );

    // read lines
    assert( Vec_WecSize(vObjs) == 0 );
    assert( Vec_IntSize(vStack) == 0 );
    while ( pCur < pContents + nFileSize )
    {
        pCur  = Json_SkipSpaces( pCur );
        if ( *pCur == '\0' )
            break;
        pNext = Json_SkipNonSpaces( pCur );
        if ( *pCur == '{' || *pCur == '[' )
        {
            // add fanin to node on the previous level
            if ( Vec_IntSize(vStack) > 0 )
                Vec_IntPush( Vec_WecEntry(vObjs, Vec_IntEntryLast(vStack)), Abc_Var2Lit(Vec_WecSize(vObjs), 0) );            
            // add node to the stack
            Vec_IntPush( vStack, Vec_WecSize(vObjs) );
            vTemp = Vec_WecPushLevel( vObjs );
            Vec_IntGrow( vTemp, 4 );
            // remember it as an array
            Vec_IntPush( vTemp, (int)(*pCur == '[') );
            pCur++;
            continue;
        }
        if ( *pCur == '}' || *pCur == ']' )
        {
            Vec_IntPop( vStack );
            pCur++;
            continue;
        }
        if ( *pCur == ',' || *pCur == ':' )
        {
            pCur++;
            continue;
        }
        iToken = Json_TokenCompare( pCur, pNext, &pCur2, &pNext2 );
        if ( iToken == 0 )
            iToken = Abc_NamStrFindOrAddLim( pStrs, pCur2, pNext2, NULL );
        Vec_IntPush( Vec_WecEntry(vObjs, Vec_IntEntryLast(vStack)), Abc_Var2Lit(iToken, 1) );
        pCur = pNext;
    }
    Vec_IntFree( vStack );
    ABC_FREE( pContents );
    *ppStrs = pStrs;
    return vObjs;
}

/**Function*************************************************************

  Synopsis    []

  Description []
  
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Json_ReadTest( char * pFileName )
{
    Abc_Nam_t * pStrs;
    Vec_Wec_t * vObjs;
    vObjs = Json_Read( pFileName, &pStrs );
    if ( vObjs == NULL )
        return;
    Json_Write( "test.json", pStrs, vObjs );
    Abc_NamDeref( pStrs );
    Vec_WecFree( vObjs );
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


ABC_NAMESPACE_IMPL_END