int main(int argc, char* argv[]) { const char* bitstr; const char* polystr; int bits; int digits; uint64 poly; const char* suffix; int columns; char* end; int i; const char* name; int reflected; if (argc != 5) { msg3("usage: ", program, " NAME BITS POLY [normal|reflected]"); return 1; } name = argv[1]; bitstr = argv[2]; bits = strtol(bitstr, &end, 0); if (*end != 0) die2(1, "Invalid bits value: ", bitstr); if (bits <= 0 || bits > 64) die1(1, "bits must be between 1 and 64"); digits = (bits + 3) / 4; if (bits > 32) suffix = "ULL,"; else if (bits > 16) suffix = "UL,"; else suffix = "U,"; columns = calc_columns(bits, suffix); polystr = argv[3]; poly = strtoull(polystr, &end, 0); if (*end != 0) die2(1, "Invalid poly value: ", polystr); reflected = 0; if (strcasecmp(argv[4], "reflected") == 0) reflected = 1; else if (strcasecmp(argv[4], "normal") != 0) die2(1, "Must be either 'normal' or 'reflected': ", argv[4]); gentab(bits, poly, reflected); obuf_put3s(&outbuf, "#include \"", name, ".h\"\n\n"); obuf_put5s(&outbuf, "const uint", bitstr, " ", name, "_table[256] = {\n"); for (i = 0; i < 256; ++i) { int column = i % columns; if (column == 0) obuf_puts(&outbuf, " "); obuf_puts(&outbuf, "0x"); if (bits > 32) { obuf_putxw(&outbuf, crctab[i]>>32, digits-8, '0'); obuf_putxw(&outbuf, crctab[i] & 0xffffffffUL, 8, '0'); } else
bool one_columns_read(const char * filename, int col1, int skip_lines, const char * delimiter, int grow_by, vec *& vcol1, int read_lines) { FILE * file = fopen(filename, "rb"); if (!file) { writelog(LOG_ERROR, "The file %s was not opened: %s",filename,strerror( errno )); return false; } if (ferror(file) != 0) { writelog(LOG_ERROR, "Loading from one-column file: file error."); fclose(file); return false; } if (grow_by == 250) { int size = file_size(file); grow_by = MAX(250,size/100); } char string_buf[MY_READ_BUF_SIZE]; int i; for (i = 0; i < skip_lines; i++) { if (!fgets(string_buf,MY_READ_BUF_SIZE,file)) { fclose(file); return false; } if (feof(file)) { fclose(file); return false; } }; vcol1 = create_vec(); vcol1->set_grow(grow_by); char * seps = strdup(delimiter); char * token = NULL; int columns = 0; int rows = 0; int lines_readed = 0; int current_column = 0; int size = 0; // calculate number of columns! fpos_t pos; if (fgetpos(file, &pos) != 0) goto one_columns_read_failed; if (!fgets(string_buf,MY_READ_BUF_SIZE,file)) goto one_columns_read_failed; columns = calc_columns(string_buf, MY_READ_BUF_SIZE, seps); fread(string_buf, MY_READ_BUF_SIZE, sizeof(char), file); if (ferror(file) != 0) goto one_columns_read_failed; rows = calc_rows(string_buf, MY_READ_BUF_SIZE); if (fsetpos(file, &pos) != 0) goto one_columns_read_failed; if (col1 > columns) goto one_columns_read_failed; while (!feof(file) ) { if (!fgets(string_buf,MY_READ_BUF_SIZE,file)) { if (feof(file)) break; goto one_columns_read_failed; } if (read_lines > 0) { lines_readed++; if (lines_readed > read_lines) break; } current_column = 0; token = strtok(string_buf, seps); REAL val1 = FLT_MAX, val2 = FLT_MAX; while (token != NULL) { if ((current_column+1 == col1) || (rows == 1)) { if (strlen(token) > 0) val1 = (REAL)ator(token); } if ((rows == 1) && (val1 != FLT_MAX)) { vcol1->push_back(val1); val1 = FLT_MAX; } current_column++; token = strtok( NULL, seps ); } if ( (((val1 != FLT_MAX) || (col1 == 0)) && (rows != 1)) ) { vcol1->push_back(val1); } val1 = FLT_MAX; } free(seps); fclose(file); return true; one_columns_read_failed: fclose(file); free(seps); if (vcol1) vcol1->release(); vcol1 = NULL; return false; };
bool three_columns_read_with_names(const char * filename, int col1, int col2, int col3, int col4, int skip_lines, const char * delimiter, int grow_by, vec *& vcol1, vec *& vcol2, vec *& vcol3, strvec *& names, int read_lines) { FILE * file = fopen(filename, "rb"); if (!file) { writelog(LOG_ERROR, "The file %s was not opened: %s",filename,strerror( errno )); return false; } if (ferror(file) != 0) { writelog(LOG_ERROR, "Loading from therr-column file: file error."); fclose(file); return false; } if (grow_by == 250) { int size = file_size(file); grow_by = MAX(250,size/100); } char string_buf[MY_READ_BUF_SIZE]; int i; for (i = 0; i < skip_lines; i++) { if (!fgets(string_buf,MY_READ_BUF_SIZE,file)) { fclose(file); return false; } if (feof(file)) { fclose(file); return false; } }; vcol1 = create_vec(); vcol1->set_grow(grow_by); vcol2 = create_vec(); vcol2->set_grow(grow_by); vcol3 = create_vec(); vcol3->set_grow(grow_by); names = create_strvec(); names->set_grow(grow_by); char * seps = strdup(delimiter); char * token = NULL; int columns = 0; int lines_readed = 0; int current_column = 0; size_t size = 0; // calculate number of columns! long pos = ftell(file); if (pos == -1L) goto three_columns_read_failed; if (!fgets(string_buf,MY_READ_BUF_SIZE,file)) goto three_columns_read_failed; if (fseek(file, pos, SEEK_SET) != 0) goto three_columns_read_failed; columns = calc_columns(string_buf, MY_READ_BUF_SIZE, seps); if (col1 > columns) goto three_columns_read_failed; if (col2 > columns) goto three_columns_read_failed; if (col3 > columns) goto three_columns_read_failed; if (col4 > columns) goto three_columns_read_failed; while (!feof(file) ) { if (!fgets(string_buf,MY_READ_BUF_SIZE,file)) { if (feof(file)) break; goto three_columns_read_failed; } if (read_lines > 0) { lines_readed++; if (lines_readed > read_lines) break; } current_column = 0; token = strtok(string_buf, seps); REAL val1 = FLT_MAX, val2 = FLT_MAX, val3 = FLT_MAX; char * sval = NULL; while (token != NULL) { if (current_column+1 == col1) { if (strlen(token) > 0) val1 = (REAL)ator(token); } if (current_column+1 == col2) { if (strlen(token) > 0) val2 = (REAL)ator(token); } if (current_column+1 == col3) { if (strlen(token) > 0) val3 = (REAL)ator(token); } if (current_column+1 == col4) { if (strlen(token) > 0) { if (sval) free(sval); sval = strdup(token); } } current_column++; token = strtok( NULL, seps ); } if ( ((val1 != FLT_MAX) || (col1 == 0)) && ((val2 != FLT_MAX) || (col2 == 0)) && ((val3 != FLT_MAX) || (col3 == 0)) && ((sval != NULL) || (col4 == 0)) ) { vcol1->push_back(val1); vcol2->push_back(val2); vcol3->push_back(val3); names->push_back(sval); free(sval); } val1 = FLT_MAX; val2 = FLT_MAX; val3 = FLT_MAX; sval = NULL; } size = MIN( vcol1->size(), vcol2->size() ); size = MIN( size, vcol3->size() ); vcol1->resize(size); vcol2->resize(size); vcol3->resize(size); names->resize(size); free(seps); fclose(file); return true; three_columns_read_failed: fclose(file); free(seps); if (vcol1) vcol1->release(); if (vcol2) vcol2->release(); if (vcol3) vcol3->release(); if (names) names->release(); vcol1 = NULL; vcol2 = NULL; vcol3 = NULL; names = NULL; return false; };