]> git.donarmstrong.com Git - lilypond.git/blob - lib/file-storage.cc
release: 0.1.7
[lilypond.git] / lib / file-storage.cc
1 /*
2   file-storage.cc -- implement Mapped_file_storage
3
4   source file of the GNU LilyPond music typesetter
5
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
18
19
20
21 #include "string.hh"
22 #include "proto.hh"
23 #include "warn.hh"
24 #include "file-storage.hh"
25
26 Mapped_file_storage::Mapped_file_storage(String s)
27 {
28     data_caddr_ = 0;
29     fildes_i_ = 0;
30     size_off_ = 0;
31     open(s);
32 }
33
34 char const*
35 Mapped_file_storage::ch_C()const
36 {
37     return (char const*)data_caddr_;
38 }
39
40 void
41 Mapped_file_storage::map()
42 {
43     if ( fildes_i_ == -1 )
44         return;
45
46     data_caddr_ = (caddr_t)mmap( (void*)0, size_off_, PROT_READ, MAP_SHARED, fildes_i_, 0 );
47
48     if ( (int)data_caddr_ == -1 )
49         warning( String( "can't map: error no: " ) + strerror( errno ));
50 }
51
52
53 void
54 Mapped_file_storage::open(String name_str)
55 {
56     fildes_i_ = ::open( name_str, O_RDONLY );   
57             
58     if ( fildes_i_ == -1 ) {
59         warning( String( "can't open: " ) + name_str + String( ": " ) + strerror( errno )); 
60         return;
61     }
62
63     struct stat file_stat;
64     fstat( fildes_i_, &file_stat );
65     size_off_ = file_stat.st_size;
66     map();
67 }
68
69 void
70 Mapped_file_storage::unmap()
71 {
72     if ( data_caddr_ ) {
73         munmap( data_caddr_, size_off_ );
74         data_caddr_ = 0;
75         size_off_ = 0;
76     }
77 }
78
79 void
80 Mapped_file_storage::close()
81 {
82     unmap();
83     if ( fildes_i_ ) {
84         ::close( fildes_i_ );
85         fildes_i_ = 0;
86     }
87 }
88
89 int
90 Mapped_file_storage::length_i()const
91 {
92     return size_off_;
93 }
94
95 Mapped_file_storage::~Mapped_file_storage()
96 {
97     close();
98 }