00001
00021 #include "io.h"
00022
00026
00027 static Abc_Ntk_t * Io_ReadEqnNetwork( Extra_FileReader_t * p );
00028 static void Io_ReadEqnStrCompact( char * pStr );
00029 static int Io_ReadEqnStrFind( Vec_Ptr_t * vTokens, char * pName );
00030 static void Io_ReadEqnStrCutAt( char * pStr, char * pStop, int fUniqueOnly, Vec_Ptr_t * vTokens );
00031
00035
00047 Abc_Ntk_t * Io_ReadEqn( char * pFileName, int fCheck )
00048 {
00049 Extra_FileReader_t * p;
00050 Abc_Ntk_t * pNtk;
00051
00052
00053 p = Extra_FileReaderAlloc( pFileName, "#", ";", "=" );
00054 if ( p == NULL )
00055 return NULL;
00056
00057
00058 pNtk = Io_ReadEqnNetwork( p );
00059 Extra_FileReaderFree( p );
00060 if ( pNtk == NULL )
00061 return NULL;
00062
00063
00064 if ( fCheck && !Abc_NtkCheckRead( pNtk ) )
00065 {
00066 printf( "Io_ReadEqn: The network check has failed.\n" );
00067 Abc_NtkDelete( pNtk );
00068 return NULL;
00069 }
00070 return pNtk;
00071 }
00072
00084 Abc_Ntk_t * Io_ReadEqnNetwork( Extra_FileReader_t * p )
00085 {
00086 ProgressBar * pProgress;
00087 Vec_Ptr_t * vTokens;
00088 Vec_Ptr_t * vVars;
00089 Abc_Ntk_t * pNtk;
00090 Abc_Obj_t * pNode;
00091 char * pNodeName, * pFormula, * pFormulaCopy, * pVarName;
00092 int iLine, i;
00093
00094
00095 pNtk = Abc_NtkAlloc( ABC_NTK_NETLIST, ABC_FUNC_AIG, 1 );
00096
00097 pNtk->pName = Extra_FileNameGeneric(Extra_FileReaderGetFileName(p));
00098 pNtk->pSpec = Extra_UtilStrsav(Extra_FileReaderGetFileName(p));
00099
00100
00101 vVars = Vec_PtrAlloc( 100 );
00102 pProgress = Extra_ProgressBarStart( stdout, Extra_FileReaderGetFileSize(p) );
00103 for ( iLine = 0; vTokens = Extra_FileReaderGetTokens(p); iLine++ )
00104 {
00105 Extra_ProgressBarUpdate( pProgress, Extra_FileReaderGetCurPosition(p), NULL );
00106
00107
00108 Io_ReadEqnStrCompact( vTokens->pArray[0] );
00109 if ( strlen(vTokens->pArray[0]) == 0 )
00110 break;
00111
00112
00113 if ( vTokens->nSize != 2 )
00114 {
00115 printf( "%s: Wrong input file format.\n", Extra_FileReaderGetFileName(p) );
00116 Abc_NtkDelete( pNtk );
00117 return NULL;
00118 }
00119
00120
00121 if ( strncmp( vTokens->pArray[0], "INORDER", 7 ) == 0 )
00122 {
00123 Io_ReadEqnStrCutAt( vTokens->pArray[1], " \n\r\t", 0, vVars );
00124 Vec_PtrForEachEntry( vVars, pVarName, i )
00125 Io_ReadCreatePi( pNtk, pVarName );
00126 }
00127 else if ( strncmp( vTokens->pArray[0], "OUTORDER", 8 ) == 0 )
00128 {
00129 Io_ReadEqnStrCutAt( vTokens->pArray[1], " \n\r\t", 0, vVars );
00130 Vec_PtrForEachEntry( vVars, pVarName, i )
00131 Io_ReadCreatePo( pNtk, pVarName );
00132 }
00133 else
00134 {
00135 extern Hop_Obj_t * Parse_FormulaParserEqn( FILE * pOutput, char * pFormInit, Vec_Ptr_t * vVarNames, Hop_Man_t * pMan );
00136
00137
00138 pNodeName = vTokens->pArray[0];
00139 pFormula = vTokens->pArray[1];
00140
00141 Io_ReadEqnStrCompact( pFormula );
00142
00143
00144 if ( pFormula[1] == 0 && (pFormula[0] == '0' || pFormula[0] == '1') )
00145 {
00146 pFormulaCopy = NULL;
00147 Vec_PtrClear( vVars );
00148 }
00149 else
00150 {
00151
00152 pFormulaCopy = Extra_UtilStrsav( pFormula );
00153
00154 Io_ReadEqnStrCutAt( pFormulaCopy, "!*+()", 1, vVars );
00155 }
00156
00157 pNode = Io_ReadCreateNode( pNtk, pNodeName, (char **)Vec_PtrArray(vVars), Vec_PtrSize(vVars) );
00158
00159 pNode->pData = Parse_FormulaParserEqn( stdout, pFormula, vVars, pNtk->pManFunc );
00160
00161 FREE( pFormulaCopy );
00162 }
00163 }
00164 Extra_ProgressBarStop( pProgress );
00165 Vec_PtrFree( vVars );
00166 Abc_NtkFinalizeRead( pNtk );
00167 return pNtk;
00168 }
00169
00170
00171
00183 void Io_ReadEqnStrCompact( char * pStr )
00184 {
00185 char * pCur, * pNew;
00186 for ( pNew = pCur = pStr; *pCur; pCur++ )
00187 if ( !(*pCur == ' ' || *pCur == '\n' || *pCur == '\r' || *pCur == '\t') )
00188 *pNew++ = *pCur;
00189 *pNew = 0;
00190 }
00191
00203 int Io_ReadEqnStrFind( Vec_Ptr_t * vTokens, char * pName )
00204 {
00205 char * pToken;
00206 int i;
00207 Vec_PtrForEachEntry( vTokens, pToken, i )
00208 if ( strcmp( pToken, pName ) == 0 )
00209 return i;
00210 return -1;
00211 }
00212
00224 void Io_ReadEqnStrCutAt( char * pStr, char * pStop, int fUniqueOnly, Vec_Ptr_t * vTokens )
00225 {
00226 char * pToken;
00227 Vec_PtrClear( vTokens );
00228 for ( pToken = strtok( pStr, pStop ); pToken; pToken = strtok( NULL, pStop ) )
00229 if ( !fUniqueOnly || Io_ReadEqnStrFind( vTokens, pToken ) == -1 )
00230 Vec_PtrPush( vTokens, pToken );
00231 }
00232
00233
00237
00238
00239