void destroyFiles(trieNodePointer root) { for (int i=0; i<36; i++) { if (root->children[i] != NULL) { destroyFiles(root->children[i]); } if (root->files != NULL) { fileNodePointer temp = root->files; fileNodePointer oldtemp = NULL; while (temp != NULL) { oldtemp = temp; temp = temp->next; free(oldtemp->fileName); free(oldtemp); } free(temp); root->files = NULL; } if (root->token != NULL) { free(root->token); root->token = NULL; } } }
void destroy() { destroyFiles(ROOT); destroyNodes(ROOT); }
int main(int argc, char **argv) { // we need the -schema <schema>, -input <json file or directory containing $(basename schema)*.json> and -pass <bool> QStringList arguments; std::string schema, input; bool pass = true; for (int i = 0; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 's': if (strcmp(argv[i], "-schema") == 0) { schema = argv[++i]; continue; } break; case 'i': if (strcmp(argv[i], "-input") == 0) { input = argv[++i]; continue; } break; case 'p': if (strcmp(argv[i], "-pass") == 0) { pass = strcmp(argv[++i], "0") != 0; continue; } break; case '-': if (strcmp(argv[i], "--help") == 0) { printUsage(stdout, 1, argc, argv); } } } arguments += argv[i]; } raw_buffer schemaData; QFileInfo schemaFileInfo(QString::fromStdString(schema)); FilePtr schemaFile(map(schemaFileInfo.absoluteFilePath(), schemaData)); if (schemaFile.isNull()) printUsage(stderr, 2, argc, argv); resolutionDir = schemaFileInfo.dir(); qDebug() << "Looking for schemas in" << resolutionDir.absolutePath(); QList<FilePtr> fileHandles; pjson::testc::Inputs jsonInput; QString inputQStr = QString::fromStdString(input); QFileInfo inputInfo(inputQStr); QDir inputsDir(inputInfo.dir()); if (inputInfo.isDir()) { QStringList nameFilters; nameFilters += QFileInfo(QString::fromStdString(schema)).completeBaseName() + "*.json"; QFileInfoList tests = inputsDir.entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Name); for (int i = 0; i < tests.size(); i++) { jsonInput.fileNames << tests.at(i).absoluteFilePath(); } } else { jsonInput.fileNames += inputQStr; } for (int i = 0; i < jsonInput.fileNames.size(); i++) { raw_buffer fileData; FilePtr fileHandle(map(jsonInput.fileNames.at(i), fileData)); if (fileHandle == NULL) { destroyFiles(fileHandles); printUsage(stderr, 3, argc, argv); } fileHandles += fileHandle; jsonInput.fileData += fileData; } if (jsonInput.fileData.size() == 0) { fprintf(stderr, "No json inputs found to validate against in %s\n", input.c_str()); printUsage(stderr, 4, argc, argv); } Q_ASSERT(jsonInput.fileData.size() == jsonInput.fileNames.size()); pjson::testc::TestSchema schemaTester(schemaData, jsonInput, pass); return QTest::qExec(&schemaTester, arguments); }