bool ReadValue(AnyValue& value,std::istream& in,const std::string& delims) { EatWhitespace(in); if(!in) { printf("ReadValue: hit end of file\n"); return false; } if(in.peek() == '"') { //beginning of string std::string str; if(!InputQuotedString(in,str)) { printf("ReadValue: unable to read quoted string\n"); return false; } value = str; return true; } else if(in.peek() == '\'') { //character: TODO: translate escapes properly in.get(); char c=in.get(); value = c; char end=in.get(); if(end != '\'') { printf("ReadValue: character not delimited properly\n"); return false; } return true; } else { std::string str; if(delims.empty()) in >> str; else { while(in && delims.find(in.peek()) == std::string::npos && !isspace(in.peek())) { str += in.get(); } } if(str.empty()) { printf("ReadValue: read an empty string\n"); return false; } if(IsValidInteger(str.c_str())) { int val; std::stringstream ss(str); ss>>val; value = val; return true; }
bool SafeInputString(std::istream& in, std::string& str) { //first eat up white space str.erase(); while(in && isspace(in.peek())) in.get(); if(!in) return false; if(in.peek() == EOF) return false; if(in.peek() == '\"') return InputQuotedString(in,str); else { //read until new whitespace while(in) { char c = in.get(); if(isspace(c) || c==EOF) return true; str += c; } if(in.eof()) return true; return false; } }
bool SafeInputString(std::istream& in, char* str,int n) { EatWhitespace(in); if(!in) return false; if(in.peek() == EOF) return false; if(in.peek() == '\"') return InputQuotedString(in,str,n); else { //read until new whitespace int i; for(i=0;i<n;i++) { str[i] = in.get(); if(isspace(str[i]) || in.eof()) { str[i] = 0; return true; } if(!in) return false; } //buffer overflowed return false; } }