Chromium Code Reviews| Index: src/utils.cc |
| =================================================================== |
| --- src/utils.cc (revision 6302) |
| +++ src/utils.cc (working copy) |
| @@ -276,4 +276,94 @@ |
| } |
| +MemoryMappedExternalResource::MemoryMappedExternalResource(const char* filename) |
| + : filename_(NULL), data_(NULL), length_(0), |
|
mnaganov (inactive)
2011/01/16 10:16:02
Please either have a single line of initialization
marklam
2011/01/18 02:49:02
Done.
|
| + remove_file_on_cleanup_(false) { |
| + |
|
mnaganov (inactive)
2011/01/16 10:16:02
Please remove this redundant empty line.
marklam
2011/01/18 02:49:02
Done.
|
| + Init(filename); |
| +} |
| + |
| + |
| +MemoryMappedExternalResource:: |
| + MemoryMappedExternalResource(const char* filename, |
| + bool remove_file_on_cleanup) |
| + : filename_(NULL), data_(NULL), length_(0), |
| + remove_file_on_cleanup_(remove_file_on_cleanup) { |
| + |
| + Init(filename); |
| +} |
| + |
| + |
| +MemoryMappedExternalResource::~MemoryMappedExternalResource() { |
| + // Release the resources if we had successfully acquired them: |
| + if (file_) { |
| + delete file_; |
| + |
|
mnaganov (inactive)
2011/01/16 10:16:02
I this empty line and the one below a redundant.
marklam
2011/01/18 02:49:02
Done.
|
| + if (remove_file_on_cleanup_) { |
| + OS::Remove(filename_); |
| + } |
| + |
| + DeleteArray<char>(filename_); |
| + } |
| +} |
| + |
| + |
| +void MemoryMappedExternalResource::Init(const char* filename) { |
| + file_ = OS::MemoryMappedFile::open(filename); |
| + if (file_) { |
| + filename_ = StrDup(filename); |
| + data_ = reinterpret_cast<char*>(file_->memory()); |
| + length_ = file_->size(); |
| + } |
| +} |
| + |
| + |
| +bool MemoryMappedExternalResource::EnsureIsAscii(bool abort_if_failed) const { |
| + bool is_ascii = true; |
| + |
| + int line_no = 1; |
| + const char* start_of_line = data_; |
| + const char* end = data_ + length_; |
| + for (const char* p = data_; p < end; p++) { |
| + char c = *p; |
| + if (c & 0x80) { |
| + // Non-ascii detected: |
| + is_ascii = false; |
| + |
| + // Report the error and abort if appropriate: |
| + if (abort_if_failed) { |
| + int char_no = (p - start_of_line) - 1; |
| + |
| + ASSERT(filename_ != NULL); |
| + PrintF("\n\n\n" |
| + "Abort: Non-Ascii character 0x%.2x in file %s line %d char %d", |
| + c, filename_, line_no, char_no); |
| + |
| + const char* err_context = p - 10; |
|
mnaganov (inactive)
2011/01/16 10:16:02
Please introduce a constant for this magic number.
marklam
2011/01/18 02:49:02
Done.
|
| + if (err_context < data_) { |
| + err_context = data_; |
| + } |
| + int err_context_length = p - err_context; |
| + if (err_context_length) { |
| + char buffer[11]; |
| + strncpy(buffer, err_context, err_context_length); |
| + buffer[err_context_length] = '\0'; |
| + PrintF(" after \"%s\"", buffer); |
|
mnaganov (inactive)
2011/01/16 10:16:02
You can use the "precision" field to specify the m
marklam
2011/01/18 02:49:02
Done.
|
| + } |
| + PrintF(".\n\n\n"); |
| + abort(); |
| + } |
| + |
| + break; // Non-ascii detected. No need to continue scanning. |
| + } |
| + if (c == '\n') { |
| + start_of_line = p; |
| + line_no++; |
| + } |
| + } |
| + |
| + return is_ascii; |
| +} |
| + |
| + |
| } } // namespace v8::internal |