#ifndef CLASS_TEXT_H // guard to prohibit multiple inclusions #define CLASS_TEXT_H // class_text.h (c) 2000 Kari Laitinen // 22.11.2000 File created. // 06.03.2001 Last modification. #include #include class Text { protected: vector text_lines ; public: Text( char given_text_lines[] = "" ) ; void append_lines( char new_text_lines[] ) ; void delete_all_lines() ; void edit_text() ; void print_with_indentation( ostream& output_stream ) ; void print_with_line_numbers( ostream& output_stream ) ; void store_to_binary_file( ostream& output_file ) ; // The program did not compile if the following argument // was declared with type ifstream& // It compiles with definition ifstream void load_from_binary_file( istream& input_file ) ; } ; Text::Text( char given_text_lines[] ) { stringstream text_lines_as_stream ; text_lines_as_stream << given_text_lines ; string line_from_stream ; // getline seems to work this way: it returns true // as long as there are lines available in the stream. while ( getline( text_lines_as_stream, line_from_stream ) ) { text_lines.push_back( line_from_stream ) ; } } void Text::append_lines( char new_text_lines[] ) { stringstream text_lines_as_stream ; text_lines_as_stream << new_text_lines ; string line_from_stream ; // getline seems to work this way: it returns true // as long as there are lines available in the stream. while ( getline( text_lines_as_stream, line_from_stream ) ) { text_lines.push_back( line_from_stream ) ; } } void Text::delete_all_lines() { text_lines.clear() ; } void Text::edit_text() { print_with_line_numbers( cout ) ; cout << "\n This is a simple Text object editor." "\n a = append lines, d = delete line : " ; string command_from_user = "???" ; getline( cin, command_from_user ) ; if ( command_from_user[ 0 ] == 'A' || command_from_user[ 0 ] == 'a' ) { cout << "\n APPENDING LINES: an empty line exits appending.\n\n" ; bool user_wants_to_quit_appending = false ; while ( user_wants_to_quit_appending == false ) { print_with_line_numbers( cout ) ; cout << setw( 2 ) << ( text_lines.size() + 1 ) << " " ; string new_text_line ; getline( cin, new_text_line ) ; if ( new_text_line.length() > 0 ) { text_lines.push_back( new_text_line ) ; } else { user_wants_to_quit_appending = true ; } } } else if ( command_from_user[ 0 ] == 'D' || command_from_user[ 0 ] == 'd' ) { cout << "\n Line number to delete: " ; string line_number_as_string ; getline( cin, line_number_as_string ) ; unsigned int line_number_to_delete = string_to_integer( line_number_as_string ) ; if ( line_number_to_delete > 0 ) { vector::iterator line_iterator = text_lines.begin() ; unsigned int line_counter = 1 ; while ( line_counter < line_number_to_delete && line_counter < text_lines.size() ) { line_iterator ++ ; line_counter ++ ; } if ( line_number_to_delete <= text_lines.size() ) { text_lines.erase( line_iterator ) ; } } print_with_line_numbers( cout ) ; } } void Text::print_with_indentation( ostream& output_stream ) { output_stream << "\n" ; unsigned int line_index = 0 ; while ( line_index < text_lines.size() ) { output_stream << " " << text_lines[ line_index ] << "\n" ; line_index ++ ; } } void Text::print_with_line_numbers( ostream& output_stream ) { output_stream << "\n" ; unsigned int line_index = 0 ; while ( line_index < text_lines.size() ) { output_stream << setw( 2 ) << ( line_index + 1 ) << " " << text_lines[ line_index ] << "\n" ; line_index ++ ; } } void Text::store_to_binary_file( ostream& output_file ) { int number_of_lines = text_lines.size() ; output_file.write( (char*) &number_of_lines, sizeof( int ) ) ; for ( int line_index = 0 ; line_index < number_of_lines ; line_index ++ ) { int line_length = text_lines[ line_index ].length() ; output_file.write( (char*) &line_length, sizeof( int ) ) ; output_file.write( text_lines[ line_index ].c_str(), line_length ) ; } } void Text::load_from_binary_file( istream& input_file ) { text_lines.clear() ; int number_of_lines_to_read ; input_file.read( (char*) &number_of_lines_to_read, sizeof( int ) ) ; for ( int line_index = 0 ; line_index < number_of_lines_to_read ; line_index ++ ) { int line_length ; input_file.read( (char*) &line_length, sizeof( int ) ) ; char* line_from_file = new char[ line_length + 2 ] ; input_file.read( line_from_file, line_length ) ; line_from_file[ line_length ] = 0 ; // making a C-string text_lines.push_back( string( line_from_file ) ) ; delete [] line_from_file ; } } #endif // end of include guard