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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include <stdarg.h> | 28 #include <stdarg.h> |
29 #include "../include/v8stdint.h" | 29 #include <sys/stat.h> |
| 30 |
| 31 #include "v8.h" |
| 32 |
30 #include "checks.h" | 33 #include "checks.h" |
31 #include "platform.h" | 34 #include "platform.h" |
32 #include "utils.h" | 35 #include "utils.h" |
33 | 36 |
34 namespace v8 { | 37 namespace v8 { |
35 namespace internal { | 38 namespace internal { |
36 | 39 |
37 | 40 |
38 SimpleStringBuilder::SimpleStringBuilder(int size) { | 41 SimpleStringBuilder::SimpleStringBuilder(int size) { |
39 buffer_ = Vector<char>::New(size); | 42 buffer_ = Vector<char>::New(size); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 buffer_[position_] = '\0'; | 93 buffer_[position_] = '\0'; |
91 // Make sure nobody managed to add a 0-character to the | 94 // Make sure nobody managed to add a 0-character to the |
92 // buffer while building the string. | 95 // buffer while building the string. |
93 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); | 96 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
94 position_ = -1; | 97 position_ = -1; |
95 ASSERT(is_finalized()); | 98 ASSERT(is_finalized()); |
96 return buffer_.start(); | 99 return buffer_.start(); |
97 } | 100 } |
98 | 101 |
99 | 102 |
| 103 void PrintF(const char* format, ...) { |
| 104 va_list arguments; |
| 105 va_start(arguments, format); |
| 106 OS::VPrint(format, arguments); |
| 107 va_end(arguments); |
| 108 } |
| 109 |
| 110 |
| 111 void PrintF(FILE* out, const char* format, ...) { |
| 112 va_list arguments; |
| 113 va_start(arguments, format); |
| 114 OS::VFPrint(out, format, arguments); |
| 115 va_end(arguments); |
| 116 } |
| 117 |
| 118 |
| 119 void PrintPID(const char* format, ...) { |
| 120 OS::Print("[%d] ", OS::GetCurrentProcessId()); |
| 121 va_list arguments; |
| 122 va_start(arguments, format); |
| 123 OS::VPrint(format, arguments); |
| 124 va_end(arguments); |
| 125 } |
| 126 |
| 127 |
| 128 void Flush(FILE* out) { |
| 129 fflush(out); |
| 130 } |
| 131 |
| 132 |
| 133 char* ReadLine(const char* prompt) { |
| 134 char* result = NULL; |
| 135 char line_buf[256]; |
| 136 int offset = 0; |
| 137 bool keep_going = true; |
| 138 fprintf(stdout, "%s", prompt); |
| 139 fflush(stdout); |
| 140 while (keep_going) { |
| 141 if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) { |
| 142 // fgets got an error. Just give up. |
| 143 if (result != NULL) { |
| 144 DeleteArray(result); |
| 145 } |
| 146 return NULL; |
| 147 } |
| 148 int len = StrLength(line_buf); |
| 149 if (len > 1 && |
| 150 line_buf[len - 2] == '\\' && |
| 151 line_buf[len - 1] == '\n') { |
| 152 // When we read a line that ends with a "\" we remove the escape and |
| 153 // append the remainder. |
| 154 line_buf[len - 2] = '\n'; |
| 155 line_buf[len - 1] = 0; |
| 156 len -= 1; |
| 157 } else if ((len > 0) && (line_buf[len - 1] == '\n')) { |
| 158 // Since we read a new line we are done reading the line. This |
| 159 // will exit the loop after copying this buffer into the result. |
| 160 keep_going = false; |
| 161 } |
| 162 if (result == NULL) { |
| 163 // Allocate the initial result and make room for the terminating '\0' |
| 164 result = NewArray<char>(len + 1); |
| 165 } else { |
| 166 // Allocate a new result with enough room for the new addition. |
| 167 int new_len = offset + len + 1; |
| 168 char* new_result = NewArray<char>(new_len); |
| 169 // Copy the existing input into the new array and set the new |
| 170 // array as the result. |
| 171 OS::MemCopy(new_result, result, offset * kCharSize); |
| 172 DeleteArray(result); |
| 173 result = new_result; |
| 174 } |
| 175 // Copy the newly read line into the result. |
| 176 OS::MemCopy(result + offset, line_buf, len * kCharSize); |
| 177 offset += len; |
| 178 } |
| 179 ASSERT(result != NULL); |
| 180 result[offset] = '\0'; |
| 181 return result; |
| 182 } |
| 183 |
| 184 |
| 185 char* ReadCharsFromFile(FILE* file, |
| 186 int* size, |
| 187 int extra_space, |
| 188 bool verbose, |
| 189 const char* filename) { |
| 190 if (file == NULL || fseek(file, 0, SEEK_END) != 0) { |
| 191 if (verbose) { |
| 192 OS::PrintError("Cannot read from file %s.\n", filename); |
| 193 } |
| 194 return NULL; |
| 195 } |
| 196 |
| 197 // Get the size of the file and rewind it. |
| 198 *size = ftell(file); |
| 199 rewind(file); |
| 200 |
| 201 char* result = NewArray<char>(*size + extra_space); |
| 202 for (int i = 0; i < *size && feof(file) == 0;) { |
| 203 int read = static_cast<int>(fread(&result[i], 1, *size - i, file)); |
| 204 if (read != (*size - i) && ferror(file) != 0) { |
| 205 fclose(file); |
| 206 DeleteArray(result); |
| 207 return NULL; |
| 208 } |
| 209 i += read; |
| 210 } |
| 211 return result; |
| 212 } |
| 213 |
| 214 |
| 215 char* ReadCharsFromFile(const char* filename, |
| 216 int* size, |
| 217 int extra_space, |
| 218 bool verbose) { |
| 219 FILE* file = OS::FOpen(filename, "rb"); |
| 220 char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename); |
| 221 if (file != NULL) fclose(file); |
| 222 return result; |
| 223 } |
| 224 |
| 225 |
| 226 byte* ReadBytes(const char* filename, int* size, bool verbose) { |
| 227 char* chars = ReadCharsFromFile(filename, size, 0, verbose); |
| 228 return reinterpret_cast<byte*>(chars); |
| 229 } |
| 230 |
| 231 |
| 232 static Vector<const char> SetVectorContents(char* chars, |
| 233 int size, |
| 234 bool* exists) { |
| 235 if (!chars) { |
| 236 *exists = false; |
| 237 return Vector<const char>::empty(); |
| 238 } |
| 239 chars[size] = '\0'; |
| 240 *exists = true; |
| 241 return Vector<const char>(chars, size); |
| 242 } |
| 243 |
| 244 |
| 245 Vector<const char> ReadFile(const char* filename, |
| 246 bool* exists, |
| 247 bool verbose) { |
| 248 int size; |
| 249 char* result = ReadCharsFromFile(filename, &size, 1, verbose); |
| 250 return SetVectorContents(result, size, exists); |
| 251 } |
| 252 |
| 253 |
| 254 Vector<const char> ReadFile(FILE* file, |
| 255 bool* exists, |
| 256 bool verbose) { |
| 257 int size; |
| 258 char* result = ReadCharsFromFile(file, &size, 1, verbose, ""); |
| 259 return SetVectorContents(result, size, exists); |
| 260 } |
| 261 |
| 262 |
| 263 int WriteCharsToFile(const char* str, int size, FILE* f) { |
| 264 int total = 0; |
| 265 while (total < size) { |
| 266 int write = static_cast<int>(fwrite(str, 1, size - total, f)); |
| 267 if (write == 0) { |
| 268 return total; |
| 269 } |
| 270 total += write; |
| 271 str += write; |
| 272 } |
| 273 return total; |
| 274 } |
| 275 |
| 276 |
| 277 int AppendChars(const char* filename, |
| 278 const char* str, |
| 279 int size, |
| 280 bool verbose) { |
| 281 FILE* f = OS::FOpen(filename, "ab"); |
| 282 if (f == NULL) { |
| 283 if (verbose) { |
| 284 OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 285 } |
| 286 return 0; |
| 287 } |
| 288 int written = WriteCharsToFile(str, size, f); |
| 289 fclose(f); |
| 290 return written; |
| 291 } |
| 292 |
| 293 |
| 294 int WriteChars(const char* filename, |
| 295 const char* str, |
| 296 int size, |
| 297 bool verbose) { |
| 298 FILE* f = OS::FOpen(filename, "wb"); |
| 299 if (f == NULL) { |
| 300 if (verbose) { |
| 301 OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 302 } |
| 303 return 0; |
| 304 } |
| 305 int written = WriteCharsToFile(str, size, f); |
| 306 fclose(f); |
| 307 return written; |
| 308 } |
| 309 |
| 310 |
| 311 int WriteBytes(const char* filename, |
| 312 const byte* bytes, |
| 313 int size, |
| 314 bool verbose) { |
| 315 const char* str = reinterpret_cast<const char*>(bytes); |
| 316 return WriteChars(filename, str, size, verbose); |
| 317 } |
| 318 |
| 319 |
| 320 |
| 321 void StringBuilder::AddFormatted(const char* format, ...) { |
| 322 va_list arguments; |
| 323 va_start(arguments, format); |
| 324 AddFormattedList(format, arguments); |
| 325 va_end(arguments); |
| 326 } |
| 327 |
| 328 |
| 329 void StringBuilder::AddFormattedList(const char* format, va_list list) { |
| 330 ASSERT(!is_finalized() && position_ <= buffer_.length()); |
| 331 int n = OS::VSNPrintF(buffer_ + position_, format, list); |
| 332 if (n < 0 || n >= (buffer_.length() - position_)) { |
| 333 position_ = buffer_.length(); |
| 334 } else { |
| 335 position_ += n; |
| 336 } |
| 337 } |
| 338 |
| 339 |
100 } } // namespace v8::internal | 340 } } // namespace v8::internal |
OLD | NEW |