]> git.donarmstrong.com Git - lilypond.git/blob - lib/simple-file-storage.cc
release: 0.1.11
[lilypond.git] / lib / simple-file-storage.cc
1 /*
2   simple-file-storage.cc -- implement Simple_file_storage
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include <stdio.h>
10
11 #include "file-storage.hh"
12 #include "varray.hh"
13 #include "string.hh"
14 #include "warn.hh"
15
16 /**
17   Stupid but foolproof way of opening files.
18
19   TODO 
20   Should check IO status
21
22   This is of course a build it yourself version of mmap, so we should
23   have been using that... (see Mapped_file_storage) But we noticed
24   some problems with this (unexplained lexer crashes)
25   
26   [Some versions later] The crashes aren't caused by the mmap
27   code. But no reason to take it out, is there?  */
28
29 Simple_file_storage::Simple_file_storage(String s)
30 {
31   data_p_ =0;
32   /*
33     let's hope that "b" opens anything binary, and does not apply 
34     CR/LF translation
35     */
36   FILE * f = fopen (s.ch_C(), "rb");
37   if (!f) 
38     {
39       warning("can't open file\n");
40       return ;
41     }
42
43   int ret = fseek(f, 0, SEEK_END);
44   len_i_ = ftell(f);
45   rewind(f);
46   data_p_ = new char[len_i_+1];
47   data_p_[len_i_] = 0;
48   ret = fread(data_p_, sizeof(char), len_i_, f);
49
50      
51 #if 1 // ugh, \r\n -> \n translation
52   assert (ret==len_i_);
53 #endif
54   fclose(f);
55 }
56
57 char const*
58 Simple_file_storage::ch_C() const
59 {
60   return data_p_;
61 }
62
63 int
64 Simple_file_storage::length_i() const
65 {
66   return len_i_;
67 }
68     
69
70 Simple_file_storage::~Simple_file_storage()
71 {
72   delete []data_p_;
73 }