/* * Read a record from the file. */ static bool readrecord( c_t ** r ) { #define MAXRECORDLENGTH 100000 static c_t record[ MAXRECORDLENGTH ] = {0}; #define MAXLINELENGTH 1000 static c_t line[ MAXLINELENGTH ] = {0}; bool done = false; bool last_record = false; c_t * p = record; *r = record; /* Add the line to record. On the first read, this will be a NOP. On all other reads, this will get the front part of the record (INSERT INTO) that we read the last time. */ strncpy( p, line, MAXLINELENGTH ); p = p + Escher_strlen( line ); while ( ! done ) { if ( fgets( line, MAXLINELENGTH, xtumlfile ) == 0 ) { done = true; last_record = true; } /* Note that we only compare as much as we care about. */ if ( 0 != strncmp( line, "INSERT INTO ", 12 ) ) { strncpy( p, line, MAXLINELENGTH ); p = p + Escher_strlen( line ); line[ 0 ] = 0; } else { done = true; } } return last_record; }
/* * Bridge: trim */ c_t * STRING_trim( c_t * p_s ) { c_t result[ESCHER_SYS_MAX_STRING_LEN]; result[0] = '\0'; c_t * a; c_t * b; // find the first non whitespace character a = p_s; for ( ; *a != '\0'; a++ ) { if ( *a != ' ' && *a != '\r' && *a != '\t' && *a != '\n' ) break; // found non whitespace } // find last non whitespace character i_t len = (i_t)Escher_strlen( p_s ); b = p_s + ( len - 1 ); for ( ; b != p_s; b-- ) { if ( *a != ' ' && *a != '\r' && *a != '\t' && *a != '\n' ) break; // found non whitespace } // check if they crossed ( all whitespace ) if ( b < a ) { return result; } else { return STRING_substr( (const i_t)(a - p_s), (const i_t)(b - p_s)+1, p_s ); } }
/* * Bridge: strlen */ i_t STRING_strlen( c_t * p_s ) { i_t result = 0; result = (i_t)Escher_strlen( p_s ); return result; }
/* * Copy characters and be paranoid about null delimiter. */ c_t * Escher_strcpy( c_t * dst, const c_t * src ) { c_t * s = dst; if ( 0 != src ) { Escher_size_t i = Escher_strlen( src ) + 1; s = Escher_malloc( i ); dst = s; while ( ( i > 0 ) && ( *src != '\0' ) ) { --i; *dst++ = *src++; } *dst = '\0'; /* Ensure delimiter. */ } return s; }
/* * Bridge: substr */ c_t * STRING_substr( const i_t p_begin, const i_t p_end, c_t * p_s ) { c_t result[ESCHER_SYS_MAX_STRING_LEN]; result[0] = '\0'; // get length of s i_t len = (i_t)Escher_strlen( p_s ); // check that the indexes are in a valid range i_t begin = p_begin; i_t end = p_end; if ( begin > len - 1 ) { return result; } if ( begin < 0 ) { begin = 0; } if ( end < 0 || end > len ) { end = len; } if ( end <= begin ) { return result; } // if we have a string and the end is greater than begin if ( !(len == 0 || end <= begin) ) { // copy in the new string c_t * p = p_s + begin; c_t * r = result; while ( p < p_s + end ) { *r++ = *p++; } *r = '\0'; // null terminate } return result; }
/* * Bridge: getword */ c_t * STRING_getword( const i_t p_i, const i_t p_j, c_t * p_s ) { c_t result[ESCHER_SYS_MAX_STRING_LEN]; result[0] = '\0'; i_t len = (i_t)Escher_strlen( p_s ); i_t lim = p_j; // if j is -1, it just means the full length of the string if ( -1 == lim ) lim = len; // check arguments if ( !(p_i < 0 || p_i > len - 1) && !(lim < 0 || lim > len) ) { c_t * w_begin; c_t * w_end; // find the first non comma, whitespace, or close parenthesis w_begin = p_s + p_i; while ( w_begin - p_s < len ) { if ( *w_begin != ',' && *w_begin != ' ' && *w_begin != '\n' && *w_begin != '\r' && *w_begin != '\t' && *w_begin != '(' && *w_begin != ')' ) break; w_begin++; } // find the first comma, whitespace, or close parenthesis after starting the word w_end = w_begin; while ( w_end - p_s < len ) { if ( *w_end == ',' || *w_end == ' ' || *w_end == '\n' || *w_end == '\r' || *w_end == '\t' || *w_end == '(' || *w_end == ')' ) break; w_end++; } if ( (w_begin - p_s) < lim && (w_end - p_s) <= lim ) { // copy the substring into the result //Escher_strcpy( result, STRING_substr( (const i_t)(w_begin - p_s), (const i_t)(w_end - p_s), p_s ) ); c_t * sub = STRING_substr( (const i_t)(w_begin - p_s), (const i_t)(w_end - p_s), p_s ); // Escher_strcpy c_t * dst = result; c_t * src = sub; if ( 0 != src ) { Escher_size_t i = Escher_strlen( src ) + 1; while ( ( i > 0 ) && ( *src != '\0' ) ) { --i; *dst++ = *src++; } *dst = '\0'; /* Ensure delimiter. */ } } } return result; }