33static inline int Json_EntryIsName(
int Fan ) {
return Abc_LitIsCompl(Fan); }
34static inline char * Json_EntryName(
Abc_Nam_t * pStrs,
int Fan ) {
assert(Json_EntryIsName(Fan));
return Abc_NamStr( pStrs, Abc_Lit2Var(Fan) ); }
35static inline Vec_Int_t * Json_EntryNode(
Vec_Wec_t * vObjs,
int Fan ) {
assert(!Json_EntryIsName(Fan));
return Vec_WecEntry( vObjs, Abc_Lit2Var(Fan) ); }
54 if ( !
strcmp(pStr,
"InputLayer") )
56 if ( !
strcmp(pStr,
"Conv2D") )
58 if ( !
strcmp(pStr,
"BatchNormalization") )
60 if ( !
strcmp(pStr,
"Activation") )
62 if ( !
strcmp(pStr,
"Add") )
64 if ( !
strcmp(pStr,
"MaxPooling2D") )
66 if ( !
strcmp(pStr,
"GlobalAveragePooling2D") )
68 if ( !
strcmp(pStr,
"Dense") )
70 if ( !
strcmp(pStr,
"ZeroPadding2D") )
79 int i, Entry1, Entry2;
80 if ( Vec_IntEntry(vArray, 0) )
82 if ( Vec_IntSize(vArray) == 1 )
84 if ( Vec_IntSize(vArray) == 2 && Json_EntryIsName(Vec_IntEntry(vArray,1)) )
87 fprintf( pFile,
"%s", Json_EntryName(pStr, Vec_IntEntry(vArray,1)) );
94 if ( Json_EntryIsName(Entry1) )
96 int Digit = Json_EntryName(pStr, Entry1)[0];
97 if ( fWrite && Digit !=
'0' )
98 fprintf( pFile,
"%s%s", Json_EntryName(pStr, Entry1), Digit >=
'0' && Digit <=
'9' ?
"" :
" " );
101 Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry1), fWrite, pCount );
109 assert( Vec_IntSize(vArray) % 2 != 0 );
112 char * pName1 = Json_EntryIsName(Entry1) ? Json_EntryName(pStr, Entry1) : NULL;
113 char * pName2 = Json_EntryIsName(Entry2) ? Json_EntryName(pStr, Entry2) : NULL;
115 if ( pName1 == NULL )
117 if ( !
strcmp(pName1,
"class_name") )
120 fprintf( pFile,
"\n%3d : %-8s ", (*pCount)++, pName3 );
122 else if ( !
strcmp(pName1,
"name") )
125 fprintf( pFile,
" N=%s ", pName2 ? pName2 :
"???" );
127 else if ( !
strcmp(pName1,
"kernel_size") )
129 fprintf( pFile,
" K=" );
130 Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount );
132 else if ( !
strcmp(pName1,
"strides") )
134 fprintf( pFile,
" S=" );
135 Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount );
137 else if ( !
strcmp(pName1,
"filters") )
138 fprintf( pFile,
" C=%s", pName2 );
139 else if ( !
strcmp(pName1,
"inbound_nodes") )
140 Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount );
141 else if ( !
strcmp(pName1,
"layers") )
142 Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 1, pCount );
143 else if ( !
strcmp(pName1,
"config") )
146 Json_Extract_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), 0, pCount );
154 FILE * pFile = fopen( pFileName,
"wb" );
157 printf(
"Cannot open file \"%s\" for writing.\n", pFileName );
160 fprintf( pFile,
"# Data extracted from JSON file:\n" );
162 fprintf( pFile,
"\n" );
177static inline int Json_CharIsSpace(
char c )
179 return (c ==
' ' || c ==
'\t' || c ==
'\r' || c ==
'\n' || c ==
':');
181static inline char * Json_SkipSpaces(
char * pCur )
183 while ( Json_CharIsSpace(*pCur) )
187static inline char * Json_SkipNonSpaces(
char * pCur )
189 while ( !Json_CharIsSpace(*pCur) )
193static inline int Json_TokenCompare(
char * pCur,
char * pNext,
char ** ppCur2,
char ** ppNext2 )
198 if ( *(pNext-1) ==
',' )
200 if ( *(pNext-1) ==
'\"' )
223 int i, Entry1, Entry2, fComma;
224 if ( Vec_IntEntry(vArray, 0) )
226 if ( Vec_IntSize(vArray) == 1 )
227 fprintf( pFile,
"[]" );
228 else if ( Vec_IntSize(vArray) == 2 && Json_EntryIsName(Vec_IntEntry(vArray,1)) )
229 fprintf( pFile,
"[ \"%s\" ]", Json_EntryName(pStr, Vec_IntEntry(vArray,1)) );
233 fprintf( pFile,
"%*s", 3*(Level-1),
"" );
234 fprintf( pFile,
"[\n" );
237 fComma = (i < Vec_IntSize(vArray) - 1);
238 if ( Json_EntryIsName(Entry1) )
239 fprintf( pFile,
"%*s\"%s\"%s\n", 3*Level,
"", Json_EntryName(pStr, Entry1), fComma ?
",":
"" );
241 Json_Write_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry1), Level+1, fComma, 1 );
243 fprintf( pFile,
"%*s]", 3*(Level-1),
"" );
245 fprintf( pFile,
"%s\n", fAddComma ?
",":
"" );
250 fprintf( pFile,
"%*s", 3*(Level-1),
"" );
251 fprintf( pFile,
"{\n" );
252 assert( Vec_IntSize(vArray) % 2 != 0 );
255 fComma = (i < Vec_IntSize(vArray) - 3);
256 if ( Json_EntryIsName(Entry1) )
257 fprintf( pFile,
"%*s\"%s\"", 3*Level,
"", Json_EntryName(pStr, Entry1) );
259 Json_Write_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry1), Level+1, 0, 1 );
260 fprintf( pFile,
" : " );
261 if ( Json_EntryIsName(Entry2) )
262 fprintf( pFile,
"\"%s\"%s\n", Json_EntryName(pStr, Entry2), fComma ?
",":
"" );
264 Json_Write_rec( pFile, pStr, vObjs, Json_EntryNode(vObjs, Entry2), Level+1, fComma, 0 );
266 fprintf( pFile,
"%*s}%s\n", 3*(Level-1),
"", fAddComma ?
",":
"" );
271 FILE * pFile = fopen( pFileName,
"wb" );
274 printf(
"Cannot open file \"%s\" for writing.\n", pFileName );
277 Json_Write_rec( pFile, pStr, vObjs, Vec_WecEntry(vObjs, 0), 1, 0, 1 );
294 char * pOut =
ABC_ALLOC(
char, 3*nFileSize );
int i, k = 0;
295 for ( i = 0; i < nFileSize; i++ )
296 if ( pIn[i] ==
'{' || pIn[i] ==
'}' || pIn[i] ==
'[' || pIn[i] ==
']' )
312 char * pContents, * pCur, * pNext, * pCur2, * pNext2;
313 int nFileSize, RetValue, iToken;
317 pFile = fopen( pFileName,
"rb" );
320 printf(
"Cannot open file \"%s\" for reading.\n", pFileName );
324 pContents = pCur =
ABC_ALLOC(
char, nFileSize+1 );
325 RetValue = fread( pContents, nFileSize, 1, pFile );
326 pContents[nFileSize] = 0;
330 nFileSize =
strlen(pContents);
335 vObjs = Vec_WecAlloc( 1000 );
336 vStack = Vec_IntAlloc( 100 );
341 assert( Vec_WecSize(vObjs) == 0 );
342 assert( Vec_IntSize(vStack) == 0 );
343 while ( pCur < pContents + nFileSize )
345 pCur = Json_SkipSpaces( pCur );
348 pNext = Json_SkipNonSpaces( pCur );
349 if ( *pCur ==
'{' || *pCur ==
'[' )
352 if ( Vec_IntSize(vStack) > 0 )
353 Vec_IntPush( Vec_WecEntry(vObjs, Vec_IntEntryLast(vStack)), Abc_Var2Lit(Vec_WecSize(vObjs), 0) );
355 Vec_IntPush( vStack, Vec_WecSize(vObjs) );
356 vTemp = Vec_WecPushLevel( vObjs );
357 Vec_IntGrow( vTemp, 4 );
359 Vec_IntPush( vTemp, (
int)(*pCur ==
'[') );
363 if ( *pCur ==
'}' || *pCur ==
']' )
365 Vec_IntPop( vStack );
369 if ( *pCur ==
',' || *pCur ==
':' )
374 iToken = Json_TokenCompare( pCur, pNext, &pCur2, &pNext2 );
377 Vec_IntPush( Vec_WecEntry(vObjs, Vec_IntEntryLast(vStack)), Abc_Var2Lit(iToken, 1) );
380 Vec_IntFree( vStack );
406 Vec_WecFree( vObjs );
#define ABC_ALLOC(type, num)
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_IMPL_START struct Vec_Int_t_ Vec_Int_t
DECLARATIONS ///.
char * Json_ReadPreprocess(char *pIn, int nFileSize)
void Json_Extract(char *pFileName, Abc_Nam_t *pStr, Vec_Wec_t *vObjs)
void Json_Extract_rec(FILE *pFile, Abc_Nam_t *pStr, Vec_Wec_t *vObjs, Vec_Int_t *vArray, int fWrite, int *pCount)
void Json_Write(char *pFileName, Abc_Nam_t *pStr, Vec_Wec_t *vObjs)
Vec_Wec_t * Json_Read(char *pFileName, Abc_Nam_t **ppStrs)
void Json_Write_rec(FILE *pFile, Abc_Nam_t *pStr, Vec_Wec_t *vObjs, Vec_Int_t *vArray, int Level, int fAddComma, int fSpaces)
char * Nnc_LayerType2Str(char *pStr)
FUNCTION DEFINITIONS ///.
void Json_ReadTest(char *pFileName)
int Abc_NamStrFindOrAddLim(Abc_Nam_t *p, char *pStr, char *pLim, int *pfFound)
Abc_Nam_t * Abc_NamStart(int nObjs, int nAveSize)
FUNCTION DEFINITIONS ///.
void Abc_NamDeref(Abc_Nam_t *p)
char * Abc_NamStr(Abc_Nam_t *p, int NameId)
typedefABC_NAMESPACE_HEADER_START struct Abc_Nam_t_ Abc_Nam_t
INCLUDES ///.
#define Vec_IntForEachEntryStart(vVec, Entry, i, Start)
#define Vec_IntForEachEntryDoubleStart(vVec, Entry1, Entry2, i, Start)
typedefABC_NAMESPACE_HEADER_START struct Vec_Wec_t_ Vec_Wec_t
INCLUDES ///.