lilypond-1.1.47
[lilypond.git] / lib / file-storage.cc
blob849c27c994caa80be71d7508781c9f8f5509137d
1 /*
2 file-storage.cc -- implement Mapped_file_storage
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 Jan Nieuwenhuizen <jan@digicash.com>
8 */
9 #include <sys/types.h> // open, mmap
10 #include <sys/stat.h> // open
11 #include <sys/mman.h> // mmap
12 #include <limits.h> // INT_MAX
13 #include <fcntl.h> // open
14 #include <unistd.h> // close, stat
15 #include <stdio.h> // fdopen
16 #include <string.h> // strerror
17 #include <errno.h> // errno
21 #include "string.hh"
22 #include "proto.hh"
23 #include "warn.hh"
24 #include "file-storage.hh"
26 Mapped_file_storage::Mapped_file_storage(String s)
28 data_caddr_ = 0;
29 fildes_i_ = 0;
30 size_off_ = 0;
31 open(s);
34 char const*
35 Mapped_file_storage::ch_C()const
37 return (char const*)data_caddr_;
40 void
41 Mapped_file_storage::map()
43 if ( fildes_i_ == -1 )
44 return;
46 data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 );
48 if ( (int)data_caddr_ == -1 )
49 warning( String( "can't map: error no: " ) + strerror( errno ));
53 void
54 Mapped_file_storage::open(String name_str)
56 fildes_i_ = ::open( name_str, O_RDONLY );
58 if ( fildes_i_ == -1 )
60 warning( String( "can't open: " ) + name_str + String( ": " ) + strerror( errno ));
61 return;
64 struct stat file_stat;
65 fstat( fildes_i_, &file_stat );
66 size_off_ = file_stat.st_size;
67 map();
70 void
71 Mapped_file_storage::unmap()
73 if ( data_caddr_ )
75 munmap( data_caddr_, size_off_ );
76 data_caddr_ = 0;
77 size_off_ = 0;
81 void
82 Mapped_file_storage::close()
84 unmap();
85 if ( fildes_i_ )
87 ::close( fildes_i_ );
88 fildes_i_ = 0;
92 int
93 Mapped_file_storage::length_i()const
95 return size_off_;
98 Mapped_file_storage::~Mapped_file_storage()
100 close();