]> git.donarmstrong.com Git - lilypond.git/blob - lib/simple-file-storage.cc
b80724d9870684529a149922e7638d2048a72758
[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 "simple-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   /*
34     let's hope that "b" opens anything binary, and does not apply
35     CR/LF translation
36     */
37   FILE * f =  (s.length_i ()) ?  fopen (s.ch_C (), "rb") : stdin;
38
39   if (!f)
40     {
41       warning (_ ("can't open file `") + s + "'");
42       return ;
43     }
44
45   int ret = fseek (f, 0, SEEK_END);
46   len_i_ = ftell (f);
47   rewind (f);
48   data_p_ = new char[len_i_+1];
49   data_p_[len_i_] = 0;
50   ret = fread (data_p_, sizeof (char), len_i_, f);
51
52
53   if  (ret!=len_i_)
54     warning (_ ("Huh? got ") + String (ret) + _ (", expected ")
55              + String (len_i_) + _ (" characters"));
56
57   if (f != stdin)
58     fclose (f);
59 }
60
61 char const*
62 Simple_file_storage::ch_C () const
63 {
64   return data_p_;
65 }
66
67 int
68 Simple_file_storage::length_i () const
69 {
70   return len_i_;
71 }
72
73
74 Simple_file_storage::~Simple_file_storage ()
75 {
76   delete []data_p_;
77 }