Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 This file is part of LilyPond, the GNU music typesetter. | 2 This file is part of LilyPond, the GNU music typesetter. |
3 | 3 |
4 Copyright (C) 1997--2020 Jan Nieuwenhuizen <janneke@gnu.org> | 4 Copyright (C) 1997--2020 Jan Nieuwenhuizen <janneke@gnu.org> |
5 Han-Wen Nienhuys <hanwen@xs4all.nl> | 5 Han-Wen Nienhuys <hanwen@xs4all.nl> |
6 | 6 |
7 LilyPond is free software: you can redistribute it and/or modify | 7 LilyPond is free software: you can redistribute it and/or modify |
8 it under the terms of the GNU General Public License as published by | 8 it under the terms of the GNU General Public License as published by |
9 the Free Software Foundation, either version 3 of the License, or | 9 the Free Software Foundation, either version 3 of the License, or |
10 (at your option) any later version. | 10 (at your option) any later version. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
45 void | 45 void |
46 Source_file::load_stdin () | 46 Source_file::load_stdin () |
47 { | 47 { |
48 data_.clear (); | 48 data_.clear (); |
49 int c; | 49 int c; |
50 while ((c = fgetc (stdin)) != EOF) | 50 while ((c = fgetc (stdin)) != EOF) |
51 data_.push_back ((char)c); | 51 data_.push_back ((char)c); |
52 } | 52 } |
53 | 53 |
54 /* | 54 /* |
55 return contents of FILENAME. *Not 0-terminated!* | 55 return contents of FILENAME. |
56 | |
57 TODO: return a std::string | |
58 */ | 56 */ |
59 vector<char> | 57 string |
60 gulp_file (const string &filename, size_t desired_size) | 58 gulp_file (const string &filename, size_t desired_size) |
61 { | 59 { |
62 /* "b" must ensure to open literally, avoiding text (CR/LF) | 60 /* "b" must ensure to open literally, avoiding text (CR/LF) |
63 conversions. */ | 61 conversions. */ |
64 FILE *f = fopen (filename.c_str (), "rb"); // TODO: RAII | 62 FILE *f = fopen (filename.c_str (), "rb"); // TODO: RAII |
65 if (!f) | 63 if (!f) |
66 { | 64 { |
67 warning (_f ("cannot open file: `%s'", filename.c_str ())); | 65 warning (_f ("cannot open file: `%s'", filename.c_str ())); |
68 return {}; | 66 return {}; |
69 } | 67 } |
70 | 68 |
71 fseek (f, 0, SEEK_END); | 69 fseek (f, 0, SEEK_END); |
72 const auto real_size = ftell (f); | 70 const auto real_size = ftell (f); |
73 if (real_size < 0) | 71 if (real_size < 0) |
74 { | 72 { |
75 warning (_f ("failed to get file size: `%s'", filename.c_str ())); | 73 warning (_f ("failed to get file size: `%s'", filename.c_str ())); |
76 fclose (f); | 74 fclose (f); |
77 return {}; | 75 return {}; |
78 } | 76 } |
79 size_t read_count = real_size; | 77 size_t read_count = real_size; |
80 | 78 |
81 if (desired_size > 0) | 79 if (desired_size > 0) |
82 read_count = std::min (read_count, desired_size); | 80 read_count = std::min (read_count, desired_size); |
83 | 81 |
84 rewind (f); | 82 rewind (f); |
85 | 83 |
86 vector<char> cxx_arr (read_count); | 84 string dest (read_count, 0); |
87 size_t bytes_read = fread (cxx_arr.data (), sizeof (char), read_count, f); | 85 size_t bytes_read = fread (&dest[0], sizeof (char), read_count, f); |
88 if (bytes_read < read_count) | 86 if (bytes_read < read_count) |
89 { | 87 { |
90 warning (_f ("expected to read %zu characters, got %zu", read_count, | 88 warning (_f ("expected to read %zu characters, got %zu", read_count, |
91 bytes_read)); | 89 bytes_read)); |
92 cxx_arr.resize (bytes_read); | 90 dest.resize (bytes_read); |
93 } | 91 } |
94 fclose (f); | 92 fclose (f); |
95 return cxx_arr; | 93 return dest; |
96 } | 94 } |
97 | 95 |
98 void | 96 void |
99 Source_file::init () | 97 Source_file::init () |
100 { | 98 { |
101 istream_ = 0; | 99 istream_ = 0; |
102 line_offset_ = 0; | 100 line_offset_ = 0; |
103 str_port_ = SCM_EOL; | 101 str_port_ = SCM_EOL; |
104 smobify_self (); | 102 smobify_self (); |
105 } | 103 } |
(...skipping 19 matching lines...) Expand all Loading... | |
125 | 123 |
126 Source_file::Source_file (const string &filename_string) | 124 Source_file::Source_file (const string &filename_string) |
127 { | 125 { |
128 init (); | 126 init (); |
129 | 127 |
130 name_ = filename_string; | 128 name_ = filename_string; |
131 | 129 |
132 if (filename_string == "-") | 130 if (filename_string == "-") |
133 load_stdin (); | 131 load_stdin (); |
134 else | 132 else |
135 { | 133 data_ = gulp_file (filename_string, -1); |
136 vector<char> chars = gulp_file (filename_string, -1); | |
137 data_ = string (&chars[0], chars.size ()); | |
dak
2020/02/14 22:40:51
gulp_file_to_string ?
That one apparently also do
hanwenn
2020/02/14 22:43:35
I prefer keeping the functionality unchanged. We'd
| |
138 } | |
139 | 134 |
140 init_port (); | 135 init_port (); |
141 init_newlines (); | 136 init_newlines (); |
142 } | 137 } |
143 | 138 |
144 void | 139 void |
145 Source_file::init_port () | 140 Source_file::init_port () |
146 { | 141 { |
147 // This is somewhat icky: the string will in general be in utf8, but | 142 // This is somewhat icky: the string will in general be in utf8, but |
148 // we do our own utf8 encoding and verification in the parser, so we | 143 // we do our own utf8 encoding and verification in the parser, so we |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
385 int | 380 int |
386 Source_file::print_smob (SCM port, scm_print_state *) const | 381 Source_file::print_smob (SCM port, scm_print_state *) const |
387 { | 382 { |
388 scm_puts ("#<Source_file ", port); | 383 scm_puts ("#<Source_file ", port); |
389 scm_puts (name_.c_str (), port); | 384 scm_puts (name_.c_str (), port); |
390 | 385 |
391 /* Do not print properties, that is too much hassle. */ | 386 /* Do not print properties, that is too much hassle. */ |
392 scm_puts (" >", port); | 387 scm_puts (" >", port); |
393 return 1; | 388 return 1; |
394 } | 389 } |
LEFT | RIGHT |