]> git.donarmstrong.com Git - lilypond.git/commitdiff
lilypond-0.1.7
authorfred <fred>
Mon, 11 Aug 1997 00:01:49 +0000 (00:01 +0000)
committerfred <fred>
Mon, 11 Aug 1997 00:01:49 +0000 (00:01 +0000)
lib/file-storage.cc [new file with mode: 0644]
lib/include/file-storage.hh [new file with mode: 0644]
lib/include/source-file.hh

diff --git a/lib/file-storage.cc b/lib/file-storage.cc
new file mode 100644 (file)
index 0000000..16bee29
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+  file-storage.cc -- implement Mapped_file_storage
+
+  source file of the GNU LilyPond music typesetter
+
+  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+  Jan Nieuwenhuizen <jan@digicash.com>
+*/
+#include <sys/types.h>         // open, mmap
+#include <sys/stat.h>          // open
+#include <sys/mman.h>          // mmap
+#include <limits.h>            // INT_MAX
+#include <fcntl.h>             // open 
+#include <unistd.h>            // close, stat
+#include <stdio.h>             // fdopen
+#include <string.h>            // strerror
+#include <errno.h>             // 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 (file)
index 0000000..8500462
--- /dev/null
@@ -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 <hanwen@stack.nl>
+*/
+
+
+#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
index 6fdcc5d88a56dd6c2e7200df10d2fcaf2fe68b79..39cd533e410cdc6b7a011c78ca259ae8f464f51d 100644 (file)
@@ -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 //