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.
18 SimpleStringBuilder::SimpleStringBuilder(int size) {
19 buffer_ = Vector<char>::New(size);
24 void SimpleStringBuilder::AddString(const char* s) {
25 AddSubstring(s, StrLength(s));
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);
37 void SimpleStringBuilder::AddPadding(char c, int count) {
38 for (int i = 0; i < count; i++) {
44 void SimpleStringBuilder::AddDecimalInteger(int32_t value) {
45 uint32_t number = static_cast<uint32_t>(value);
48 number = static_cast<uint32_t>(-value);
51 for (uint32_t factor = 10; digits < 10; digits++, factor *= 10) {
52 if (factor > number) break;
55 for (int i = 1; i <= digits; i++) {
56 buffer_[position_ - i] = '0' + static_cast<char>(number % 10);
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()) {
68 for (int i = 3; i > 0 && position_ > i; --i) buffer_[position_ - i] = '.';
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_));
75 ASSERT(is_finalized());
76 return buffer_.start();
80 void PrintF(const char* format, ...) {
82 va_start(arguments, format);
83 OS::VPrint(format, arguments);
88 void PrintF(FILE* out, const char* format, ...) {
90 va_start(arguments, format);
91 OS::VFPrint(out, format, arguments);
96 void PrintPID(const char* format, ...) {
97 OS::Print("[%d] ", OS::GetCurrentProcessId());
99 va_start(arguments, format);
100 OS::VPrint(format, arguments);
105 void Flush(FILE* out) {
110 char* ReadLine(const char* prompt) {
114 bool keep_going = true;
115 fprintf(stdout, "%s", prompt);
118 if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) {
119 // fgets got an error. Just give up.
120 if (result != NULL) {
125 int len = StrLength(line_buf);
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;
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.
139 if (result == NULL) {
140 // Allocate the initial result and make room for the terminating '\0'
141 result = NewArray<char>(len + 1);
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);
152 // Copy the newly read line into the result.
153 OS::MemCopy(result + offset, line_buf, len * kCharSize);
156 ASSERT(result != NULL);
157 result[offset] = '\0';
162 char* ReadCharsFromFile(FILE* file,
166 const char* filename) {
167 if (file == NULL || fseek(file, 0, SEEK_END) != 0) {
169 OS::PrintError("Cannot read from file %s.\n", filename);
174 // Get the size of the file and rewind it.
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) {
192 char* ReadCharsFromFile(const char* filename,
196 FILE* file = OS::FOpen(filename, "rb");
197 char* result = ReadCharsFromFile(file, size, extra_space, verbose, filename);
198 if (file != NULL) fclose(file);
203 byte* ReadBytes(const char* filename, int* size, bool verbose) {
204 char* chars = ReadCharsFromFile(filename, size, 0, verbose);
205 return reinterpret_cast<byte*>(chars);
209 static Vector<const char> SetVectorContents(char* chars,
214 return Vector<const char>::empty();
218 return Vector<const char>(chars, size);
222 Vector<const char> ReadFile(const char* filename,
226 char* result = ReadCharsFromFile(filename, &size, 1, verbose);
227 return SetVectorContents(result, size, exists);
231 Vector<const char> ReadFile(FILE* file,
235 char* result = ReadCharsFromFile(file, &size, 1, verbose, "");
236 return SetVectorContents(result, size, exists);
240 int WriteCharsToFile(const char* str, int size, FILE* f) {
242 while (total < size) {
243 int write = static_cast<int>(fwrite(str, 1, size - total, f));
254 int AppendChars(const char* filename,
258 FILE* f = OS::FOpen(filename, "ab");
261 OS::PrintError("Cannot open file %s for writing.\n", filename);
265 int written = WriteCharsToFile(str, size, f);
271 int WriteChars(const char* filename,
275 FILE* f = OS::FOpen(filename, "wb");
278 OS::PrintError("Cannot open file %s for writing.\n", filename);
282 int written = WriteCharsToFile(str, size, f);
288 int WriteBytes(const char* filename,
292 const char* str = reinterpret_cast<const char*>(bytes);
293 return WriteChars(filename, str, size, verbose);
298 void StringBuilder::AddFormatted(const char* format, ...) {
300 va_start(arguments, format);
301 AddFormattedList(format, arguments);
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();
317 } } // namespace v8::internal