/*---------------------------------------------------------------------- | NPT_DirectoryCreate +---------------------------------------------------------------------*/ NPT_Result NPT_DirectoryCreate(const char* path, bool create_parents) { NPT_Result res = NPT_SUCCESS; NPT_String fullpath = path; // replace delimiters with the proper one for the platform fullpath.Replace((NPT_DIR_DELIMITER_CHR == '/')?'\\':'/', NPT_DIR_DELIMITER_CHR); // remove excessive delimiters fullpath.TrimRight(NPT_DIR_DELIMITER_CHR); if (create_parents) { NPT_String parent_path; // look for a delimiter from the beginning int delimiter = fullpath.Find(NPT_DIR_DELIMITER_CHR, 1); while (delimiter > 0 && NPT_SUCCEEDED(res)) { // copy the path up to the delimiter parent_path = fullpath.SubString(0, delimiter); // create the directory non recursively res = NPT_DirectoryCreate(parent_path, false); // look for the next delimiter delimiter = fullpath.Find(NPT_DIR_DELIMITER_CHR, delimiter + 1); } if (NPT_FAILED(res)) return res; } // create directory return NPT_Directory::Create(fullpath); }
/*---------------------------------------------------------------------- | TestRemoveEmptyDirectory +---------------------------------------------------------------------*/ void TestRemoveEmptyDirectory() { NPT_DirectoryEntryInfo info; NPT_String path; NPT_Result res; // read path from environment variable res = NPT_GetEnvironment("NEPTUNE_TEST_ROOT", path); CHECK(NPT_SUCCEEDED(res)); // find the next directory path that does not exist res = FindNextAvailableDirectory(path, path); CHECK(NPT_SUCCEEDED(res)); // create directory object res = NPT_DirectoryCreate(path, true); CHECK(NPT_SUCCEEDED(res)); // remove empty directory res = NPT_DirectoryRemove(path, false); CHECK(NPT_SUCCEEDED(res)); // verify directory does not exist any more res = NPT_DirectoryEntry::GetInfo(path, &info); CHECK(res == NPT_ERROR_NO_SUCH_ITEM); }
/*---------------------------------------------------------------------- | TestRemoveDirectory +---------------------------------------------------------------------*/ void TestRemoveDirectory() { NPT_DirectoryEntryInfo info; NPT_String path; NPT_Result res; // read path from environment variable res = NPT_GetEnvironment("NEPTUNE_TEST_ROOT", path); CHECK(NPT_SUCCEEDED(res)); // find the next directory path that does not exist res = FindNextAvailableDirectory(path, path); CHECK(NPT_SUCCEEDED(res)); // create directory object res = NPT_DirectoryCreate(path, true); CHECK(NPT_SUCCEEDED(res)); // create some subdirectories res = CreateEntry(path, "foof1", true); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foof2", true); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foof2.dir", true); CHECK(NPT_SUCCEEDED(res)); // create some files res = CreateEntry(path, "foo1", false); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foo2.", false); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foo2.e", false); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foo2.e3", false); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foo2.gas", false); CHECK(NPT_SUCCEEDED(res)); res = CreateEntry(path, "foo4.tee", false); CHECK(NPT_SUCCEEDED(res)); // remove directory res = NPT_DirectoryRemove(path, true); CHECK(NPT_SUCCEEDED(res)); // verify directory does not exist any more res = NPT_DirectoryEntry::GetInfo(path, &info); CHECK(res == NPT_ERROR_NO_SUCH_ITEM); }
/*---------------------------------------------------------------------- | CreateEntry +---------------------------------------------------------------------*/ static NPT_Result CreateEntry(const char* root_path, const char* name, bool directory) { NPT_Result res; NPT_String path = root_path; NPT_DirectoryAppendToPath(path, name); if (directory) { res = NPT_DirectoryCreate(path, true); } else { NPT_File* file = new NPT_File(path); res = file->Open(NPT_FILE_OPEN_MODE_TRUNCATE | NPT_FILE_OPEN_MODE_WRITE | NPT_FILE_OPEN_MODE_CREATE); delete file; } return res; }