Files
pip/utils/resources_compiler/main.cpp
2022-05-13 13:24:09 +03:00

99 lines
3.2 KiB
C++

/*
PIP - Platform Independent Primitives
Resources compiller
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "parser.h"
#include "generator.h"
#include "picli.h"
#include "piiostream.h"
using namespace PICoutManipulators;
void usage() {
piCout << Bold << "PIP Resources Compiler";
piCout << Cyan << "Version" << Bold << PIPVersion() << NewLine;
piCout << Green << Bold << "Usage:" << Default << "\"pip_rc [-hl] [-n <name>] -i <in_file> -o <out_file>\"" << NewLine;
piCout << Green << Bold << "Details:";
piCout << "-h --help " << Green << "- display this message and exit";
piCout << "-n --name <name> " << Green << "- name of initialize function, by default is <out_file> base name";
piCout << "-i --input <in_file> " << Green << "- resources description file";
piCout << "-o --out <out_file> " << Green << "- output .cpp file";
piCout << "-l --list " << Green << "- print readed files from description and exit";
piCout << "-s --silent " << Green << "- no console output";
}
int main (int argc, char * argv[]) {
PICLI cli(argc, argv);
cli.addArgument("input", true);
cli.addArgument("out", true);
cli.addArgument("name", true);
cli.addArgument("help");
cli.addArgument("list");
cli.addArgument("silent");
if (cli.hasArgument("silent"))
piDebug = false;
if (cli.hasArgument("help") || !cli.hasArgument("input") ||
(!cli.hasArgument("out") && !cli.hasArgument("list"))) {
usage();
return 0;
}
PIVector<ParserSection> files = parse(cli.argumentValue("input"));
if (files.isEmpty()) {
piCout << "Error: resources description file is empty";
return 0;
}
if (cli.hasArgument("list")) {
piForeachC (ParserSection & s, files) {
piForeachC (ParserEntry & e, s.files) {
piCout << e.path;
}
}
return 0;
}
PIString out_file = cli.argumentValue("out");
PIString init_name = cli.argumentValue("name");
init_name = initName(init_name.isEmpty() ? out_file : init_name);
PIFile outf;
if (!out_file.isEmpty()) {
if (outf.open(out_file, PIIODevice::ReadWrite)) {
outf.clear();
} else {
piCout << "Error: can`t open out file" << out_file;
return 1;
}
PIIOTextStream ts(&outf);
ts << "// Generated by \"PIP Resources Compiler\" " << PIDateTime::current().toString("dd.MM.yyyy hh:mm:ss\n");
ts << "// Execute command:\n";
piForeachC (PIString & _a, cli.rawArguments())
ts << "// \"" << _a << "\"\n";
ts << "\n";
if (!generate(init_name, outf, files)) {
piCout << "Error: generate fail";
return 1;
}
}
return 0;
}