| 1 |
/* |
| 2 |
Copyright 2019 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <fstream> |
| 18 |
#include <iostream> |
| 19 |
|
| 20 |
#include "utils.h" |
| 21 |
|
| 22 |
long strtol_check(const std::string &str) |
| 23 |
{ |
| 24 |
const char *arg = str.c_str(); |
| 25 |
char *ep; |
| 26 |
long r = std::strtol(arg, &ep, 10); |
| 27 |
if (*ep != '\0' || *arg == '\0') { |
| 28 |
std::cerr << "ERROR: invalid integer \"" << arg << "\"\n" << std::endl; |
| 29 |
exit(EXIT_FAILURE); |
| 30 |
} |
| 31 |
return r; |
| 32 |
} |
| 33 |
|
| 34 |
std::string next_arg(unsigned &i, const std::vector<std::string> &args) |
| 35 |
{ |
| 36 |
i++; |
| 37 |
if (i >= args.size()) { |
| 38 |
std::cerr << "Expected another commandline argument." << std::endl; |
| 39 |
exit(EXIT_FAILURE); |
| 40 |
} |
| 41 |
return args[i]; |
| 42 |
} |
| 43 |
|
| 44 |
std::vector<std::string> simplify_args(int argc, const char **argv) |
| 45 |
{ |
| 46 |
std::vector<std::string> r; |
| 47 |
for (int i = 1; i < argc; ++i) { |
| 48 |
std::string arg = argv[i]; |
| 49 |
if (arg == "--") { |
| 50 |
// Add this arg and all remaining ones without simplification. |
| 51 |
r.push_back(arg); |
| 52 |
while ((++i) < argc) |
| 53 |
r.push_back(argv[i]); |
| 54 |
break; |
| 55 |
} |
| 56 |
// Check if it is of the form -abc and convert to -a -b -c |
| 57 |
if (arg.length() > 2 && arg[0] == '-' && arg[1] != '-') { |
| 58 |
for (unsigned j = 1; j < arg.length(); ++j) { |
| 59 |
r.push_back("-" + arg.substr(j, 1)); |
| 60 |
} |
| 61 |
} else { |
| 62 |
r.push_back(arg); |
| 63 |
} |
| 64 |
} |
| 65 |
return r; |
| 66 |
} |
| 67 |
|
| 68 |
bool read_input_content(std::string filename, std::string *input) |
| 69 |
{ |
| 70 |
// Input file "-" tells Jsonnet to read stdin. |
| 71 |
if (filename == "-") { |
| 72 |
input->assign(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>()); |
| 73 |
} else { |
| 74 |
std::ifstream f; |
| 75 |
f.open(filename); |
| 76 |
if (!f.good()) { |
| 77 |
std::string msg = "Opening input file: " + filename; |
| 78 |
perror(msg.c_str()); |
| 79 |
return false; |
| 80 |
} |
| 81 |
input->assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()); |
| 82 |
if (!f.good()) { |
| 83 |
std::string msg = "Reading input file: " + filename; |
| 84 |
perror(msg.c_str()); |
| 85 |
return false; |
| 86 |
} |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
void change_special_filename(bool filename_is_code, std::string *filename) |
| 92 |
{ |
| 93 |
if (filename_is_code) { |
| 94 |
*filename = "<cmdline>"; |
| 95 |
} else if (*filename == "-") { |
| 96 |
*filename = "<stdin>"; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
bool read_input( |
| 101 |
bool filename_is_code, std::string *filename, std::string *input) |
| 102 |
{ |
| 103 |
bool ok; |
| 104 |
if (filename_is_code) { |
| 105 |
*input = *filename; |
| 106 |
ok = true; |
| 107 |
} else { |
| 108 |
ok = read_input_content(*filename, input); |
| 109 |
} |
| 110 |
// Let's change the filename to something we can show the user |
| 111 |
// if it is not a real filename. |
| 112 |
change_special_filename(filename_is_code, filename); |
| 113 |
return ok; |
| 114 |
} |
| 115 |
|
| 116 |
bool write_output_file(const char *output, const std::string &output_file) |
| 117 |
{ |
| 118 |
std::ostream *o; |
| 119 |
std::ofstream f; |
| 120 |
|
| 121 |
if (output_file.empty()) { |
| 122 |
o = &std::cout; |
| 123 |
} else { |
| 124 |
f.open(output_file.c_str()); |
| 125 |
if (!f.good()) { |
| 126 |
std::string msg = "Writing to output file: " + output_file; |
| 127 |
perror(msg.c_str()); |
| 128 |
return false; |
| 129 |
} |
| 130 |
o = &f; |
| 131 |
} |
| 132 |
|
| 133 |
(*o) << output; |
| 134 |
|
| 135 |
if (output_file.empty()) { |
| 136 |
std::cout.flush(); |
| 137 |
} else { |
| 138 |
f.close(); |
| 139 |
if (!f.good()) { |
| 140 |
std::string msg = "Writing to output file: " + output_file; |
| 141 |
perror(msg.c_str()); |
| 142 |
return false; |
| 143 |
} |
| 144 |
} |
| 145 |
return true; |
| 146 |
} |
| 147 |
|