Main Page | Namespace List | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

fileio.h

Go to the documentation of this file.
00001 
00002 // Common Text Transformation Library
00003 // Copyright (C) 1997-2006 by Igor Kholodov. 
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the
00017 // Free Software Foundation, Inc.,
00018 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 // mailto:cttl@users.sourceforge.net
00021 // http://sourceforge.net/projects/cttl/
00023 
00031 // fileio.h
00032 
00033 #ifndef _CTTL_FILEIO_H_INCLUDED_
00034 #define _CTTL_FILEIO_H_INCLUDED_
00035 
00036 #include "../cttl/coreutils.h"
00037 #include <fstream> 
00038 
00044 namespace cttl {
00045 
00047 void file2string( const CTTL_STD_STRING& filename_, CTTL_STD_STRING& str_ )
00048 {
00049     // on windows platform \r\n are converted into single \n
00050     std::basic_ifstream< CTTL_STD_CHAR > fs( reinterpret_cast< char const* >( filename_.c_str() ) );
00051     str_.erase();
00052     str_.reserve( fs.rdbuf()->in_avail() );
00053     CTTL_STD_CHAR ch;
00054     while( fs.get( ch ) ) {
00055         if( str_.capacity() == str_.size() )
00056             str_.reserve( str_.capacity() * 3 );
00057         str_.append( 1, ch );
00058     }
00059 }
00060 
00062 CTTL_STD_STRING file2string( const CTTL_STD_STRING& filename_ )
00063 {
00064     CTTL_STD_STRING str_temp;
00065     file2string( filename_, str_temp );
00066     return str_temp;
00067 }
00068 
00070 void string2file( const CTTL_STD_STRING& filename_, const CTTL_STD_STRING& str_ )
00071 {
00072     // on windows platform single \n are converted into \r\n
00073     std::basic_ofstream< CTTL_STD_CHAR > file; 
00074     file.open( reinterpret_cast< char const* >( filename_.c_str() ), std::ios_base::trunc );
00075     file << str_;
00076     file.close(); 
00077 }
00078 
00079 }//namespace cttl
00080 
00081 
00106 #endif // _CTTL_FILEIO_H_INCLUDED_

Generated on Thu Nov 2 17:43:39 2006 for CTTL Utility Classes and Functions by  doxygen 1.3.9.1