blob: 9310301a15e8dae6cdd2ad0c7a8c66921374a6cc [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
28#include <stdarg.h>
29
30#include "v8.h"
31
32#include "platform.h"
33
34#include "sys/stat.h"
35
36namespace v8 { namespace internal {
37
38
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000039// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
40// figure 3-3, page 48, where the function is called clp2.
41uint32_t RoundUpToPowerOf2(uint32_t x) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000042 x = x - 1;
43 x = x | (x >> 1);
44 x = x | (x >> 2);
45 x = x | (x >> 4);
46 x = x | (x >> 8);
47 x = x | (x >> 16);
48 return x + 1;
49}
50
51
52byte* EncodeInt(byte* p, int x) {
53 while (x < -64 || x >= 64) {
54 *p++ = static_cast<byte>(x & 127);
55 x = ArithmeticShiftRight(x, 7);
56 }
57 // -64 <= x && x < 64
58 *p++ = static_cast<byte>(x + 192);
59 return p;
60}
61
62
63byte* DecodeInt(byte* p, int* x) {
64 int r = 0;
65 unsigned int s = 0;
66 byte b = *p++;
67 while (b < 128) {
68 r |= static_cast<int>(b) << s;
69 s += 7;
70 b = *p++;
71 }
72 // b >= 128
73 *x = r | ((static_cast<int>(b) - 192) << s);
74 return p;
75}
76
77
78byte* EncodeUnsignedIntBackward(byte* p, unsigned int x) {
79 while (x >= 128) {
80 *--p = static_cast<byte>(x & 127);
81 x = x >> 7;
82 }
83 // x < 128
84 *--p = static_cast<byte>(x + 128);
85 return p;
86}
87
88
89void PrintF(const char* format, ...) {
90 va_list arguments;
91 va_start(arguments, format);
92 OS::VPrint(format, arguments);
93 va_end(arguments);
94}
95
96
97void Flush() {
98 fflush(stdout);
99}
100
101
102char* ReadLine(const char* prompt) {
103 char* result = NULL;
104 char line_buf[256];
105 int offset = 0;
106 bool keep_going = true;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000107 fprintf(stdout, "%s", prompt);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 fflush(stdout);
109 while (keep_going) {
110 if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) {
111 // fgets got an error. Just give up.
112 if (result != NULL) {
113 DeleteArray(result);
114 }
115 return NULL;
116 }
117 int len = strlen(line_buf);
118 if (len > 1 &&
119 line_buf[len - 2] == '\\' &&
120 line_buf[len - 1] == '\n') {
121 // When we read a line that ends with a "\" we remove the escape and
122 // append the remainder.
123 line_buf[len - 2] = '\n';
124 line_buf[len - 1] = 0;
125 len -= 1;
126 } else if ((len > 0) && (line_buf[len - 1] == '\n')) {
127 // Since we read a new line we are done reading the line. This
128 // will exit the loop after copying this buffer into the result.
129 keep_going = false;
130 }
131 if (result == NULL) {
132 // Allocate the initial result and make room for the terminating '\0'
133 result = NewArray<char>(len + 1);
134 } else {
135 // Allocate a new result with enough room for the new addition.
136 int new_len = offset + len + 1;
137 char* new_result = NewArray<char>(new_len);
138 // Copy the existing input into the new array and set the new
139 // array as the result.
140 memcpy(new_result, result, offset * kCharSize);
141 DeleteArray(result);
142 result = new_result;
143 }
144 // Copy the newly read line into the result.
145 memcpy(result + offset, line_buf, len * kCharSize);
146 offset += len;
147 }
148 ASSERT(result != NULL);
149 result[offset] = '\0';
150 return result;
151}
152
153
154char* ReadCharsFromFile(const char* filename,
155 int* size,
156 int extra_space,
157 bool verbose) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000158 FILE* file = OS::FOpen(filename, "rb");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000159 if (file == NULL || fseek(file, 0, SEEK_END) != 0) {
160 if (verbose) {
161 OS::PrintError("Cannot read from file %s.\n", filename);
162 }
163 return NULL;
164 }
165
166 // Get the size of the file and rewind it.
167 *size = ftell(file);
168 rewind(file);
169
170 char* result = NewArray<char>(*size + extra_space);
171 for (int i = 0; i < *size;) {
172 int read = fread(&result[i], 1, *size - i, file);
173 if (read <= 0) {
174 fclose(file);
175 DeleteArray(result);
176 return NULL;
177 }
178 i += read;
179 }
180 fclose(file);
181 return result;
182}
183
184
185char* ReadChars(const char* filename, int* size, bool verbose) {
186 return ReadCharsFromFile(filename, size, 0, verbose);
187}
188
189
190Vector<const char> ReadFile(const char* filename,
191 bool* exists,
192 bool verbose) {
193 int size;
194 char* result = ReadCharsFromFile(filename, &size, 1, verbose);
195 if (!result) {
196 *exists = false;
197 return Vector<const char>::empty();
198 }
199 result[size] = '\0';
200 *exists = true;
201 return Vector<const char>(result, size);
202}
203
204
205int WriteCharsToFile(const char* str, int size, FILE* f) {
206 int total = 0;
207 while (total < size) {
208 int write = fwrite(str, 1, size - total, f);
209 if (write == 0) {
210 return total;
211 }
212 total += write;
213 str += write;
214 }
215 return total;
216}
217
218
219int WriteChars(const char* filename,
220 const char* str,
221 int size,
222 bool verbose) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000223 FILE* f = OS::FOpen(filename, "wb");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 if (f == NULL) {
225 if (verbose) {
226 OS::PrintError("Cannot open file %s for reading.\n", filename);
227 }
228 return 0;
229 }
230 int written = WriteCharsToFile(str, size, f);
231 fclose(f);
232 return written;
233}
234
235
kasper.lund7276f142008-07-30 08:49:36 +0000236StringBuilder::StringBuilder(int size) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000237 buffer_ = Vector<char>::New(size);
kasper.lund7276f142008-07-30 08:49:36 +0000238 position_ = 0;
239}
240
241
242void StringBuilder::AddString(const char* s) {
243 AddSubstring(s, strlen(s));
244}
245
246
247void StringBuilder::AddSubstring(const char* s, int n) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000248 ASSERT(!is_finalized() && position_ + n < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +0000249 ASSERT(static_cast<size_t>(n) <= strlen(s));
250 memcpy(&buffer_[position_], s, n * kCharSize);
251 position_ += n;
252}
253
254
255void StringBuilder::AddFormatted(const char* format, ...) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000256 ASSERT(!is_finalized() && position_ < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +0000257 va_list args;
258 va_start(args, format);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000259 int n = OS::VSNPrintF(buffer_ + position_, format, args);
kasper.lund7276f142008-07-30 08:49:36 +0000260 va_end(args);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000261 if (n < 0 || n >= (buffer_.length() - position_)) {
262 position_ = buffer_.length();
kasper.lund7276f142008-07-30 08:49:36 +0000263 } else {
264 position_ += n;
265 }
266}
267
268
269void StringBuilder::AddPadding(char c, int count) {
270 for (int i = 0; i < count; i++) {
271 AddCharacter(c);
272 }
273}
274
275
276char* StringBuilder::Finalize() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000277 ASSERT(!is_finalized() && position_ < buffer_.length());
kasper.lund7276f142008-07-30 08:49:36 +0000278 buffer_[position_] = '\0';
279 // Make sure nobody managed to add a 0-character to the
280 // buffer while building the string.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000281 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
kasper.lund7276f142008-07-30 08:49:36 +0000282 position_ = -1;
283 ASSERT(is_finalized());
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000284 return buffer_.start();
kasper.lund7276f142008-07-30 08:49:36 +0000285}
286
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000287} } // namespace v8::internal