/////////////////////////////////////////////////////////////// // Kenneth Ma // CSE143 Summer 98 // Homework 2 // MY Screen client! /////////////////////////////////////////////////////////////// #include "screen.h" #include #include #include const int BUFLENGTH = 80; const int CMDLENGTH = 10; void fatalError( char msg[] ) { cerr << msg << endl; exit( -1 ); } /* readChar expects to read in something of the form 'c' where c is some * arbitrary character. it ignores all characters before the first ' */ char readChar( istream& is ) { char ch; char ret; while( is.get( ch ) && (ch != '\'') ); if( !is ) { fatalError( "Bad char" ); } is.get( ret ); is.get( ch ); if( ch != '\'' ) { fatalError( "Bad char" ); } return ret; } /* readText reads in text of the form "text" where text is some arbitrary * character string */ bool readText( istream& is, char buf[] ) { char ch; int idx = 0; while( is.get( ch ) && (ch != '"') ); while( is.get( ch ) ) { if( ch == '"' ) { buf[idx] = '\0'; return true; } else { buf[ idx++ ] = ch; } } return false; } void printHelp(){ cerr << "Valid commands:" << endl; cerr << "fill 'c'" << endl; cerr << "\t" << "Fill the entire screen with the character 'c'." << endl; cerr << "hline 'c' " << endl; cerr << "\t" << "Draw a line from (,) to (+-1,)," << endl; cerr << "\t" << "made entirely out of the character 'c'." << endl; cerr << "vline 'c' " << endl; cerr << "\t" << "Draw a line from (,) to (<,row>+-1)," << endl; cerr << "\t" << "made entirely out of the character 'c'." << endl; cerr << "rectangle 'c' " << endl; cerr << "\t" << "Draw a rectangle from (,) to" << endl; cerr << "\t" << "(+-1,+-1), filled with the char acter 'c'. " << endl; cerr << "text \"some text to write\" " << endl; cerr << "\t" << "Write the text in the quotes starting at position (,)." << endl; cerr << "ctext \"some text to write\" " << endl; cerr << "\t" << "Place the text in quotes centered on row ." << endl; cerr << "load " << endl; cerr << "\t" << "Replace the contents of the screen with the contents of ." << endl; cerr << "\t" << " is given with no quotes, such as load BLAH.SCREEN" << endl; cerr << "save " << endl; cerr << "\t" << "Save the contents of the screen to a file given by . " << endl; cerr << "print " << endl; cerr << "\t" << "Print the screen to the console " << endl; cerr << "quit " << endl; cerr << "\t" << "Quit the program " << endl; } int main( void ) { char cmd[ CMDLENGTH ]; char buf[ BUFLENGTH ]; int col, row, width, height; char fill; Screen screen; while( cin >> cmd ) { if( strcmp( cmd, "fill" ) == 0 ) { fill = readChar( cin ); screen.fill( fill ); } else if( strcmp( cmd, "hline" ) == 0 ) { cin >> col >> row >> width; fill = readChar( cin ); if( !screen.horizontalLine( col, row, width, fill ) ) { cerr << "Error." << endl; } } else if( strcmp( cmd, "vline" ) == 0 ) { cin >> col >> row >> width; fill = readChar( cin ); if( !screen.verticalLine( col, row, width, fill ) ) { cerr << "Error." << endl; } } else if( strcmp( cmd, "rectangle" ) == 0 ) { cin >> col >> row >> width >> height; fill = readChar( cin ); if( !screen.rectangle( col, row, width, height, fill ) ) { cerr << "Error." << endl; } } else if( strcmp( cmd, "text" ) == 0 ) { cin >> col >> row; readText( cin, buf ); if( !screen.putText(col, row, buf) ){ cerr << "Error." << endl; } } else if( strcmp( cmd, "ctext" ) == 0 ) { cin >> row; readText( cin, buf ); if( !screen.putCenteredText(row, buf) ){ cerr << "Error." << endl; } } else if( strcmp( cmd, "load" ) == 0 ) { cin >> buf; ifstream inFile(buf, ios::nocreate | ios::in); // If the file has been opened, then read it in if( inFile.is_open() ){ screen.read(inFile); // otherwise, it's an error } else { cerr << "Error loading \"" << buf << "\"" << endl; } inFile.close(); } else if( strcmp( cmd, "save" ) == 0 ) { cin >> buf; ofstream outFile(buf); // If the file has been opened, then print to it if( outFile.is_open() ){ screen.print(outFile); // otherwise, it's an error } else { cerr << "Error saving to \"" << buf << "\"" << endl; } } else if( strcmp( cmd, "print" ) == 0 ) { screen.print(cout); } else if( strcmp( cmd, "quit" ) == 0 ) { exit(0); } else if( strcmp( cmd, "help" ) == 0 ) { printHelp(); } else { // Tell the user the error of their ways cerr << "\"" << cmd << "\" is not a valid command" << endl; cerr << "Type \"help\" for a complete list of commands" << endl; } } return 0; }