Stupid but foolproof way of opening files.
TODO
- Should use obstack. Should check IO status
+ Should check IO status
This is of course a build it yourself version of mmap, so we should
have been using that... (see Mapped_file_storage) But we noticed
some problems with this (unexplained lexer crashes)
- */
+ [Some versions later] The crashes aren't caused by the mmap
+ code. But no reason to take it out, is there? */
Simple_file_storage::Simple_file_storage(String s)
{
- data_p_ =0;
- FILE * f = fopen ( s.ch_C(), "r");
- if ( !f )
- {
- warning("can't open file\n");
- return ;
- }
+ data_p_ =0;
- int ret = fseek( f, 0, SEEK_END);
- len_i_ = ftell(f);
- rewind(f);
- data_p_ = new char[len_i_+1];
- data_p_[len_i_] = 0;
- ret = fread(data_p_, sizeof(char), len_i_, f);
- assert (ret==len_i_);
+ /*
+ let's hope that "b" opens anything binary, and does not apply
+ CR/LF translation
+ */
+ FILE * f = (s) ? fopen (s.ch_C(), "rb") : stdin;
+
+ if (!f)
+ {
+ warning("can't open file `" + s + "'");
+ return ;
+ }
+
+ int ret = fseek(f, 0, SEEK_END);
+ len_i_ = ftell(f);
+ rewind(f);
+ data_p_ = new char[len_i_+1];
+ data_p_[len_i_] = 0;
+ ret = fread(data_p_, sizeof(char), len_i_, f);
+
+
+ if (ret!=len_i_)
+ warning ("Huh? got " + String(ret) + ", expected "
+ + String(len_i_) + " characters");
+
+ if (f != stdin)
fclose(f);
}
char const*
Simple_file_storage::ch_C() const
{
- return data_p_;
+ return data_p_;
}
int
-Simple_file_storage::length_i()const
+Simple_file_storage::length_i() const
{
- return len_i_;
+ return len_i_;
}
Simple_file_storage::~Simple_file_storage()
{
- delete []data_p_;
+ delete []data_p_;
}