00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdarg.h>
00029
00030 #include "v8.h"
00031
00032 #include "platform.h"
00033
00034 #include "sys/stat.h"
00035
00036 namespace v8 { namespace internal {
00037
00038
00039
00040
00041 uint32_t RoundUpToPowerOf2(uint32_t x) {
00042 x = x - 1;
00043 x = x | (x >> 1);
00044 x = x | (x >> 2);
00045 x = x | (x >> 4);
00046 x = x | (x >> 8);
00047 x = x | (x >> 16);
00048 return x + 1;
00049 }
00050
00051
00052 byte* EncodeInt(byte* p, int x) {
00053 while (x < -64 || x >= 64) {
00054 *p++ = static_cast<byte>(x & 127);
00055 x = ArithmeticShiftRight(x, 7);
00056 }
00057
00058 *p++ = static_cast<byte>(x + 192);
00059 return p;
00060 }
00061
00062
00063 byte* DecodeInt(byte* p, int* x) {
00064 int r = 0;
00065 unsigned int s = 0;
00066 byte b = *p++;
00067 while (b < 128) {
00068 r |= static_cast<int>(b) << s;
00069 s += 7;
00070 b = *p++;
00071 }
00072
00073 *x = r | ((static_cast<int>(b) - 192) << s);
00074 return p;
00075 }
00076
00077
00078 byte* EncodeUnsignedIntBackward(byte* p, unsigned int x) {
00079 while (x >= 128) {
00080 *--p = static_cast<byte>(x & 127);
00081 x = x >> 7;
00082 }
00083
00084 *--p = static_cast<byte>(x + 128);
00085 return p;
00086 }
00087
00088
00089 void PrintF(const char* format, ...) {
00090 va_list arguments;
00091 va_start(arguments, format);
00092 OS::VPrint(format, arguments);
00093 va_end(arguments);
00094 }
00095
00096
00097 void Flush() {
00098 fflush(stdout);
00099 }
00100
00101
00102 char* ReadLine(const char* prompt) {
00103 char* result = NULL;
00104 char line_buf[256];
00105 int offset = 0;
00106 bool keep_going = true;
00107 fprintf(stdout, "%s", prompt);
00108 fflush(stdout);
00109 while (keep_going) {
00110 if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) {
00111
00112 if (result != NULL) {
00113 DeleteArray(result);
00114 }
00115 return NULL;
00116 }
00117 int len = strlen(line_buf);
00118 if (len > 1 &&
00119 line_buf[len - 2] == '\\' &&
00120 line_buf[len - 1] == '\n') {
00121
00122
00123 line_buf[len - 2] = '\n';
00124 line_buf[len - 1] = 0;
00125 len -= 1;
00126 } else if ((len > 0) && (line_buf[len - 1] == '\n')) {
00127
00128
00129 keep_going = false;
00130 }
00131 if (result == NULL) {
00132
00133 result = NewArray<char>(len + 1);
00134 } else {
00135
00136 int new_len = offset + len + 1;
00137 char* new_result = NewArray<char>(new_len);
00138
00139
00140 memcpy(new_result, result, offset * kCharSize);
00141 DeleteArray(result);
00142 result = new_result;
00143 }
00144
00145 memcpy(result + offset, line_buf, len * kCharSize);
00146 offset += len;
00147 }
00148 ASSERT(result != NULL);
00149 result[offset] = '\0';
00150 return result;
00151 }
00152
00153
00154 char* ReadCharsFromFile(const char* filename,
00155 int* size,
00156 int extra_space,
00157 bool verbose) {
00158 FILE* file = OS::FOpen(filename, "rb");
00159 if (file == NULL || fseek(file, 0, SEEK_END) != 0) {
00160 if (verbose) {
00161 OS::PrintError("Cannot read from file %s.\n", filename);
00162 }
00163 return NULL;
00164 }
00165
00166
00167 *size = ftell(file);
00168 rewind(file);
00169
00170 char* result = NewArray<char>(*size + extra_space);
00171 for (int i = 0; i < *size;) {
00172 int read = fread(&result[i], 1, *size - i, file);
00173 if (read <= 0) {
00174 fclose(file);
00175 DeleteArray(result);
00176 return NULL;
00177 }
00178 i += read;
00179 }
00180 fclose(file);
00181 return result;
00182 }
00183
00184
00185 char* ReadChars(const char* filename, int* size, bool verbose) {
00186 return ReadCharsFromFile(filename, size, 0, verbose);
00187 }
00188
00189
00190 Vector<const char> ReadFile(const char* filename,
00191 bool* exists,
00192 bool verbose) {
00193 int size;
00194 char* result = ReadCharsFromFile(filename, &size, 1, verbose);
00195 if (!result) {
00196 *exists = false;
00197 return Vector<const char>::empty();
00198 }
00199 result[size] = '\0';
00200 *exists = true;
00201 return Vector<const char>(result, size);
00202 }
00203
00204
00205 int WriteCharsToFile(const char* str, int size, FILE* f) {
00206 int total = 0;
00207 while (total < size) {
00208 int write = fwrite(str, 1, size - total, f);
00209 if (write == 0) {
00210 return total;
00211 }
00212 total += write;
00213 str += write;
00214 }
00215 return total;
00216 }
00217
00218
00219 int WriteChars(const char* filename,
00220 const char* str,
00221 int size,
00222 bool verbose) {
00223 FILE* f = OS::FOpen(filename, "wb");
00224 if (f == NULL) {
00225 if (verbose) {
00226 OS::PrintError("Cannot open file %s for reading.\n", filename);
00227 }
00228 return 0;
00229 }
00230 int written = WriteCharsToFile(str, size, f);
00231 fclose(f);
00232 return written;
00233 }
00234
00235
00236 StringBuilder::StringBuilder(int size) {
00237 buffer_ = Vector<char>::New(size);
00238 position_ = 0;
00239 }
00240
00241
00242 void StringBuilder::AddString(const char* s) {
00243 AddSubstring(s, strlen(s));
00244 }
00245
00246
00247 void StringBuilder::AddSubstring(const char* s, int n) {
00248 ASSERT(!is_finalized() && position_ + n < buffer_.length());
00249 ASSERT(static_cast<size_t>(n) <= strlen(s));
00250 memcpy(&buffer_[position_], s, n * kCharSize);
00251 position_ += n;
00252 }
00253
00254
00255 void StringBuilder::AddFormatted(const char* format, ...) {
00256 ASSERT(!is_finalized() && position_ < buffer_.length());
00257 va_list args;
00258 va_start(args, format);
00259 int n = OS::VSNPrintF(buffer_ + position_, format, args);
00260 va_end(args);
00261 if (n < 0 || n >= (buffer_.length() - position_)) {
00262 position_ = buffer_.length();
00263 } else {
00264 position_ += n;
00265 }
00266 }
00267
00268
00269 void StringBuilder::AddPadding(char c, int count) {
00270 for (int i = 0; i < count; i++) {
00271 AddCharacter(c);
00272 }
00273 }
00274
00275
00276 char* StringBuilder::Finalize() {
00277 ASSERT(!is_finalized() && position_ < buffer_.length());
00278 buffer_[position_] = '\0';
00279
00280
00281 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_));
00282 position_ = -1;
00283 ASSERT(is_finalized());
00284 return buffer_.start();
00285 }
00286
00287 } }