All pastes #1958967 Raw Edit

jsbeautify + ugly hack

public cpp v1 · immutable
#1958967 ·published 2010-10-11 00:07 UTC
rendered paste body
/*    This file is part of the Ofi Labs X2 project.    Copyright (C) 2010 Ariya Hidayat <ariya.hidayat@gmail.com>    Based on http://jsbeautifier.org/    Copyright (c) 2009 Einar Lielmanis    Permission is hereby granted, free of charge, to any person    obtaining a copy of this software and associated documentation    files (the "Software"), to deal in the Software without    restriction, including without limitation the rights to use,    copy, modify, merge, publish, distribute, sublicense, and/or sell    copies of the Software, and to permit persons to whom the    Software is furnished to do so, subject to the following    conditions:    The above copyright notice and this permission notice shall be    included in all copies or substantial portions of the Software.    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR    OTHER DEALINGS IN THE SOFTWARE. */#include <QtScript>#include <iostream>static QString readFile(const QString &fileName){  QFile file;  file.setFileName(fileName);  if (!file.open(QFile::ReadOnly)) {    std::cerr << "Can't open " << qPrintable(fileName) << std::endl;    return QString();  }  QString content = file.readAll();  file.close();  return content;}static bool writeFile(const QString &content, const QString& fileName, bool overwrite = false){  QFile file;  file.setFileName(fileName);  if (overwrite) {    QString backupFileName = fileName + ".orig";    QFile f(fileName + ".orig");    if (f.exists()) {      if (!f.remove())        std::cerr << "cant't overwrite backup file " << qPrintable(backupFileName) << " (giving up)" << std::endl;    }  }  if (!file.rename(fileName + ".orig")) {    std::cerr << "can't backup original file to " << qPrintable(fileName + ".orig") << " (giving up)" << std::endl;    return false;  }  file.setFileName(fileName);  if (!file.open(QFile::WriteOnly | QIODevice::Text)) {    std::cerr << "Can't open " << qPrintable(fileName) << " for writing" << std::endl;    return false;  }  QTextStream out(&file);  out << content;  file.close();  return true;}int main(int argc, char **argv){  QCoreApplication app(argc, argv);  if (argc < 2) {    std::cout << "Usage:" << std::endl << std::endl;    std::cout << "    jsbeautify [--overwrite] source-file" << std::endl << std::endl;    return 0;  }  QString script = readFile(":/beautify.js");  if (script.isEmpty()) {    std::cerr << "script file empty" << std::endl;    return 0;  }  QScriptEngine engine;  engine.evaluate(script);  QStringList args = app.arguments();  args.removeFirst(); // might cause issues on some systems where argv[0] is not the program name  bool overwrite = args.contains("--overwrite");  if (overwrite)    args.removeAll("--overwrite");  for (QStringList::Iterator iter = args.begin(); iter != args.end(); ++iter) {    QString fileName = *iter;    QString source = readFile(fileName);    if (source.isEmpty())      continue;    engine.globalObject().setProperty("source", QScriptValue(source));    QScriptValue result = engine.evaluate("js_beautify(source);");    if (source == result.toString())      std::cout << "No changes in " << qPrintable(fileName) <<  " (skipping)" << std::endl;    else      writeFile(result.toString(), fileName, overwrite);  }  return 0;}