]> git.donarmstrong.com Git - lilypond.git/blob - lib/simple-file-storage.cc
partial: 1.0.1.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--1998 Han-Wen Nienhuys <hanwen@stack.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 "varray.hh"
17 #include "string.hh"
18 #include "warn.hh"
19
20 void
21 Simple_file_storage::load_stdin ()
22 {
23   int data_len = 0;
24   len_i_ = 0;
25
26   int c;
27   Array<char> ch_arr;
28   while ((c = fgetc (stdin)) != EOF)
29     ch_arr.push (c);
30   len_i_ = ch_arr.size ();
31   data_p_ = ch_arr.remove_array_p ();
32 }
33
34 void
35 Simple_file_storage::load_file (String s)
36 {
37   /*
38     let's hope that "b" opens anything binary, and does not apply
39     CR/LF translation
40     */
41   FILE * f =  fopen (s.ch_C (), "rb");
42
43   if (!f)
44     {
45       warning (_ ("can't open file `") + s + "'");
46       return ;
47     }
48
49   int ret = fseek (f, 0, SEEK_END);
50   len_i_ = ftell (f);
51   rewind (f);
52   data_p_ = new char[len_i_+1];
53   data_p_[len_i_] = 0;
54   ret = fread (data_p_, sizeof (char), len_i_, f);
55
56   if  (ret!=len_i_)
57     warning (_ ("Huh? got ") + String (ret) + _ (", expected ")
58              + String (len_i_) + _ (" characters"));
59
60   fclose (f);
61 }
62
63 /**
64   Stupid but foolproof way of opening files.
65
66   TODO
67   Should check IO status
68
69   This is of course a build it yourself version of mmap, so we should
70   have been using that... (see Mapped_file_storage) But we noticed
71   some problems with this (unexplained lexer crashes)
72
73   [Some versions later] The crashes aren't caused by the mmap
74   code. But no reason to take it out, is there?  mmap ()
75
76 */
77
78 Simple_file_storage::Simple_file_storage (String s)
79 {
80   data_p_ = 0;
81   len_i_ = 0;
82
83   if (!s.length_i ())
84     load_stdin ();
85   else
86     load_file (s);
87 }
88
89 char const*
90 Simple_file_storage::ch_C () const
91 {
92   return data_p_;
93 }
94
95 int
96 Simple_file_storage::length_i () const
97 {
98   return len_i_;
99 }
100
101
102 Simple_file_storage::~Simple_file_storage ()
103 {
104   delete []data_p_;
105 }