xref: /third_party/node/deps/v8/tools/shell-utils.h (revision 1cb0ef41)
11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
31cb0ef41Sopenharmony_ci// modification, are permitted provided that the following conditions are
41cb0ef41Sopenharmony_ci// met:
51cb0ef41Sopenharmony_ci//
61cb0ef41Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
71cb0ef41Sopenharmony_ci//       notice, this list of conditions and the following disclaimer.
81cb0ef41Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
91cb0ef41Sopenharmony_ci//       copyright notice, this list of conditions and the following
101cb0ef41Sopenharmony_ci//       disclaimer in the documentation and/or other materials provided
111cb0ef41Sopenharmony_ci//       with the distribution.
121cb0ef41Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
131cb0ef41Sopenharmony_ci//       contributors may be used to endorse or promote products derived
141cb0ef41Sopenharmony_ci//       from this software without specific prior written permission.
151cb0ef41Sopenharmony_ci//
161cb0ef41Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171cb0ef41Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181cb0ef41Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191cb0ef41Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201cb0ef41Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211cb0ef41Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221cb0ef41Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231cb0ef41Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241cb0ef41Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251cb0ef41Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261cb0ef41Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci// Utility functions used by parser-shell.
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#include "src/common/globals.h"
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci#include <stdio.h>
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_cinamespace v8 {
351cb0ef41Sopenharmony_cinamespace internal {
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cienum Encoding {
381cb0ef41Sopenharmony_ci  LATIN1,
391cb0ef41Sopenharmony_ci  UTF8,
401cb0ef41Sopenharmony_ci  UTF16
411cb0ef41Sopenharmony_ci};
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciconst byte* ReadFileAndRepeat(const char* name, int* size, int repeat) {
441cb0ef41Sopenharmony_ci  FILE* file = fopen(name, "rb");
451cb0ef41Sopenharmony_ci  *size = 0;
461cb0ef41Sopenharmony_ci  if (file == NULL) return NULL;
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  fseek(file, 0, SEEK_END);
491cb0ef41Sopenharmony_ci  int file_size = static_cast<int>(ftell(file));
501cb0ef41Sopenharmony_ci  rewind(file);
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  *size = file_size * repeat;
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  byte* chars = new byte[*size + 1];
551cb0ef41Sopenharmony_ci  for (int i = 0; i < file_size;) {
561cb0ef41Sopenharmony_ci    int read = static_cast<int>(fread(&chars[i], 1, file_size - i, file));
571cb0ef41Sopenharmony_ci    i += read;
581cb0ef41Sopenharmony_ci  }
591cb0ef41Sopenharmony_ci  fclose(file);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  for (int i = file_size; i < *size; i++) {
621cb0ef41Sopenharmony_ci    chars[i] = chars[i - file_size];
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci  chars[*size] = 0;
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  return chars;
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci}  // namespace internal
701cb0ef41Sopenharmony_ci}  // namespace v8
71