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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 buffer_[position_] = '\0'; | 269 buffer_[position_] = '\0'; |
270 // Make sure nobody managed to add a 0-character to the | 270 // Make sure nobody managed to add a 0-character to the |
271 // buffer while building the string. | 271 // buffer while building the string. |
272 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); | 272 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
273 position_ = -1; | 273 position_ = -1; |
274 ASSERT(is_finalized()); | 274 ASSERT(is_finalized()); |
275 return buffer_.start(); | 275 return buffer_.start(); |
276 } | 276 } |
277 | 277 |
278 | 278 |
| 279 MemoryMappedExternalResource::MemoryMappedExternalResource(const char* filename) |
| 280 : filename_(NULL), |
| 281 data_(NULL), |
| 282 length_(0), |
| 283 remove_file_on_cleanup_(false) { |
| 284 Init(filename); |
| 285 } |
| 286 |
| 287 |
| 288 MemoryMappedExternalResource:: |
| 289 MemoryMappedExternalResource(const char* filename, |
| 290 bool remove_file_on_cleanup) |
| 291 : filename_(NULL), |
| 292 data_(NULL), |
| 293 length_(0), |
| 294 remove_file_on_cleanup_(remove_file_on_cleanup) { |
| 295 Init(filename); |
| 296 } |
| 297 |
| 298 |
| 299 MemoryMappedExternalResource::~MemoryMappedExternalResource() { |
| 300 // Release the resources if we had successfully acquired them: |
| 301 if (file_ != NULL) { |
| 302 delete file_; |
| 303 if (remove_file_on_cleanup_) { |
| 304 OS::Remove(filename_); |
| 305 } |
| 306 DeleteArray<char>(filename_); |
| 307 } |
| 308 } |
| 309 |
| 310 |
| 311 void MemoryMappedExternalResource::Init(const char* filename) { |
| 312 file_ = OS::MemoryMappedFile::open(filename); |
| 313 if (file_ != NULL) { |
| 314 filename_ = StrDup(filename); |
| 315 data_ = reinterpret_cast<char*>(file_->memory()); |
| 316 length_ = file_->size(); |
| 317 } |
| 318 } |
| 319 |
| 320 |
| 321 bool MemoryMappedExternalResource::EnsureIsAscii(bool abort_if_failed) const { |
| 322 bool is_ascii = true; |
| 323 |
| 324 int line_no = 1; |
| 325 const char* start_of_line = data_; |
| 326 const char* end = data_ + length_; |
| 327 for (const char* p = data_; p < end; p++) { |
| 328 char c = *p; |
| 329 if ((c & 0x80) != 0) { |
| 330 // Non-ascii detected: |
| 331 is_ascii = false; |
| 332 |
| 333 // Report the error and abort if appropriate: |
| 334 if (abort_if_failed) { |
| 335 int char_no = (p - start_of_line) - 1; |
| 336 |
| 337 ASSERT(filename_ != NULL); |
| 338 PrintF("\n\n\n" |
| 339 "Abort: Non-Ascii character 0x%.2x in file %s line %d char %d", |
| 340 c, filename_, line_no, char_no); |
| 341 |
| 342 // Allow for some context up to kNumberOfLeadingContextChars chars |
| 343 // before the offending non-ascii char to help the user see where |
| 344 // the offending char is. |
| 345 const int kNumberOfLeadingContextChars = 10; |
| 346 const char* err_context = p - kNumberOfLeadingContextChars; |
| 347 if (err_context < data_) { |
| 348 err_context = data_; |
| 349 } |
| 350 // Compute the length of the error context and print it. |
| 351 int err_context_length = p - err_context; |
| 352 if (err_context_length != 0) { |
| 353 PrintF(" after \"%.*s\"", err_context_length, err_context); |
| 354 } |
| 355 PrintF(".\n\n\n"); |
| 356 OS::Abort(); |
| 357 } |
| 358 |
| 359 break; // Non-ascii detected. No need to continue scanning. |
| 360 } |
| 361 if (c == '\n') { |
| 362 start_of_line = p; |
| 363 line_no++; |
| 364 } |
| 365 } |
| 366 |
| 367 return is_ascii; |
| 368 } |
| 369 |
| 370 |
279 } } // namespace v8::internal | 371 } } // namespace v8::internal |
OLD | NEW |