void Skim_File::Setup (void) { Matrix_File::Setup (); File_Type ("Skim File"); File_ID ("Skim"); time_format = SECONDS; len_format = FEET; time = walk = drive = transit = wait = other = length = cost = impedance = -1; time_flag = (Data_Type () == TIME_TABLE); table_flag = len_flag = false; mode = MAX_MODE; near_factor = 0; table = 0; ttime = 0; }
bool User_Program::Initial_Table (String &line) { int i, j, index, lnum, len; Int_Itr litr, litr0; double fnum; Dbl_Itr fitr, fitr0; String token; Str_Itr sitr, sitr0; string::iterator ch, ch0; i = cmd.token; j = cmd.value; for (;;) { //---- remove leading spaces ---- line.Trim_Left (" \t"); if (line.empty ()) return (false); ch = ch0 = line.begin (); //---- determine the token type ---- if ((*ch >= '0' && *ch <= '9') || (*ch == '.' && *(ch+1) != '.') || (*ch == '-' && (*(ch+1) >= '0' && *(ch+1) <= '9'))) { //---- process a number ---- if (*ch == '-') ch++; do { if (++ch == line.end ()) break; } while ((*ch >= '0' && *ch <= '9') || (*ch == '.' && *(ch+1) != '.')); len = (int) (ch - ch0); token = line.substr (0, len); line.erase (0, len); //---- set the table size ---- if (open_par) { lnum = token.Integer (); if (lnum < 1) { exe->Error (String ("%s Table %d Size %d is Out of Range") % Data_Type (i) % j % lnum); return (false); } if (table [i] [j].size () > 0) { exe->Write (1); exe->Warning (String ("%s Table %d is Redefined") % Data_Type (i) % j); table [i] [j].clear (); } table [i] [j].reserve (lnum); } else { if (table_type == STAB) { exe->Error ("Attempt to Assign a Number to a String Table"); return (false); } //---- search for the value in the list ---- if (table_type == ITAB) { lnum = token.Integer (); for (litr = litr0 = lvalue.begin (); litr != lvalue.end (); litr++) { if (*litr == lnum) break; } if (litr == lvalue.end ()) { index = (int) lvalue.size (); //---- 0 based ---- lvalue.push_back (lnum); } else { index = (int) (litr - litr0); //---- 0 based ---- } } else { fnum = token.Double (); for (fitr = fitr0 = fvalue.begin (); fitr != fvalue.end (); fitr++) { if (*fitr == fnum) break; } if (fitr == fvalue.end ()) { index = (int) fvalue.size (); //---- 0 based ---- fvalue.push_back (fnum); } else { index = (int) (fitr - fitr0); //---- 0 based ---- } } table [i] [j].push_back (index); } } else if (*ch == '"') { if (table_type != STAB) goto error; //---- process a string ---- ch++; token.clear (); while (ch != line.end () && *ch != '"') { token += *ch++; } ch++; line.erase (0, ch - ch0); for (sitr = sitr0 = svalue.begin (); sitr != svalue.end (); sitr++) { if (token.Equals (*sitr)) break; } if (sitr == svalue.end ()) { index = (int) svalue.size (); svalue.push_back (token); } else { index = (int) (sitr - sitr0); //---- 0 based ---- } table [i] [j].push_back (index); } else if ((*ch >= 'A' && *ch <= 'Z') || (*ch >= 'a' && *ch <= 'z')) { do { if (++ch == line.end ()) break; } while ((*ch >= 'A' && *ch <= 'Z') || (*ch >= 'a' && *ch <= 'z') || (*ch >= '0' && *ch <= '9') || (*ch == '.' && *(ch+1) != '.') || *ch == '_'); len = (int) (ch - ch0); token = line.substr (0, len); line.erase (0, len); if (token.Equals ("ENDTAB")) { table_flag = false; open_par = false; return (true); } else { goto error; } } else if (*ch == '(' || *ch == ')' || *ch == ',' || *ch == '=') { if (*ch == '(') { open_par = true; } else if (*ch == ')') { open_par = false; } else if (*ch == ',' || *ch == '=') { if (open_par) { goto error; } } ch++; line.erase (0, ch - ch0); } else { goto error; } } return (true); error: return (Syntax_Error ("Table Declaration")); }
bool User_Program::Math (void) { int l1, l2; double f1, f2; const char *math_types [] = {"Plus", "Minus", "Multiply", "Divide", "Modulo", "Power", "Negative"}; if (cmd_ptr->token == NEGATIVE) { if (s->type == STRUCT_DATA || s->type == STRING_DATA) goto error; //---- reverse the sign ---- if (s->type == INT_DATA) { s->lvalue = -(s->lvalue); } else if (s->type == FLOAT_DATA) { s->fvalue = -(s->fvalue); } else { goto error; } return (true); } if (s->type == STRUCT_DATA || s1->type == STRUCT_DATA) goto error; //---- process the data types ---- if (s->type == STRING_DATA || s1->type == STRING_DATA) { //---- string contatination ---- if (cmd_ptr->token != PLUS || s->type != STRING_DATA || s1->type != STRING_DATA) goto error; *(s1->str_ptr) += *(s->str_ptr); } else if (s1->type == FLOAT_DATA || s->type == FLOAT_DATA) { //---- floating point math ---- if (s1->type == FLOAT_DATA) { f1 = s1->fvalue; } else { f1 = s1->lvalue; } if (s->type == FLOAT_DATA) { f2 = s->fvalue; } else { f2 = s->lvalue; } switch (cmd_ptr->token) { case PLUS: f1 += f2; break; case MINUS: f1 -= f2; break; case MULTIPLY: f1 *= f2; break; case DIVIDE: if (f2) { f1 /= f2; } break; case MODULO: if (f2) { f1 = (f1 / f2) - 1.0; } break; case POWER: f1 = pow (f1, f2); break; case FMIN: if (f2 < f1) f1 = f2; break; case FMAX: if (f2 > f1) f1 = f2; break; default: goto token_error; break; } s1->type = FLOAT_DATA; s1->fvalue = f1; } else { l1 = s1->lvalue; l2 = s->lvalue; switch (cmd_ptr->token) { case PLUS: l1 += l2; break; case MINUS: l1 -= l2; break; case MULTIPLY: l1 *= l2; break; case DIVIDE: if (l2) { l1 /= l2; } break; case MODULO: if (l2) { l1 %= l2; } break; case POWER: l1 = (int) pow ((double) l1, (double) l2); break; case FMIN: if (l2 < l1) l1 = l2; break; case FMAX: if (l2 > l1) l1 = l2; break; default: goto token_error; break; } s1->type = INT_DATA; s1->lvalue = l1; } s = &stack [--sindex]; return (true); error: if (cmd_ptr->token == FMIN || cmd_ptr->token == FMAX) { exe->Error (String ("Function \"%s\" does not support %s Data") % Token_Name (FUNCTION, cmd_ptr->token) % Data_Type (s->type)); } else { exe->Error (String ("%s Operator does not support %s Data") % math_types [cmd_ptr->token - PLUS] % Data_Type (s->type)); } return (false); token_error: exe->Error (String ("Unrecognized Math Token = %d") % cmd_ptr->token); return (false); }
bool User_Program::Initial_Table (void) { char *token_ptr; double fnum, *fptr; int i, j, lnum, *lptr; Svalue_Data *sptr, srec; i = cmd.token; j = cmd.value; for (;;) { while (*buf == ' ' && *buf != '\0') { buf++; } if (*buf == '\0') break; token_ptr = token; //---- determine the token type ---- if ((*buf >= '0' && *buf <= '9') || (*buf == '.' && *(buf+1) != '.')) { //---- process a number ---- do { *token_ptr++ = *buf++; } while ((*buf >= '0' && *buf <= '9') || (*buf == '.' && *(buf+1) != '.')); *token_ptr = '\0'; //---- set the table size ---- if (open_par) { lnum = atol (token); if (lnum < 1) { exe->Error ("%s Table %d Size %d is Out of Range", Data_Type (i), j, lnum); return (false); } if (table [i] [j].Num_Records () > 0) { exe->Write (1); exe->Warning ("%s Table %d is Redefined", Data_Type (i), j); table [i] [j].Reset (); } if (!table [i] [j].Max_Records (lnum)) goto mem_error; } else { if (table_type == STAB) { exe->Error ("Attempt to Assign a Number to a String Table"); return (false); } //---- search for the value in the list ---- if (table_type == ITAB) { lnum = atol (token); for (lptr = lvalue.First (); lptr != NULL; lptr = lvalue.Next ()) { if (*lptr == lnum) break; } if (lptr == NULL) { if (!lvalue.Add (lnum)) goto mem_error; } lnum = lvalue.Record_Index (); } else { fnum = atof (token); for (fptr = fvalue.First (); fptr != NULL; fptr = fvalue.Next ()) { if (*fptr == fnum) break; } if (fptr == NULL) { if (!fvalue.Add (fnum)) goto mem_error; } lnum = fvalue.Record_Index (); } table [i] [j].Add (lnum); } } else if (*buf == '"') { if (table_type != STAB) goto error; //---- process a string ---- buf++; while (*buf != '"' && *buf != '\0') { *token_ptr++ = *buf++; } buf++; *token_ptr = '\0'; for (sptr = svalue.First (); sptr != NULL; sptr = svalue.Next ()) { if (!str_cmp (sptr->string, token)) break; } if (sptr == NULL) { srec.length = (int) strlen (token) + 1; srec.string = new char [srec.length]; if (srec.string == NULL) goto mem_error; str_cpy (srec.string, srec.length, token); if (!svalue.Add (&srec)) goto mem_error; } lnum = svalue.Record_Index (); table [i] [j].Add (lnum); } else if ((*buf >= 'A' && *buf <= 'Z') || (*buf >= 'a' && *buf <= 'z')) { do { *token_ptr++ = *buf++; } while ((*buf >= 'A' && *buf <= 'Z') || (*buf >= 'a' && *buf <= 'z') || (*buf >= '0' && *buf <= '9') || (*buf == '.' && *(buf+1) != '.') || *buf == '_'); *token_ptr = '\0'; if (!(str_cmp ("ENDTAB", token))) { table_flag = false; open_par = false; return (true); } else { goto error; } } else if (*buf == '(' || *buf == ')' || *buf == ',' || *buf == '=') { if (*buf == '(') { open_par = true; } else if (*buf == ')') { open_par = false; } else if (*buf == ',' || *buf == '=') { if (open_par) { goto error; } } buf++; } else { goto error; } } return (true); error: return (Syntax_Error ("Table Declaration")); mem_error: exe->Error ("Insufficient Memory for %s Table %d", Data_Type (i), j); return (false); }
bool User_Program::Convert (void) { String *str_ptr; //--- sub string function ---- if (cmd_ptr->token == SUBSTR) { int i1, i2, len; if (sindex <= 2) goto string_error; Stack_Data *s2 = &stack [sindex - 2]; if (s2->type != STRING_DATA || s1->type != INT_DATA || s->type != INT_DATA) goto string_error; i1 = s1->lvalue; i2 = s->lvalue; str_ptr = s2->str_ptr; len = (int) str_ptr->length (); if (i1 < 0) i1 = 0; if (i2 < i1) i2 = i1; if (i2 > len) { i2 = len; if (i1 > i2) i1 = i2; } len = i2 - i1 + 1; (*str_ptr) = str_ptr->substr (i1, len); sindex -= 2; s = s2; s->str_ptr = str_ptr; return (true); } else if (cmd_ptr->token == TRIM) { if (s->type != STRING_DATA) { exe->Error ("Illegal TRIM Function Syntax"); return (false); } s->str_ptr->Trim (); return (true); } //---- type conversion functions ---- if (s->type == STRUCT_DATA) goto error; if (cmd_ptr->token != ATOI && cmd_ptr->token != ATOF) { if (s->type == STRING_DATA) goto error; } else { if (s->type != STRING_DATA) goto error; } switch (cmd_ptr->token) { case FINT: if (s->type == FLOAT_DATA) { s->type = INT_DATA; s->lvalue = (int) s->fvalue; } break; case FFLOAT: if (s->type == INT_DATA) { s->type = FLOAT_DATA; s->fvalue = (double) s->lvalue; } break; case ROUND: if (s->type == FLOAT_DATA) { s->type = INT_DATA; s->lvalue = (int) (s->fvalue + 0.5); } break; case ATOI: s->type = INT_DATA; s->lvalue = s->str_ptr->Integer (); break; case ATOF: s->type = FLOAT_DATA; s->fvalue = s->str_ptr->Double (); break; case ITOA: if (s->type == FLOAT_DATA) { s->lvalue = (int) s->fvalue; } str_ptr = &svalue [0]; (*str_ptr)("%d") % s->lvalue; s->type = STRING_DATA; s->str_ptr = str_ptr; break; case FTOA: if (s->type == INT_DATA) { s->fvalue = s->lvalue; } str_ptr = &svalue [0]; (*str_ptr)("%lf") % s->fvalue; s->type = STRING_DATA; s->str_ptr = str_ptr; break; case TTOI: if (s->type == FLOAT_DATA) { s->lvalue = (int) s->fvalue; } s->type = INT_DATA; s->lvalue /= 10; break; case TTOF: if (s->type == INT_DATA) { s->fvalue = s->lvalue; } s->type = FLOAT_DATA; s->fvalue /= 10.0; break; case ITOT: if (s->type == FLOAT_DATA) { s->lvalue = (int) s->fvalue; } s->type = INT_DATA; s->lvalue *= 10; break; case FTOT: if (s->type == INT_DATA) { s->fvalue = s->lvalue; } s->type = FLOAT_DATA; s->fvalue *= 10.0; break; default: goto token_error; break; } return (true); error: exe->Error (String ("Conversion \"%s\" does not support %s Data") % Token_Name (CONVERT, cmd_ptr->token) % Data_Type (s->type)); return (false); token_error: exe->Error (String ("Conversion Token %d was Unrecognized") % cmd_ptr->token); return (false); string_error: exe->Error ("Illegal SUBSTR Function Syntax"); return (false); }