]> git.donarmstrong.com Git - lilypond.git/blob - flower/simple-file-storage.cc
``slikken kreng''
[lilypond.git] / flower / 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_ = 0;
24
25   int c;
26   Array<char> chs;
27   while ((c = fgetc (stdin)) != EOF)
28     chs.push (c);
29   len_ = chs.size ();
30   data_ = chs.remove_array ();
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.to_str0 (), "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_ = ftell (f);
50   rewind (f);
51   data_ = new char[len_+1];
52   data_[len_] = 0;
53   ret = fread (data_, sizeof (char), len_, f);
54
55   if (ret!=len_)
56     warning (_f ("Huh?  Got %d, expected %d characters", ret, len_));
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_ = 0;
75   len_ = 0;
76
77   if ((s == "-"))
78     load_stdin ();
79   else
80     load_file (s);
81 }
82
83 char const*
84 Simple_file_storage::to_str0 () const
85 {
86   return data_;
87 }
88
89 int
90 Simple_file_storage::length () const
91 {
92   return len_;
93 }
94
95
96 Simple_file_storage::~Simple_file_storage ()
97 {
98   delete []data_;
99 }