lilypond-0.1.37
[lilypond.git] / lib / simple-file-storage.cc
blobe09bb1699ab1cc443af8bce0211ae9cfade0afef
1 /*
2 simple-file-storage.cc -- implement Simple_file_storage
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
9 #include <stdio.h>
11 #include "file-storage.hh"
12 #include "varray.hh"
13 #include "string.hh"
14 #include "warn.hh"
16 /**
17 Stupid but foolproof way of opening files.
19 TODO
20 Should check IO status
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)
26 [Some versions later] The crashes aren't caused by the mmap
27 code. But no reason to take it out, is there? */
29 Simple_file_storage::Simple_file_storage(String s)
31 data_p_ =0;
34 let's hope that "b" opens anything binary, and does not apply
35 CR/LF translation
37 FILE * f = (s.length_i ()) ? fopen (s.ch_C(), "rb") : stdin;
39 if (!f)
41 warning(_("can't open file `") + s + "'");
42 return ;
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);
53 if (ret!=len_i_)
54 warning (_("Huh? got ") + String(ret) + _(", expected ")
55 + String(len_i_) + _(" characters"));
57 if (f != stdin)
58 fclose(f);
61 char const*
62 Simple_file_storage::ch_C() const
64 return data_p_;
67 int
68 Simple_file_storage::length_i() const
70 return len_i_;
74 Simple_file_storage::~Simple_file_storage()
76 delete []data_p_;