| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 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 |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 if (write == 0) { | 161 if (write == 0) { |
| 162 return total; | 162 return total; |
| 163 } | 163 } |
| 164 total += write; | 164 total += write; |
| 165 str += write; | 165 str += write; |
| 166 } | 166 } |
| 167 return total; | 167 return total; |
| 168 } | 168 } |
| 169 | 169 |
| 170 | 170 |
| 171 int AppendChars(const char* filename, |
| 172 const char* str, |
| 173 int size, |
| 174 bool verbose) { |
| 175 FILE* f = OS::FOpen(filename, "ab"); |
| 176 if (f == NULL) { |
| 177 if (verbose) { |
| 178 OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 179 } |
| 180 return 0; |
| 181 } |
| 182 int written = WriteCharsToFile(str, size, f); |
| 183 fclose(f); |
| 184 return written; |
| 185 } |
| 186 |
| 187 |
| 171 int WriteChars(const char* filename, | 188 int WriteChars(const char* filename, |
| 172 const char* str, | 189 const char* str, |
| 173 int size, | 190 int size, |
| 174 bool verbose) { | 191 bool verbose) { |
| 175 FILE* f = OS::FOpen(filename, "wb"); | 192 FILE* f = OS::FOpen(filename, "wb"); |
| 176 if (f == NULL) { | 193 if (f == NULL) { |
| 177 if (verbose) { | 194 if (verbose) { |
| 178 OS::PrintError("Cannot open file %s for writing.\n", filename); | 195 OS::PrintError("Cannot open file %s for writing.\n", filename); |
| 179 } | 196 } |
| 180 return 0; | 197 return 0; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 207 | 224 |
| 208 void StringBuilder::AddSubstring(const char* s, int n) { | 225 void StringBuilder::AddSubstring(const char* s, int n) { |
| 209 ASSERT(!is_finalized() && position_ + n < buffer_.length()); | 226 ASSERT(!is_finalized() && position_ + n < buffer_.length()); |
| 210 ASSERT(static_cast<size_t>(n) <= strlen(s)); | 227 ASSERT(static_cast<size_t>(n) <= strlen(s)); |
| 211 memcpy(&buffer_[position_], s, n * kCharSize); | 228 memcpy(&buffer_[position_], s, n * kCharSize); |
| 212 position_ += n; | 229 position_ += n; |
| 213 } | 230 } |
| 214 | 231 |
| 215 | 232 |
| 216 void StringBuilder::AddFormatted(const char* format, ...) { | 233 void StringBuilder::AddFormatted(const char* format, ...) { |
| 234 va_list arguments; |
| 235 va_start(arguments, format); |
| 236 AddFormattedList(format, arguments); |
| 237 va_end(arguments); |
| 238 } |
| 239 |
| 240 |
| 241 void StringBuilder::AddFormattedList(const char* format, va_list list) { |
| 217 ASSERT(!is_finalized() && position_ < buffer_.length()); | 242 ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 218 va_list args; | 243 int n = OS::VSNPrintF(buffer_ + position_, format, list); |
| 219 va_start(args, format); | |
| 220 int n = OS::VSNPrintF(buffer_ + position_, format, args); | |
| 221 va_end(args); | |
| 222 if (n < 0 || n >= (buffer_.length() - position_)) { | 244 if (n < 0 || n >= (buffer_.length() - position_)) { |
| 223 position_ = buffer_.length(); | 245 position_ = buffer_.length(); |
| 224 } else { | 246 } else { |
| 225 position_ += n; | 247 position_ += n; |
| 226 } | 248 } |
| 227 } | 249 } |
| 228 | 250 |
| 229 | 251 |
| 230 void StringBuilder::AddPadding(char c, int count) { | 252 void StringBuilder::AddPadding(char c, int count) { |
| 231 for (int i = 0; i < count; i++) { | 253 for (int i = 0; i < count; i++) { |
| 232 AddCharacter(c); | 254 AddCharacter(c); |
| 233 } | 255 } |
| 234 } | 256 } |
| 235 | 257 |
| 236 | 258 |
| 237 char* StringBuilder::Finalize() { | 259 char* StringBuilder::Finalize() { |
| 238 ASSERT(!is_finalized() && position_ < buffer_.length()); | 260 ASSERT(!is_finalized() && position_ < buffer_.length()); |
| 239 buffer_[position_] = '\0'; | 261 buffer_[position_] = '\0'; |
| 240 // Make sure nobody managed to add a 0-character to the | 262 // Make sure nobody managed to add a 0-character to the |
| 241 // buffer while building the string. | 263 // buffer while building the string. |
| 242 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); | 264 ASSERT(strlen(buffer_.start()) == static_cast<size_t>(position_)); |
| 243 position_ = -1; | 265 position_ = -1; |
| 244 ASSERT(is_finalized()); | 266 ASSERT(is_finalized()); |
| 245 return buffer_.start(); | 267 return buffer_.start(); |
| 246 } | 268 } |
| 247 | 269 |
| 248 | 270 |
| 249 } } // namespace v8::internal | 271 } } // namespace v8::internal |
| OLD | NEW |