//Assign from a string. Rational& Rational::operator=(const string input) { int pos = input.find('/'); //Find the '/', or exit if invalid. if(pos > input.size()) { cout << "Invalid program string input. Creating whole number fraction.\n"; makeThis(parseStr(input), 1); return *this; } //Send the string chunks off for processing. makeThis( parseStr(input.substr( 0, pos )), parseStr(input.substr( (pos + 1), (input.size() - (pos + 1)) )) ); return *this; }
const char *ScriptHandler::readStr() { end_status = END_NONE; current_variable.type = VAR_NONE; current_script = next_script; SKIP_SPACE( current_script ); char *buf = current_script; string_buffer[0] = '\0'; string_counter = 0; while(1){ parseStr(&buf); buf = checkComma(buf); string_counter += strlen(str_string_buffer); if (string_counter+1 >= STRING_BUFFER_LENGTH) errorAndExit("readStr: string length exceeds 2048 bytes."); strcat(string_buffer, str_string_buffer); if (buf[0] != '+') break; buf++; } next_script = buf; return string_buffer; }
ObjLoader::ObjLoader(std::string filename) { vec = NULL; nor = NULL; tex = NULL; col = NULL; vectors = new std::vector<vec3f*>(); normals = new std::vector<vec3f*>(); texture = new std::vector<vec2f*>(); triangle = new std::vector<tri*>(); //open file std::ifstream ifs(filename.c_str()); std::string temp; //std::cout<<"Objloader filename "<<filename<<std::endl; /** Every Line is either one vector * texture or face object so every line can be * parsed on its own. */ while(getline(ifs, temp)) { //debug //std::cout<<temp<<std::endl; parseStr(temp); } /** These function transform the data from * the std::vectors to arrays so they can be * pushed to the graphicscard */ makeVertexArray(); makeNormalArray(); makeTextureArray(); makeColorArray(); floatCount = vectors->size()*3; //printVertexArray(); }
static Exp *parseStrs(int left, char *sPtr[]) { if(left == 0) { return newOpn(O_Empty); } else { return newApp(newApp(newOpn(O_Cons), parseStr(*sPtr)), parseStrs(left - 1, sPtr + 1)); } }
/* * Helper functions for parseArgs(). */ static Exp *parseStr(char *cPtr) { if(*cPtr == '\0') { return newOpn(O_Empty); } else { return newApp(newApp(newOpn(O_Cons), newCon(C_Char, *cPtr)), parseStr(cPtr + 1)); } }
inline Val parseVal() { // Check value type switch(getC()) { case '{': return parseObj(); case '[': return parseArr(); case '"': return parseStr(); case 't': return parseBlnTrue(); case 'f': return parseBlnFalse(); case 'n': return parseNll(); } // Check if value is a number if(isNumStart(getC())) return parseNum(); throwError("Invalid value", std::string{"No match for values beginning with `"} + getC() + "`"); return Val{Nll{}}; }
void getSentences(const char* input_str, char** ret) { char* temp = input_str; char* temp2 = input_str; int len = strlen(input_str); int count = 0; // will be length int idx = 0; for (int i = 0; i < len; ++i) { temp++; if (ispunct(input_str[i])) { parseStr(temp2, ret, i - count, idx); count = i + 1; temp2 = temp; ++idx; } } ret[idx] = NULL; }
const char* ScriptHandler::readStr() { end_status = END_NONE; current_variable.type = VAR_NONE; current_script = next_script; SKIP_SPACE(current_script); const char* buf = current_script; string_buffer.trunc(0); while (1) { string_buffer += parseStr(&buf); buf = checkComma(buf); if (buf[0] != '+') break; buf++; } next_script = buf; return string_buffer; }
// basic parser function const char* ScriptHandler::readToken(bool no_kidoku) { current_script = next_script; const char* buf = current_script; end_status = END_NONE; current_variable.type = VAR_NONE; text_flag = false; SKIP_SPACE(buf); if (!no_kidoku) markAsKidoku(buf); readTokenTop: string_buffer.trunc(0); char ch = *buf; if (ch == ';') { // comment addStrBuf(ch); do { ch = *++buf; addStrBuf(ch); } while (ch != 0x0a && ch != '\0'); } else if (ch & 0x80 || (ch >= '0' && ch <= '9') || ch == '@' || ch == '\\' || ch == '/' || ch == '%' || ch == '?' || ch == '$' || ch == '[' || ch == '(' || ch == '!' || ch == '#' || ch == ',' || ch == '"') { // text if (ch != '!' and !warned_unmarked) { // errorWarning("unmarked text found"); //Mion: stop warnings, for compatibility // TODO: make this more robust; permit only !-directives // warned_unmarked = true; } bool loop_flag = true; bool ignore_click_flag = false; do { char bytes = file_encoding->NextCharSize(buf); if (bytes > 1) { if (textgosub_flag && !ignore_click_flag && checkClickstr(buf)) loop_flag = false; string_buffer.add(buf, bytes); buf += bytes; SKIP_SPACE(buf); ch = *buf; } else { if (ch == '%' || ch == '?') { addIntVariable(&buf); } else if (ch == '$') { addStrVariable(&buf); } else { if (textgosub_flag && !ignore_click_flag && checkClickstr(buf)) loop_flag = false; string_buffer += ch; buf++; ignore_click_flag = false; if (ch == '_') ignore_click_flag = true; } // CHECKME: why do we ignore text markers here? if (isadigit(ch) && (isawspace(*buf) || *buf == file_encoding->TextMarker()) && (string_buffer.length() % 2)) { string_buffer += ' '; } ch = *buf; if (ch == 0x0a || ch == '\0' || !loop_flag || ch == file_encoding->TextMarker()) { break; } SKIP_SPACE(buf); ch = *buf; } } while (ch != 0x0a && ch != '\0' && loop_flag && ch != file_encoding->TextMarker()) /*nop*/; if (loop_flag && ch == 0x0a && !(textgosub_flag && linepage_flag)) { string_buffer += ch; if (!no_kidoku) markAsKidoku(buf++); } text_flag = true; } else if (ch == file_encoding->TextMarker()) { ch = *++buf; while (ch != file_encoding->TextMarker() && ch != 0x0a && ch != '\0') { if ((ch == '\\' || ch == '@') && (textgosub_flag || buf[1] == 0x0a || buf[1] == 0)) { string_buffer += *buf++; ch = *buf; break; } // Interpolate expressions. if (ch == '{' && (buf[1] == '%' || buf[1] == '$' || buf[1] == '?')) { const char* start = buf + 1; while (*buf && *buf != '\n' && *buf != '}') ++buf; if (*buf != '}') errorAndExit("interpolation missing }"); pstring var_expr(start, buf++ - start); const char* var_iter = var_expr; if (var_expr[0] == '$') { pstring val = parseStr(&var_iter); if (val[0] == file_encoding->TextMarker()) val.remove(0, 1); string_buffer += val; } else { string_buffer += stringFromInteger(parseInt(&var_iter), -1); } ch = *buf; continue; } if (file_encoding->UseTags() && ch == '~' && (ch = *++buf) != '~') { while (ch != '~') { int l; string_buffer += file_encoding->TranslateTag(buf, l); buf += l; ch = *buf; } ch = *++buf; continue; } int bytes; // NOTE: we don't substitute ligatures at this stage. string_buffer += file_encoding->Encode(file_encoding->DecodeChar(buf, bytes)); buf += bytes; ch = *buf; } if (ch == file_encoding->TextMarker() && !textgosub_flag) ++buf; if (ch == 0x0a && !(textgosub_flag && linepage_flag)) { string_buffer += ch; if (!no_kidoku) markAsKidoku(buf++); } text_flag = true; } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_') { // command do { if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A'; string_buffer += ch; ch = *++buf; } while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_'); } else if (ch == '*') { // label return readLabel(); } else if (ch == '~' || ch == 0x0a || ch == ':') { string_buffer += ch; if (!no_kidoku) markAsKidoku(buf++); } else if (ch != '\0') { fprintf(stderr, "readToken: skip unknown heading character %c (%x)\n", ch, ch); buf++; goto readTokenTop; } if (text_flag) next_script = buf; else next_script = checkComma(buf); return string_buffer; }
pstring ScriptHandler::parseStr(const char** buf) { SKIP_SPACE(*buf); if (**buf == '(') { // (foo) bar baz : apparently returns bar if foo has been // viewed, baz otherwise. // (Rather like a trigram implicitly using "fchk") (*buf)++; pstring s = parseStr(buf); SKIP_SPACE(*buf); if ((*buf)[0] != ')') errorAndExit("parseStr: ) is not found."); (*buf)++; if (file_log.find(s)) { s = parseStr(buf); parseStr(buf); } else { parseStr(buf); s = parseStr(buf); } current_variable.type |= VAR_CONST; return s; } else if (**buf == '$') { (*buf)++; int no = parseInt(buf); current_variable.type = VAR_STR; current_variable.var_no = no; return getVariableData(no).str; } else if (**buf == '"') { (*buf)++; const char* const start = *buf; int len = 0; while (**buf != '"' && **buf != 0x0a) { ++len; (*buf)++; } if (**buf == '"') (*buf)++; current_variable.type |= VAR_CONST; return pstring(start, len); } else if (**buf == file_encoding->TextMarker()) { pstring s(file_encoding->TextMarker()); (*buf)++; char ch = **buf; while (ch != file_encoding->TextMarker() && ch != 0x0a && ch != '\0') { if (file_encoding->UseTags() && ch == '~' && (ch = *++ (*buf)) != '~') { while (ch != '~') { int l; s += file_encoding->TranslateTag(*buf, l); *buf += l; ch = **buf; } ch = *++ (*buf); continue; } int bytes; s += file_encoding->Encode(file_encoding->DecodeChar(*buf, bytes)); *buf += bytes; ch = **buf; } if (**buf == file_encoding->TextMarker()) (*buf)++; current_variable.type |= VAR_CONST; return s; } else if (**buf == '#') { // for color pstring s(*buf, 7); *buf += 7; current_variable.type = VAR_NONE; return s; } else if (**buf == '*') { // label pstring s(*(*buf)++); SKIP_SPACE(*buf); char ch = **buf; while((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_') { if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A'; s += ch; ch = *++(*buf); } current_variable.type |= VAR_CONST | VAR_LABEL; return s; } else { // bareword char ch; pstring alias_buf; bool first_flag = true; while (1) { ch = **buf; if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_') { if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A'; first_flag = false; alias_buf += ch; } else if (ch >= '0' && ch <= '9') { if (first_flag) errorAndExit("parseStr: number is not allowed for the " "first letter of str alias."); first_flag = false; alias_buf += ch; } else break; (*buf)++; } if (!alias_buf) { current_variable.type = VAR_NONE; return ""; } stralias_t::iterator a = str_aliases.find(alias_buf); if (a == str_aliases.end()) { current_variable.type = VAR_NONE; return alias_buf; } current_variable.type |= VAR_CONST; return a->second; } }
void ScriptHandler::parseStr( char **buf ) { SKIP_SPACE( *buf ); if ( **buf == '(' ){ (*buf)++; parseStr(buf); SKIP_SPACE( *buf ); if ( (*buf)[0] != ')' ) errorAndExit("parseStr: missing ')'."); (*buf)++; if ( findAndAddLog( log_info[FILE_LOG], str_string_buffer, false ) ){ parseStr(buf); char *tmp_buf = new char[ strlen( str_string_buffer ) + 1 ]; strcpy( tmp_buf, str_string_buffer ); parseStr(buf); strcpy( str_string_buffer, tmp_buf ); delete[] tmp_buf; } else{ parseStr(buf); parseStr(buf); } current_variable.type |= VAR_CONST; } else if ( **buf == '$' ){ (*buf)++; int no = parseInt(buf); VariableData &vd = getVariableData(no); if ( vd.str ) strcpy( str_string_buffer, vd.str ); else str_string_buffer[0] = '\0'; current_variable.type = VAR_STR; current_variable.var_no = no; } else if ( **buf == '"' ){ int c=0; (*buf)++; while ( **buf != '"' && **buf != 0x0a ) str_string_buffer[c++] = *(*buf)++; str_string_buffer[c] = '\0'; if ( **buf == '"' ) (*buf)++; current_variable.type |= VAR_CONST; } #ifdef ENABLE_1BYTE_CHAR else if ( **buf == '`' ){ int c=0; str_string_buffer[c++] = *(*buf)++; while ( **buf != '`' && **buf != 0x0a ) str_string_buffer[c++] = *(*buf)++; str_string_buffer[c] = '\0'; if ( **buf == '`' ) (*buf)++; current_variable.type |= VAR_CONST; end_status |= END_1BYTE_CHAR; } #endif else if ( **buf == '#' ){ // for color for ( int i=0 ; i<7 ; i++ ) str_string_buffer[i] = *(*buf)++; str_string_buffer[7] = '\0'; current_variable.type = VAR_NONE; } else if ( **buf == '*' ){ // label int c=0; str_string_buffer[c++] = *(*buf)++; SKIP_SPACE(*buf); char ch = **buf; while((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '_'){ if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A'; str_string_buffer[c++] = ch; ch = *++(*buf); } str_string_buffer[c] = '\0'; current_variable.type |= VAR_CONST; } else{ // str alias char ch, alias_buf[512]; int alias_buf_len = 0; bool first_flag = true; while(1){ if ( alias_buf_len == 511 ) break; ch = **buf; if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' ){ if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A'; first_flag = false; alias_buf[ alias_buf_len++ ] = ch; } else if ( ch >= '0' && ch <= '9' ){ if ( first_flag ) errorAndExit("parseStr: number is not allowed for the first letter of str alias."); first_flag = false; alias_buf[ alias_buf_len++ ] = ch; } else break; (*buf)++; } alias_buf[alias_buf_len] = '\0'; if ( alias_buf_len == 0 ){ str_string_buffer[0] = '\0'; current_variable.type = VAR_NONE; return; } Alias *p_str_alias = root_str_alias.next; while( p_str_alias ){ if ( !strcmp( p_str_alias->alias, (const char*)alias_buf ) ){ strcpy( str_string_buffer, p_str_alias->str ); break; } p_str_alias = p_str_alias->next; } if ( !p_str_alias ){ printf("can't find str alias for %s...\n", alias_buf ); exit(-1); } current_variable.type |= VAR_CONST; } }
/*! \param vLine */ bool kScriptCommandObject::setCommandLine(string vLine) { CommandLine = vLine; return parseStr(vLine); }