| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // Copy the newly read line into the result. | 103 // Copy the newly read line into the result. |
| 104 memcpy(result + offset, line_buf, len * kCharSize); | 104 memcpy(result + offset, line_buf, len * kCharSize); |
| 105 offset += len; | 105 offset += len; |
| 106 } | 106 } |
| 107 ASSERT(result != NULL); | 107 ASSERT(result != NULL); |
| 108 result[offset] = '\0'; | 108 result[offset] = '\0'; |
| 109 return result; | 109 return result; |
| 110 } | 110 } |
| 111 | 111 |
| 112 | 112 |
| 113 char* ReadCharsFromFile(const char* filename, | 113 char* ReadCharsFromFile(FILE* file, |
| 114 int* size, | 114 int* size, |
| 115 int extra_space, | 115 int extra_space, |
| 116 bool verbose) { | 116 bool verbose, |
| 117 FILE* file = OS::FOpen(filename, "rb"); | 117 const char* filename) { |
| 118 if (file == NULL || fseek(file, 0, SEEK_END) != 0) { | 118 if (file == NULL || fseek(file, 0, SEEK_END) != 0) { |
| 119 if (verbose) { | 119 if (verbose) { |
| 120 OS::PrintError("Cannot read from file %s.\n", filename); | 120 OS::PrintError("Cannot read from file %s.\n", filename); |
| 121 } | 121 } |
| 122 return NULL; | 122 return NULL; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // Get the size of the file and rewind it. | 125 // Get the size of the file and rewind it. |
| 126 *size = ftell(file); | 126 *size = ftell(file); |
| 127 rewind(file); | 127 rewind(file); |
| 128 | 128 |
| 129 char* result = NewArray<char>(*size + extra_space); | 129 char* result = NewArray<char>(*size + extra_space); |
| 130 for (int i = 0; i < *size;) { | 130 for (int i = 0; i < *size && feof(file) == 0;) { |
| 131 int read = static_cast<int>(fread(&result[i], 1, *size - i, file)); | 131 int read = static_cast<int>(fread(&result[i], 1, *size - i, file)); |
| 132 if (read <= 0) { | 132 if (read != (*size - i) && ferror(file) != 0) { |
| 133 fclose(file); | 133 fclose(file); |
| 134 DeleteArray(result); | 134 DeleteArray(result); |
| 135 return NULL; | 135 return NULL; |
| 136 } | 136 } |
| 137 i += read; | 137 i += read; |
| 138 } | 138 } |
| 139 fclose(file); | 139 return result; |
| 140 } |
| 141 |
| 142 |
| 143 char* ReadCharsFromFile(const char* filename, |
| 144 int* size, |
| 145 int extra_space, |
| 146 bool verbose) { |
| 147 FILE* file = OS::FOpen(filename, "rb"); |
| 148 char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename); |
| 149 if (file != NULL) fclose(file); |
| 140 return result; | 150 return result; |
| 141 } | 151 } |
| 142 | 152 |
| 143 | 153 |
| 144 byte* ReadBytes(const char* filename, int* size, bool verbose) { | 154 byte* ReadBytes(const char* filename, int* size, bool verbose) { |
| 145 char* chars = ReadCharsFromFile(filename, size, 0, verbose); | 155 char* chars = ReadCharsFromFile(filename, size, 0, verbose); |
| 146 return reinterpret_cast<byte*>(chars); | 156 return reinterpret_cast<byte*>(chars); |
| 147 } | 157 } |
| 148 | 158 |
| 149 | 159 |
| 160 static Vector<const char> SetVectorContents(char* chars, |
| 161 int size, |
| 162 bool* exists) { |
| 163 if (!chars) { |
| 164 *exists = false; |
| 165 return Vector<const char>::empty(); |
| 166 } |
| 167 chars[size] = '\0'; |
| 168 *exists = true; |
| 169 return Vector<const char>(chars, size); |
| 170 } |
| 171 |
| 172 |
| 150 Vector<const char> ReadFile(const char* filename, | 173 Vector<const char> ReadFile(const char* filename, |
| 151 bool* exists, | 174 bool* exists, |
| 152 bool verbose) { | 175 bool verbose) { |
| 153 int size; | 176 int size; |
| 154 char* result = ReadCharsFromFile(filename, &size, 1, verbose); | 177 char* result = ReadCharsFromFile(filename, &size, 1, verbose); |
| 155 if (!result) { | 178 return SetVectorContents(result, size, exists); |
| 156 *exists = false; | 179 } |
| 157 return Vector<const char>::empty(); | 180 |
| 158 } | 181 |
| 159 result[size] = '\0'; | 182 Vector<const char> ReadFile(FILE* file, |
| 160 *exists = true; | 183 bool* exists, |
| 161 return Vector<const char>(result, size); | 184 bool verbose) { |
| 185 int size; |
| 186 char* result = ReadCharsFromFile(file, &size, 1, verbose, ""); |
| 187 return SetVectorContents(result, size, exists); |
| 162 } | 188 } |
| 163 | 189 |
| 164 | 190 |
| 165 int WriteCharsToFile(const char* str, int size, FILE* f) { | 191 int WriteCharsToFile(const char* str, int size, FILE* f) { |
| 166 int total = 0; | 192 int total = 0; |
| 167 while (total < size) { | 193 while (total < size) { |
| 168 int write = static_cast<int>(fwrite(str, 1, size - total, f)); | 194 int write = static_cast<int>(fwrite(str, 1, size - total, f)); |
| 169 if (write == 0) { | 195 if (write == 0) { |
| 170 return total; | 196 return total; |
| 171 } | 197 } |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 start_of_line = p; | 351 start_of_line = p; |
| 326 line_no++; | 352 line_no++; |
| 327 } | 353 } |
| 328 } | 354 } |
| 329 | 355 |
| 330 return is_ascii; | 356 return is_ascii; |
| 331 } | 357 } |
| 332 | 358 |
| 333 | 359 |
| 334 } } // namespace v8::internal | 360 } } // namespace v8::internal |
| OLD | NEW |