Advertisement
Infus

Converting file list to ModelPaths.lua (c++ souce)

Oct 26th, 2014
176
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <QCoreApplication>
  2. #include <QString>
  3. #include <QMap>
  4. #include <QFile>
  5. #include <QVector>
  6. #include <QStringList>
  7. #include <QDebug>
  8.  
  9. class Node
  10. {
  11. public:
  12. QByteArray name;
  13. QVector<Node> children;
  14. };
  15.  
  16. bool operator<(const Node &a, const Node &b)
  17. {
  18. return a.name.toLower() < b.name.toLower();
  19. }
  20.  
  21. bool operator<(const Node &a, const QByteArray &b)
  22. {
  23. return a.name.toLower() < b.toLower();
  24. }
  25.  
  26. void appendNode(QByteArray &result, const Node &n, int indentation)
  27. {
  28. QByteArray indentString(indentation, ' ');
  29. result += indentString + "{value = \"" + n.name + "\",\n"
  30. + indentString + "text = \"" + n.name + "\"";
  31. if(n.children.isEmpty()) {
  32. result += "},\n";
  33. } else {
  34. result += ",\n";
  35. result += indentString + " children = {\n";
  36. foreach (const Node &n, n.children)
  37. appendNode(result, n, indentation + 4);
  38. result += indentString +" }\n";
  39. result += indentString +"},\n";
  40. }
  41. }
  42.  
  43. void insertNodes(QVector<Node> &root, const QList<QByteArray> parts, int index)
  44. {
  45. if (index >= parts.size())
  46. return;
  47.  
  48. Node n;
  49. n.name = parts.at(index);
  50.  
  51. auto it = std::lower_bound(root.begin(), root.end(), n.name);
  52. if (it == root.end() || (*it).name.toLower() != n.name.toLower())
  53. it = root.insert(it, n);
  54.  
  55. insertNodes((*it).children, parts, index + 1);
  56. }
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60. QCoreApplication a(argc, argv);
  61.  
  62. if (a.arguments().size() < 2)
  63. return -1;
  64.  
  65. QFile inputFile(a.arguments().at(1));
  66. inputFile.open(QIODevice::ReadOnly);
  67.  
  68. QVector<Node> rootNodes;
  69.  
  70. qDebug() << "reading...";
  71. // read
  72. while (!inputFile.atEnd()) {
  73. QByteArray line = inputFile.readLine();
  74. line = line.trimmed();
  75. if (line.endsWith(".M2")) {
  76. line.truncate(line.size() - 2);
  77. line.append("m2");
  78. }
  79. if (!line.endsWith(".m2"))
  80. continue;
  81. if (line.left(4).toLower() == ("item"))
  82. continue;
  83. if (line.left(7).toLower() == ("cameras"))
  84. continue;
  85.  
  86. // qDebug() << line;
  87.  
  88. QList<QByteArray> parts = line.split('\\');
  89. insertNodes(rootNodes, parts, 0);
  90. }
  91. inputFile.close();
  92.  
  93. qDebug() << "dumping...";
  94.  
  95. // dump
  96. int indentation = 0;
  97. QByteArray result = "WeakAuras.ModelPaths = {\n";
  98. foreach (const Node &n, rootNodes)
  99. appendNode(result, n, indentation);
  100.  
  101. result += "};\n";
  102.  
  103. QFile outputFile("ModelPaths.lua");
  104. outputFile.open(QIODevice::WriteOnly);
  105. outputFile.write(result);
  106. outputFile.close();
  107.  
  108. return 0;
  109. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement