]> git.donarmstrong.com Git - lilypond.git/blob - lib/file-storage.cc
patch::: 0.1.10.jcn1: Re: Lilypond under Win/NT (fwd)
[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       {
60         warning( String( "can't open: " ) + name_str + String( ": " ) + strerror( errno )); 
61         return;
62       }
63
64     struct stat file_stat;
65     fstat( fildes_i_, &file_stat );
66     size_off_ = file_stat.st_size;
67     map();
68 }
69
70 void
71 Mapped_file_storage::unmap()
72 {
73     if ( data_caddr_ ) 
74       {
75         munmap( data_caddr_, size_off_ );
76         data_caddr_ = 0;
77         size_off_ = 0;
78       }
79 }
80
81 void
82 Mapped_file_storage::close()
83 {
84     unmap();
85     if ( fildes_i_ ) 
86       {
87         ::close( fildes_i_ );
88         fildes_i_ = 0;
89       }
90 }
91
92 int
93 Mapped_file_storage::length_i()const
94 {
95     return size_off_;
96 }
97
98 Mapped_file_storage::~Mapped_file_storage()
99 {
100     close();
101 }