From d59034a201765497c5be7846753b2f6674ba2902 Mon Sep 17 00:00:00 2001 From: fred Date: Mon, 11 Aug 1997 00:01:49 +0000 Subject: [PATCH] lilypond-0.1.7 --- lib/file-storage.cc | 98 +++++++++++++++++++++++++++++++++++++ lib/include/file-storage.hh | 62 +++++++++++++++++++++++ lib/include/source-file.hh | 31 ++++++------ 3 files changed, 174 insertions(+), 17 deletions(-) create mode 100644 lib/file-storage.cc create mode 100644 lib/include/file-storage.hh diff --git a/lib/file-storage.cc b/lib/file-storage.cc new file mode 100644 index 0000000000..16bee29c68 --- /dev/null +++ b/lib/file-storage.cc @@ -0,0 +1,98 @@ +/* + file-storage.cc -- implement Mapped_file_storage + + source file of the GNU LilyPond music typesetter + + (c) 1997 Han-Wen Nienhuys + Jan Nieuwenhuizen +*/ +#include // open, mmap +#include // open +#include // mmap +#include // INT_MAX +#include // open +#include // close, stat +#include // fdopen +#include // strerror +#include // errno + + + +#include "string.hh" +#include "proto.hh" +#include "warn.hh" +#include "file-storage.hh" + +Mapped_file_storage::Mapped_file_storage(String s) +{ + data_caddr_ = 0; + fildes_i_ = 0; + size_off_ = 0; + open(s); +} + +char const* +Mapped_file_storage::ch_C()const +{ + return (char const*)data_caddr_; +} + +void +Mapped_file_storage::map() +{ + if ( fildes_i_ == -1 ) + return; + + data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 ); + + if ( (int)data_caddr_ == -1 ) + warning( String( "can't map: error no: " ) + strerror( errno )); +} + + +void +Mapped_file_storage::open(String name_str) +{ + fildes_i_ = ::open( name_str, O_RDONLY ); + + if ( fildes_i_ == -1 ) { + warning( String( "can't open: " ) + name_str + String( ": " ) + strerror( errno )); + return; + } + + struct stat file_stat; + fstat( fildes_i_, &file_stat ); + size_off_ = file_stat.st_size; + map(); +} + +void +Mapped_file_storage::unmap() +{ + if ( data_caddr_ ) { + munmap( data_caddr_, size_off_ ); + data_caddr_ = 0; + size_off_ = 0; + } +} + +void +Mapped_file_storage::close() +{ + unmap(); + if ( fildes_i_ ) { + ::close( fildes_i_ ); + fildes_i_ = 0; + } +} + +int +Mapped_file_storage::length_i()const +{ + return size_off_; +} + +Mapped_file_storage::~Mapped_file_storage() +{ + close(); +} diff --git a/lib/include/file-storage.hh b/lib/include/file-storage.hh new file mode 100644 index 0000000000..850046236c --- /dev/null +++ b/lib/include/file-storage.hh @@ -0,0 +1,62 @@ +/* + file-storage.hh -- declare File_storage, Mapped_file_storage, Simple_file_storage + + source file of the GNU LilyPond music typesetter + + (c) 1997 Han-Wen Nienhuys +*/ + + +#ifndef FILE_STORAGE_HH +#define FILE_STORAGE_HH + +#include "proto.hh" + +/** + store a file in-memory. + */ +class File_storage +{ +public: + virtual char const* ch_C()const=0; + virtual int length_i()const=0; + virtual ~File_storage(){} +}; + +/** + Use mmap to "copy" a file into memory + */ +class Mapped_file_storage:public File_storage +{ +public: + Mapped_file_storage(String); +protected: + virtual char const* ch_C()const; + virtual int length_i()const; + virtual ~Mapped_file_storage(); +private: + void open(String name); + void close(); + + void map(); + void unmap(); + int fildes_i_; + off_t size_off_; + caddr_t data_caddr_; +}; + +/** + read file char by char and copy into a malloc array. + */ +class Simple_file_storage : public File_storage +{ + char * data_p_; + int len_i_; +protected: + virtual char const*ch_C()const; + virtual int length_i()const; + virtual ~Simple_file_storage(); +public: + Simple_file_storage(String); +}; +#endif // FILE_STORAGE_HH diff --git a/lib/include/source-file.hh b/lib/include/source-file.hh index 6fdcc5d88a..39cd533e41 100644 --- a/lib/include/source-file.hh +++ b/lib/include/source-file.hh @@ -5,9 +5,13 @@ #ifndef SOURCE_FILE_HH #define SOURCE_FILE_HH -#include "fproto.hh" + +#include "proto.hh" #include "string.hh" + class istream; + + /// class for reading and mapping a file. class Source_file { @@ -17,26 +21,19 @@ public: Source_file( String filename_str_r ); virtual ~Source_file(); - char const* ch_C(); - virtual String error_str( char const* pos_ch_c_l ); + char const* ch_C()const; + virtual String error_str( char const* pos_ch_c_l )const; istream * istream_l(); - bool in_b( char const* pos_ch_c_l ); - off_t length_off(); - virtual int line_i( char const* pos_ch_c_l ); - String name_str(); - String file_line_no_str( char const* ch_c_l ); + bool in_b( char const* pos_ch_c_l )const; + int length_i()const; + virtual int line_i( char const* pos_ch_c_l )const; + String name_str()const; + String file_line_no_str( char const* ch_c_l )const; private: - void close(); - void map(); - void open(); - void unmap(); - - istream* istream_p_; - int fildes_i_; String name_str_; - off_t size_off_; - caddr_t data_caddr_; + istream* istream_p_; + File_storage * storage_p_; }; #endif // SOURCE_FILE_HH // -- 2.39.5