int main() { ServerMsgLexer lexer; char buffer[4096]; int readValue; try { do { readValue = read(STDIN_FILENO, &buffer, sizeof(buffer)); if (READ_ERROR(readValue)) { perror("read"); break; } else if (READ_EOF(readValue)) { printf("[EOF]\n"); break; } else { lexer.putNewData(buffer, readValue); printMessages(lexer); } } while (1); } catch(Exception& ex) { printf("Exception: %s\n", ex.toString()); } return 0; }
void read_string( const char* szFileName, const char* szVarName, char* pVariable) { char* szValue = NULL; /* string containg the read variable value */ if( szVarName == 0 ) ERROR("null pointer given as variable name" ); if( szFileName == 0 ) ERROR("null pointer given as filename" ); if( pVariable == 0 ) ERROR("null pointer given as variable" ); if( szVarName[0] == '*' ) szValue = find_string( szFileName, szVarName +1 ); else szValue = find_string( szFileName, szVarName ); if( sscanf( szValue, "%s", pVariable) == 0) READ_ERROR("wrong format", szVarName, szFileName,0); int rank; MPI_Comm_rank( MPI_COMM_WORLD, &rank ); if(!rank) printf( "File: %s\t\t%s%s= %s\n", szFileName, szVarName, &(" "[min_int( (int)strlen(szVarName), 15)]), pVariable ); }
void read_double( const char* szFileName, const char* szVarName, double* pVariable) { char* szValue = NULL; /* String mit dem eingelesenen Variablenwert */ if( szVarName == 0 ) ERROR("null pointer given as varable name" ); if( szFileName == 0 ) ERROR("null pointer given as filename" ); if( pVariable == 0 ) ERROR("null pointer given as variable" ); if( szVarName[0] == '*' ) szValue = find_string( szFileName, szVarName +1 ); else szValue = find_string( szFileName, szVarName ); if( sscanf( szValue, "%lf", pVariable) == 0) READ_ERROR("wrong format", szVarName, szFileName, 0); printf( "File: %s\t\t%s%s= %f\n", szFileName, szVarName, &(" "[min_int( strlen(szVarName), 15)]), *pVariable ); }
void read_int( const char* szFileName, const char* szVarName, int* pVariable) { char* szValue = NULL; /* string containing the read variable value */ if( szVarName == 0 ) ERROR("null pointer given as varable name" ); if( szFileName == 0 ) ERROR("null pointer given as filename" ); if( pVariable == 0 ) ERROR("null pointer given as variable" ); if( szVarName[0] == '*' ) szValue = find_string( szFileName, szVarName +1 ); else szValue = find_string( szFileName, szVarName ); if( sscanf( szValue, "%d", pVariable) == 0) READ_ERROR("wrong format", szVarName, szFileName, 0); printf( "File: %s\t\t%s%s= %d\n", szFileName, szVarName, &(" "[min_int( strlen(szVarName), 15)]), *pVariable ); }
char* find_string( const char* szFileName, const char *szVarName ) { int nLine = 0; int i; FILE *fh = NULL; static char szBuffer[MAX_LINE_LENGTH]; /* containes the line read */ /* from the datafile */ char* szLine = szBuffer; char* szValue = NULL; char* szName = NULL; /* open file */ fh = fopen( szFileName, "rt" ); if( fh == 0 ) READ_ERROR("Could not open file", szVarName, szFileName, 0); /* searching */ while( ! feof(fh) ) { fgets( szLine, MAX_LINE_LENGTH, fh ); ++nLine; /* remove comments */ for( i = 0; i < strlen(szLine); i++) if( szLine[i] == '#' ) { szLine[i] = '\0'; /* Stringende setzen */ break; } /* remove empty lines */ while( isspace( (int)*szLine ) && *szLine) ++szLine; if( strlen( szLine ) == 0) continue; /* now, the name can be extracted */ szName = szLine; szValue = szLine; while( (isalnum( (int)*szValue ) || *szValue == '_') && *szValue) ++szValue; /* is the value for the respective name missing? */ if( *szValue == '\n' || strlen( szValue) == 0) READ_ERROR("wrong format", szName, szFileName, nLine); *szValue = 0; /* complete szName! at the right place */ ++szValue; /* read next line if the correct name wasn't found */ if( strcmp( szVarName, szName)) continue; /* remove all leading blnkets and tabs from the value string */ while( isspace( (int)*szValue) ) ++szValue; if( *szValue == '\n' || strlen( szValue) == 0) READ_ERROR("wrong format", szName, szFileName, nLine); fclose(fh); return szValue; } READ_ERROR("variable not found", szVarName, szFileName, nLine); return NULL; /* dummy to satisfy the compiler */ }
/*! \brief Reads a graph from a file, binary format \param[in,out] graph At output, <em>(*graph)</em> has been initialized by using the information on the file \em filename. \param[in] filename The name of the file, binary formatted similar to \c GMRFLib_read_graph() (without newlines), containing the specification of the graph. \sa GMRFLib_read_graph, GMRFLib_write_graph_binary */ int GMRFLib_read_graph_binary_EXPERIMENTAL(GMRFLib_graph_tp ** graph, const char *filename) { /* * read a graph in the following format * * N node[0] nnbs[0] nbs[node[0]][0] nbs[node[0]][1] ... nbs[node[0]][nnbs[0]-1] node[1] nnbs[1] nbs[node[1]][0] * nbs[node[1]][1] ... nbs[node[1]][nnbs[1]-1] : node[N-1] nnbs[N-1] nbs[node[N-1]][0] nbs[node[N-1]][1] ... * nbs[node[N-1]][nnbs[N-1]-1] */ #define READ_ERROR() do { \ if (1){\ fprintf(stderr,"\n\n\t%s: error: file [%s]:\n",__GMRFLib_FuncName,filename);\ fprintf(stderr,"\t\tfail to read [%1lu] bytes from byte [%1lu]\n", (unsigned long)(nelm*sizeof(int)), (unsigned long)byte); \ } \ if (fp) fclose(fp);\ (*graph) = NULL;\ GMRFLib_ERROR(GMRFLib_EREADFILE);} while(0) int *storage, n_neig_tot = 0, storage_indx; int i, byte, tnode; size_t nelm; FILE *fp; if (!filename) { return GMRFLib_SUCCESS; } if (!(fp = fopen(filename, "r"))) { GMRFLib_ERROR(GMRFLib_EOPENFILE); } GMRFLib_make_empty_graph(graph); byte = 0; nelm = 1; if (fread(&((*graph)->n), sizeof(int), nelm, fp) != 1) { READ_ERROR(); } byte += sizeof(int); (*graph)->nnbs = Calloc((*graph)->n, int); (*graph)->nbs = Calloc((*graph)->n, int *); for (i = 0; i < (*graph)->n; i++) { nelm = 1; if (fread(&tnode, sizeof(int), nelm, fp) != 1) { READ_ERROR(); /* target node */ } byte += sizeof(int); if (tnode < 0 || tnode >= (*graph)->n) { fprintf(stderr, "\n\n\t%s: error: file [%s]: byte[%1d]\n", __GMRFLib_FuncName, filename, byte); fprintf(stderr, "\t\tnode-number[%1d] is not in the range[0:%1d]\n", tnode, (*graph)->n); if (fp) { fclose(fp); } GMRFLib_ERROR(GMRFLib_EPARAMETER); } nelm = 1; if (fread(&((*graph)->nnbs[tnode]), sizeof(int), nelm, fp) != 1) { READ_ERROR(); } byte += sizeof(int); n_neig_tot += (*graph)->nnbs[tnode]; if ((*graph)->nnbs[tnode]) { (*graph)->nbs[tnode] = Calloc((*graph)->nnbs[tnode], int); nelm = (*graph)->nnbs[tnode]; if (fread((*graph)->nbs[tnode], sizeof(int), nelm, fp) != nelm) { READ_ERROR(); } byte += sizeof(int) * ((*graph)->nnbs[tnode]); } else {