OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 ASSERT(result != NULL); | 148 ASSERT(result != NULL); |
149 result[offset] = '\0'; | 149 result[offset] = '\0'; |
150 return result; | 150 return result; |
151 } | 151 } |
152 | 152 |
153 | 153 |
154 char* ReadCharsFromFile(const char* filename, | 154 char* ReadCharsFromFile(const char* filename, |
155 int* size, | 155 int* size, |
156 int extra_space, | 156 int extra_space, |
157 bool verbose) { | 157 bool verbose) { |
158 FILE* file = fopen(filename, "rb"); | 158 FILE* file = OS::FOpen(filename, "rb"); |
159 if (file == NULL || fseek(file, 0, SEEK_END) != 0) { | 159 if (file == NULL || fseek(file, 0, SEEK_END) != 0) { |
160 if (verbose) { | 160 if (verbose) { |
161 OS::PrintError("Cannot read from file %s.\n", filename); | 161 OS::PrintError("Cannot read from file %s.\n", filename); |
162 } | 162 } |
163 return NULL; | 163 return NULL; |
164 } | 164 } |
165 | 165 |
166 // Get the size of the file and rewind it. | 166 // Get the size of the file and rewind it. |
167 *size = ftell(file); | 167 *size = ftell(file); |
168 rewind(file); | 168 rewind(file); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 str += write; | 213 str += write; |
214 } | 214 } |
215 return total; | 215 return total; |
216 } | 216 } |
217 | 217 |
218 | 218 |
219 int WriteChars(const char* filename, | 219 int WriteChars(const char* filename, |
220 const char* str, | 220 const char* str, |
221 int size, | 221 int size, |
222 bool verbose) { | 222 bool verbose) { |
223 FILE* f = fopen(filename, "wb"); | 223 FILE* f = OS::FOpen(filename, "wb"); |
224 if (f == NULL) { | 224 if (f == NULL) { |
225 if (verbose) { | 225 if (verbose) { |
226 OS::PrintError("Cannot open file %s for reading.\n", filename); | 226 OS::PrintError("Cannot open file %s for reading.\n", filename); |
227 } | 227 } |
228 return 0; | 228 return 0; |
229 } | 229 } |
230 int written = WriteCharsToFile(str, size, f); | 230 int written = WriteCharsToFile(str, size, f); |
231 fclose(f); | 231 fclose(f); |
232 return written; | 232 return written; |
233 } | 233 } |
234 | 234 |
235 | 235 |
236 StringBuilder::StringBuilder(int size) { | 236 StringBuilder::StringBuilder(int size) { |
237 buffer_ = NewArray<char>(size); | 237 buffer_ = Vector<char>::New(size); |
238 size_ = size; | |
239 position_ = 0; | 238 position_ = 0; |
240 } | 239 } |
241 | 240 |
242 | 241 |
243 void StringBuilder::AddString(const char* s) { | 242 void StringBuilder::AddString(const char* s) { |
244 AddSubstring(s, strlen(s)); | 243 AddSubstring(s, strlen(s)); |
245 } | 244 } |
246 | 245 |
247 | 246 |
248 void StringBuilder::AddSubstring(const char* s, int n) { | 247 void StringBuilder::AddSubstring(const char* s, int n) { |
249 ASSERT(!is_finalized() && position_ + n < size_); | 248 ASSERT(!is_finalized() && position_ + n < buffer_.length()); |
250 ASSERT(static_cast<size_t>(n) <= strlen(s)); | 249 ASSERT(static_cast<size_t>(n) <= strlen(s)); |
251 memcpy(&buffer_[position_], s, n * kCharSize); | 250 memcpy(&buffer_[position_], s, n * kCharSize); |
252 position_ += n; | 251 position_ += n; |
253 } | 252 } |
254 | 253 |
255 | 254 |
256 void StringBuilder::AddFormatted(const char* format, ...) { | 255 void StringBuilder::AddFormatted(const char* format, ...) { |
257 ASSERT(!is_finalized() && position_ < size_); | 256 ASSERT(!is_finalized() && position_ < buffer_.length()); |
258 va_list args; | 257 va_list args; |
259 va_start(args, format); | 258 va_start(args, format); |
260 int remaining = size_ - position_; | 259 int n = OS::VSNPrintF(buffer_ + position_, format, args); |
261 int n = OS::VSNPrintF(&buffer_[position_], remaining, format, args); | |
262 va_end(args); | 260 va_end(args); |
263 if (n < 0 || n >= remaining) { | 261 if (n < 0 || n >= (buffer_.length() - position_)) { |
264 position_ = size_; | 262 position_ = buffer_.length(); |
265 } else { | 263 } else { |
266 position_ += n; | 264 position_ += n; |
267 } | 265 } |
268 } | 266 } |
269 | 267 |
270 | 268 |
271 void StringBuilder::AddPadding(char c, int count) { | 269 void StringBuilder::AddPadding(char c, int count) { |
272 for (int i = 0; i < count; i++) { | 270 for (int i = 0; i < count; i++) { |
273 AddCharacter(c); | 271 AddCharacter(c); |
274 } | 272 } |
275 } | 273 } |
276 | 274 |
277 | 275 |
278 char* StringBuilder::Finalize() { | 276 char* StringBuilder::Finalize() { |
279 ASSERT(!is_finalized() && position_ < size_); | 277 ASSERT(!is_finalized() && position_ < buffer_.length()); |
280 buffer_[position_] = '\0'; | 278 buffer_[position_] = '\0'; |
281 // Make sure nobody managed to add a 0-character to the | 279 // Make sure nobody managed to add a 0-character to the |
282 // buffer while building the string. | 280 // buffer while building the string. |
283 ASSERT(strlen(buffer_) == static_cast<size_t>(position_)); | 281 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
284 position_ = -1; | 282 position_ = -1; |
285 ASSERT(is_finalized()); | 283 ASSERT(is_finalized()); |
286 return buffer_; | 284 return buffer_.start(); |
287 } | 285 } |
288 | 286 |
289 } } // namespace v8::internal | 287 } } // namespace v8::internal |
OLD | NEW |