lilypond-1.4.3
[lilypond.git] / flower / simple-file-storage.cc
blob8feb1a5843312fa6b91268ef867321f4953c9ad4
1 /*
2 simple-file-storage.cc -- implement Simple_file_storage
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <stdio.h>
11 #ifndef SEEK_END
12 #define SEEK_END 2
13 #endif
15 #include "simple-file-storage.hh"
16 #include "array.hh"
17 #include "string.hh"
18 #include "warn.hh"
20 void
21 Simple_file_storage::load_stdin ()
23 len_i_ = 0;
25 int c;
26 Array<char> ch_arr;
27 while ((c = fgetc (stdin)) != EOF)
28 ch_arr.push (c);
29 len_i_ = ch_arr.size ();
30 data_p_ = ch_arr.remove_array_p ();
33 void
34 Simple_file_storage::load_file (String s)
37 let's hope that "b" opens anything binary, and does not apply
38 CR/LF translation
40 FILE * f = fopen (s.ch_C (), "rb");
42 if (!f)
44 warning (_f ("can't open file: `%s'", s));
45 return ;
48 int ret = fseek (f, 0, SEEK_END);
49 len_i_ = ftell (f);
50 rewind (f);
51 data_p_ = new char[len_i_+1];
52 data_p_[len_i_] = 0;
53 ret = fread (data_p_, sizeof (char), len_i_, f);
55 if (ret!=len_i_)
56 warning (_f ("Huh? Got %d, expected %d characters", ret, len_i_));
58 fclose (f);
61 /**
62 Stupid but foolproof way of opening files.
64 TODO
65 Should check IO status
67 This is of course a build it yourself version of mmap, so we should
68 have been using that..., but this is simple & portable
72 Simple_file_storage::Simple_file_storage (String s)
74 data_p_ = 0;
75 len_i_ = 0;
77 if ((s == "-"))
78 load_stdin ();
79 else
80 load_file (s);
83 char const*
84 Simple_file_storage::ch_C () const
86 return data_p_;
89 int
90 Simple_file_storage::length_i () const
92 return len_i_;
96 Simple_file_storage::~Simple_file_storage ()
98 delete []data_p_;