7af30f27b7bd57a395bf028aa97883d606423cb8
[platform/framework/web/crosswalk.git] / src / v8 / src / utils.cc
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdarg.h>
6 #include <sys/stat.h>
7
8 #include "v8.h"
9
10 #include "checks.h"
11 #include "platform.h"
12 #include "utils.h"
13
14 namespace v8 {
15 namespace internal {
16
17
18 SimpleStringBuilder::SimpleStringBuilder(int size) {
19   buffer_ = Vector<char>::New(size);
20   position_ = 0;
21 }
22
23
24 void SimpleStringBuilder::AddString(const char* s) {
25   AddSubstring(s, StrLength(s));
26 }
27
28
29 void SimpleStringBuilder::AddSubstring(const char* s, int n) {
30   ASSERT(!is_finalized() && position_ + n <= buffer_.length());
31   ASSERT(static_cast<size_t>(n) <= strlen(s));
32   OS::MemCopy(&buffer_[position_], s, n * kCharSize);
33   position_ += n;
34 }
35
36
37 void SimpleStringBuilder::AddPadding(char c, int count) {
38   for (int i = 0; i < count; i++) {
39     AddCharacter(c);
40   }
41 }
42
43
44 void SimpleStringBuilder::AddDecimalInteger(int32_t value) {
45   uint32_t number = static_cast<uint32_t>(value);
46   if (value < 0) {
47     AddCharacter('-');
48     number = static_cast<uint32_t>(-value);
49   }
50   int digits = 1;
51   for (uint32_t factor = 10; digits < 10; digits++, factor *= 10) {
52     if (factor > number) break;
53   }
54   position_ += digits;
55   for (int i = 1; i <= digits; i++) {
56     buffer_[position_ - i] = '0' + static_cast<char>(number % 10);
57     number /= 10;
58   }
59 }
60
61
62 char* SimpleStringBuilder::Finalize() {
63   ASSERT(!is_finalized() && position_ <= buffer_.length());
64   // If there is no space for null termination, overwrite last character.
65   if (position_ == buffer_.length()) {
66     position_--;
67     // Print ellipsis.
68     for (int i = 3; i > 0 && position_ > i; --i) buffer_[position_ - i] = '.';
69   }
70   buffer_[position_] = '\0';
71   // Make sure nobody managed to add a 0-character to the
72   // buffer while building the string.
73   ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
74   position_ = -1;
75   ASSERT(is_finalized());
76   return buffer_.start();
77 }
78
79
80 void PrintF(const char* format, ...) {
81   va_list arguments;
82   va_start(arguments, format);
83   OS::VPrint(format, arguments);
84   va_end(arguments);
85 }
86
87
88 void PrintF(FILE* out, const char* format, ...) {
89   va_list arguments;
90   va_start(arguments, format);
91   OS::VFPrint(out, format, arguments);
92   va_end(arguments);
93 }
94
95
96 void PrintPID(const char* format, ...) {
97   OS::Print("[%d] ", OS::GetCurrentProcessId());
98   va_list arguments;
99   va_start(arguments, format);
100   OS::VPrint(format, arguments);
101   va_end(arguments);
102 }
103
104
105 void Flush(FILE* out) {
106   fflush(out);
107 }
108
109
110 char* ReadLine(const char* prompt) {
111   char* result = NULL;
112   char line_buf[256];
113   int offset = 0;
114   bool keep_going = true;
115   fprintf(stdout, "%s", prompt);
116   fflush(stdout);
117   while (keep_going) {
118     if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) {
119       // fgets got an error. Just give up.
120       if (result != NULL) {
121         DeleteArray(result);
122       }
123       return NULL;
124     }
125     int len = StrLength(line_buf);
126     if (len > 1 &&
127         line_buf[len - 2] == '\\' &&
128         line_buf[len - 1] == '\n') {
129       // When we read a line that ends with a "\" we remove the escape and
130       // append the remainder.
131       line_buf[len - 2] = '\n';
132       line_buf[len - 1] = 0;
133       len -= 1;
134     } else if ((len > 0) && (line_buf[len - 1] == '\n')) {
135       // Since we read a new line we are done reading the line. This
136       // will exit the loop after copying this buffer into the result.
137       keep_going = false;
138     }
139     if (result == NULL) {
140       // Allocate the initial result and make room for the terminating '\0'
141       result = NewArray<char>(len + 1);
142     } else {
143       // Allocate a new result with enough room for the new addition.
144       int new_len = offset + len + 1;
145       char* new_result = NewArray<char>(new_len);
146       // Copy the existing input into the new array and set the new
147       // array as the result.
148       OS::MemCopy(new_result, result, offset * kCharSize);
149       DeleteArray(result);
150       result = new_result;
151     }
152     // Copy the newly read line into the result.
153     OS::MemCopy(result + offset, line_buf, len * kCharSize);
154     offset += len;
155   }
156   ASSERT(result != NULL);
157   result[offset] = '\0';
158   return result;
159 }
160
161
162 char* ReadCharsFromFile(FILE* file,
163                         int* size,
164                         int extra_space,
165                         bool verbose,
166                         const char* filename) {
167   if (file == NULL || fseek(file, 0, SEEK_END) != 0) {
168     if (verbose) {
169       OS::PrintError("Cannot read from file %s.\n", filename);
170     }
171     return NULL;
172   }
173
174   // Get the size of the file and rewind it.
175   *size = ftell(file);
176   rewind(file);
177
178   char* result = NewArray<char>(*size + extra_space);
179   for (int i = 0; i < *size && feof(file) == 0;) {
180     int read = static_cast<int>(fread(&result[i], 1, *size - i, file));
181     if (read != (*size - i) && ferror(file) != 0) {
182       fclose(file);
183       DeleteArray(result);
184       return NULL;
185     }
186     i += read;
187   }
188   return result;
189 }
190
191
192 char* ReadCharsFromFile(const char* filename,
193                         int* size,
194                         int extra_space,
195                         bool verbose) {
196   FILE* file = OS::FOpen(filename, "rb");
197   char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename);
198   if (file != NULL) fclose(file);
199   return result;
200 }
201
202
203 byte* ReadBytes(const char* filename, int* size, bool verbose) {
204   char* chars = ReadCharsFromFile(filename, size, 0, verbose);
205   return reinterpret_cast<byte*>(chars);
206 }
207
208
209 static Vector<const char> SetVectorContents(char* chars,
210                                             int size,
211                                             bool* exists) {
212   if (!chars) {
213     *exists = false;
214     return Vector<const char>::empty();
215   }
216   chars[size] = '\0';
217   *exists = true;
218   return Vector<const char>(chars, size);
219 }
220
221
222 Vector<const char> ReadFile(const char* filename,
223                             bool* exists,
224                             bool verbose) {
225   int size;
226   char* result = ReadCharsFromFile(filename, &size, 1, verbose);
227   return SetVectorContents(result, size, exists);
228 }
229
230
231 Vector<const char> ReadFile(FILE* file,
232                             bool* exists,
233                             bool verbose) {
234   int size;
235   char* result = ReadCharsFromFile(file, &size, 1, verbose, "");
236   return SetVectorContents(result, size, exists);
237 }
238
239
240 int WriteCharsToFile(const char* str, int size, FILE* f) {
241   int total = 0;
242   while (total < size) {
243     int write = static_cast<int>(fwrite(str, 1, size - total, f));
244     if (write == 0) {
245       return total;
246     }
247     total += write;
248     str += write;
249   }
250   return total;
251 }
252
253
254 int AppendChars(const char* filename,
255                 const char* str,
256                 int size,
257                 bool verbose) {
258   FILE* f = OS::FOpen(filename, "ab");
259   if (f == NULL) {
260     if (verbose) {
261       OS::PrintError("Cannot open file %s for writing.\n", filename);
262     }
263     return 0;
264   }
265   int written = WriteCharsToFile(str, size, f);
266   fclose(f);
267   return written;
268 }
269
270
271 int WriteChars(const char* filename,
272                const char* str,
273                int size,
274                bool verbose) {
275   FILE* f = OS::FOpen(filename, "wb");
276   if (f == NULL) {
277     if (verbose) {
278       OS::PrintError("Cannot open file %s for writing.\n", filename);
279     }
280     return 0;
281   }
282   int written = WriteCharsToFile(str, size, f);
283   fclose(f);
284   return written;
285 }
286
287
288 int WriteBytes(const char* filename,
289                const byte* bytes,
290                int size,
291                bool verbose) {
292   const char* str = reinterpret_cast<const char*>(bytes);
293   return WriteChars(filename, str, size, verbose);
294 }
295
296
297
298 void StringBuilder::AddFormatted(const char* format, ...) {
299   va_list arguments;
300   va_start(arguments, format);
301   AddFormattedList(format, arguments);
302   va_end(arguments);
303 }
304
305
306 void StringBuilder::AddFormattedList(const char* format, va_list list) {
307   ASSERT(!is_finalized() && position_ <= buffer_.length());
308   int n = OS::VSNPrintF(buffer_ + position_, format, list);
309   if (n < 0 || n >= (buffer_.length() - position_)) {
310     position_ = buffer_.length();
311   } else {
312     position_ += n;
313   }
314 }
315
316
317 } }  // namespace v8::internal