diff options
Diffstat (limited to 'cpp/demo/Freeze/library/Parser.cpp')
-rw-r--r-- | cpp/demo/Freeze/library/Parser.cpp | 197 |
1 files changed, 49 insertions, 148 deletions
diff --git a/cpp/demo/Freeze/library/Parser.cpp b/cpp/demo/Freeze/library/Parser.cpp index c884b42fd6e..257ac8ca43b 100644 --- a/cpp/demo/Freeze/library/Parser.cpp +++ b/cpp/demo/Freeze/library/Parser.cpp @@ -40,10 +40,9 @@ Parser::usage() "shutdown Shut the library server down.\n"; } -ParserPtr -Parser::createParser(const LibraryPrx& library) +Parser::Parser(const LibraryPrx& library) : + _library(library) { - return new Parser(library); } void @@ -354,114 +353,73 @@ Parser::shutdown() void Parser::getInput(char* buf, int& result, int maxSize) { - if(!_commands.empty()) +#ifdef HAVE_READLINE + + const char* prompt = parser->getPrompt(); + char* line = readline(const_cast<char*>(prompt)); + if(!line) { - if(_commands == ";") - { - result = 0; - } - else - { -#if defined(_MSC_VER) && !defined(_STLP_MSVC) - // COMPILERBUG: Stupid Visual C++ defines min and max as macros - result = _MIN(maxSize, static_cast<int>(_commands.length())); -#else - result = min(maxSize, static_cast<int>(_commands.length())); -#endif - strncpy(buf, _commands.c_str(), result); - _commands.erase(0, result); - if(_commands.empty()) - { - _commands = ";"; - } - } + result = 0; } - else if(isatty(fileno(yyin))) + else { -#ifdef HAVE_READLINE - - const char* prompt = parser->getPrompt(); - char* line = readline(const_cast<char*>(prompt)); - if(!line) + if(*line) + { + add_history(line); + } + + result = strlen(line) + 1; + if(result > maxSize) { + free(line); + error("input line too long"); result = 0; } else { - if(*line) - { - add_history(line); - } - - result = strlen(line) + 1; - if(result > maxSize) - { - free(line); - error("input line too long"); - result = 0; - } - else - { - strcpy(buf, line); - strcat(buf, "\n"); - free(line); - } + strcpy(buf, line); + strcat(buf, "\n"); + free(line); } - + } + #else - cout << parser->getPrompt() << flush; + cout << parser->getPrompt() << flush; - string line; - while(true) + string line; + while(true) + { + char c = static_cast<char>(getc(yyin)); + if(c == EOF) { - char c = static_cast<char>(getc(yyin)); - if(c == EOF) + if(line.size()) { - if(line.size()) - { - line += '\n'; - } - break; + line += '\n'; } + break; + } - line += c; + line += c; - if(c == '\n') - { - break; - } - } - - result = static_cast<int>(line.length()); - if(result > maxSize) - { - error("input line too long"); - buf[0] = EOF; - result = 1; - } - else + if(c == '\n') { - strcpy(buf, line.c_str()); + break; } + } -#endif + result = static_cast<int>(line.length()); + if(result > maxSize) + { + error("input line too long"); + buf[0] = EOF; + result = 1; } else { - if(((result = static_cast<int>(fread(buf, 1, maxSize, yyin))) == 0) && ferror(yyin)) - { - error("input in flex scanner failed"); - buf[0] = EOF; - result = 1; - } + strcpy(buf, line.c_str()); } -} - -void -Parser::nextLine() -{ - _currentLine++; +#endif } void @@ -473,8 +431,6 @@ Parser::continueLine() const char* Parser::getPrompt() { - assert(_commands.empty() && isatty(fileno(yyin))); - if(_continue) { _continue = false; @@ -489,14 +445,7 @@ Parser::getPrompt() void Parser::error(const char* s) { - if(_commands.empty() && !isatty(fileno(yyin))) - { - cerr << _currentFile << ':' << _currentLine << ": " << s << endl; - } - else - { - cerr << "error: " << s << endl; - } + cerr << "error: " << s << endl; _errors++; } @@ -509,14 +458,7 @@ Parser::error(const string& s) void Parser::warning(const char* s) { - if(_commands.empty() && !isatty(fileno(yyin))) - { - cerr << _currentFile << ':' << _currentLine << ": warning: " << s << endl; - } - else - { - cerr << "warning: " << s << endl; - } + cerr << "warning: " << s << endl; } void @@ -526,7 +468,7 @@ Parser::warning(const string& s) } int -Parser::parse(FILE* file, bool debug) +Parser::parse(bool debug) { extern int yydebug; yydebug = debug ? 1 : 0; @@ -535,46 +477,10 @@ Parser::parse(FILE* file, bool debug) parser = this; _errors = 0; - _commands.empty(); - yyin = file; + yyin = stdin; assert(yyin); - _currentFile = ""; - _currentLine = 0; - _continue = false; - nextLine(); - - _foundBooks.clear(); - _current = _foundBooks.end(); - - int status = yyparse(); - if(_errors) - { - status = EXIT_FAILURE; - } - - parser = 0; - return status; -} - -int -Parser::parse(const string& commands, bool debug) -{ - extern int yydebug; - yydebug = debug ? 1 : 0; - - assert(!parser); - parser = this; - - _errors = 0; - _commands = commands; - assert(!_commands.empty()); - yyin = 0; - - _currentFile = "<command line>"; - _currentLine = 0; _continue = false; - nextLine(); _foundBooks.clear(); _current = _foundBooks.end(); @@ -588,8 +494,3 @@ Parser::parse(const string& commands, bool debug) parser = 0; return status; } - -Parser::Parser(const LibraryPrx& library) : - _library(library) -{ -} |