]> git.donarmstrong.com Git - lilypond.git/blob - lib/simple-file-storage.cc
release: 0.1.9
[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 use obstack. 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   */
27
28 Simple_file_storage::Simple_file_storage(String s)
29 {
30     data_p_ =0;
31     FILE * f = fopen ( s.ch_C(), "r");
32     if ( !f ) 
33       {
34         warning("can't open file\n");
35         return ;
36       }
37
38     int ret = fseek( f, 0, SEEK_END);
39     len_i_ = ftell(f);
40     rewind(f);
41     data_p_ = new char[len_i_+1];
42     data_p_[len_i_] = 0;
43     ret = fread(data_p_, sizeof(char), len_i_, f);
44     assert (ret==len_i_);
45     fclose(f);
46 }
47
48 char const*
49 Simple_file_storage::ch_C() const
50 {
51     return data_p_;
52 }
53
54 int
55 Simple_file_storage::length_i()const
56 {
57     return len_i_;
58 }
59     
60
61 Simple_file_storage::~Simple_file_storage()
62 {
63     delete []data_p_;
64 }