]> git.donarmstrong.com Git - lilypond.git/blob - lib/simple-file-storage.cc
partial: 1.3.24.jcn
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdio.h>
10
11 #ifndef SEEK_END
12 #define SEEK_END 2
13 #endif
14
15 #include "simple-file-storage.hh"
16 #include "array.hh"
17 #include "string.hh"
18 #include "warn.hh"
19
20 void
21 Simple_file_storage::load_stdin ()
22 {
23   len_i_ = 0;
24
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 ();
31 }
32
33 void
34 Simple_file_storage::load_file (String s)
35 {
36   /*
37     let's hope that "b" opens anything binary, and does not apply
38     CR/LF translation
39     */
40   FILE * f =  fopen (s.ch_C (), "rb");
41
42   if (!f)
43     {
44       warning (_f ("Can't open file: `%s'", s));
45       return ;
46     }
47
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);
54
55   if  (ret!=len_i_)
56     warning (_f ("Huh?  Got %d, expected %d characters", ret, len_i_));
57
58   fclose (f);
59 }
60
61 /**
62   Stupid but foolproof way of opening files.
63
64   TODO
65   Should check IO status
66
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
69   
70 */
71
72 Simple_file_storage::Simple_file_storage (String s)
73 {
74   data_p_ = 0;
75   len_i_ = 0;
76
77   if (!s.length_i () || (s == "-"))
78     load_stdin ();
79   else
80     load_file (s);
81 }
82
83 char const*
84 Simple_file_storage::ch_C () const
85 {
86   return data_p_;
87 }
88
89 int
90 Simple_file_storage::length_i () const
91 {
92   return len_i_;
93 }
94
95
96 Simple_file_storage::~Simple_file_storage ()
97 {
98   delete []data_p_;
99 }