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