static void cycle_font(int direction) { tinydir_dir dir; int ret = tinydir_open_sorted(&dir, FONTS_PATH); if (ret != 0) { // Error opening fonts dir. return; } int current_idx = -1; tinydir_file file; int i; // Current font file is set, look for its index in the dir. if (current_font_file[0]) { for (i = 0; i < dir.n_files; i++) { tinydir_readfile_n(&dir, &file, i); if (strcmp(current_font_file, file.name) == 0) { current_idx = i; break; } } } // Traversal direction and the loop bounds. direction = direction < 0 ? -1 : 1; int begin = direction < 0 ? dir.n_files - 1 : 0; int end = direction < 0 ? -1 : dir.n_files; // Load the next font sheet file you hit. boolean get_next = current_idx == -1 ? true : false; for (i = begin; i != end; i += direction) { tinydir_readfile_n(&dir, &file, i); if (get_next && is_font_sheet(&file)) { // Try to load the next thing that looks like a bitmap. // Keep going if the image loading fails. ret = sodna_load_font_sheet(file.path); if (ret == SODNA_OK) { snprintf(current_font_file, sizeof(current_font_file), "%s", file.name); refreshScreen(); return; } } // Load the file after the current file. if (i == current_idx) { get_next = true; } } // Traversed the whole directory and didn't find a next sheet, revert to built-in font. snprintf(current_font_file, sizeof(current_font_file), ""); sodna_resize(8, 14, COLS, ROWS); refreshScreen(); }
void OGUIFileBrowser::readFolder(string path) { realpath(path.c_str(), resolved_path); _file_list.clear(); tinydir_dir dir; if (tinydir_open_sorted(&dir, resolved_path) == -1) { cout << "Error opening file" << endl; return; } for (int i = 0; i < dir.n_files; i++) { tinydir_file file; if (tinydir_readfile_n(&dir, &file, i) == -1) { cout << "Error getting file" << endl; return; } if(strcmp(file.name,".")==0 || (file.name[0]=='.' && file.name[1]!='.')) continue; OGUIFile *oguifile= new OGUIFile(_window, file.name, file.path, file.is_dir, file.extension, 0); _file_list.push_back(oguifile); printf("\n"); } tinydir_close(&dir); }
/** * Get the list of midi files directly in the given directory * return a vector of file names * note: we don't check if they are valid MIDI files, only if the * MIDI extension is here */ static std::vector<std::string> get_midi_files(const std::string& path) { const std::string MIDI_EXTENSION(".mid"); std::vector<std::string> midiFileNames; tinydir_dir midiDir; tinydir_open_sorted(&midiDir, path.c_str()); for (unsigned i = 0; i < midiDir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&midiDir, &file, i); if (file.is_dir) { continue; } std::string tmpFileName(file.name); std::cout << tmpFileName << std::endl; if (!ends_with(tmpFileName, MIDI_EXTENSION)) { continue ; } std::cout << tmpFileName << std::endl; midiFileNames.push_back(tmpFileName); } tinydir_close(&midiDir); return midiFileNames; }
int main(void) { tinydir_dir dir; if (tinydir_open_sorted(&dir, ".") == -1) { perror("Error opening file"); goto bail; } for (;;) { int i; char input[256]; for (i = 0; i < dir.n_files; i++) { tinydir_file file; if (tinydir_readfile_n(&dir, &file, i) == -1) { perror("Error getting file"); goto bail; } if (file.is_dir) { printf("[%d] ", i); } printf("%s", file.name); if (file.is_dir) { printf("/"); } printf("\n"); } printf("?"); if (fgets(input, 256, stdin) == NULL) { break; } else { int choice = atoi(input); if (choice >= 0 && choice < dir.n_files) { if (tinydir_open_subdir_n(&dir, choice) == -1) { perror("Error opening subdirectory"); goto bail; } } } } bail: tinydir_close(&dir); return 0; }
void ImageFunction::getFiles() { #ifdef LIBMESH_HAVE_VTK // Storage for the file names _files = vtkSmartPointer<vtkStringArray>::New(); // Use specified file name if (isParamValid("file")) { std::string filename = getParam<FileName>("file"); _files->InsertNextValue(filename); _file_type = filename.substr(filename.find_last_of(".") + 1); } // File stack else { // Separate the file base from the path std::pair<std::string, std::string> split_file = MooseUtils::splitFileName(_file_base); // Create directory object tinydir_dir dir; tinydir_open_sorted(&dir, split_file.first.c_str()); // Regex for extracting numbers from file std::ostringstream oss; oss << "(" << split_file.second << ".*?(\\d+))\\..*"; pcrecpp::RE re_base_and_file_num(oss.str()); // Will pull out the full base and the file number simultaneously // Loop through the files in the directory for (unsigned int i = 0; i < dir.n_files; i++) { // Upate the current file tinydir_file file; tinydir_readfile_n(&dir, &file, i); // Store the file if it has proper extension as in numeric range if (!file.is_dir && MooseUtils::hasExtension(file.name, _file_type)) { std::string the_base; int file_num = 0; re_base_and_file_num.FullMatch(file.name, &the_base, &file_num); if (!the_base.empty() && file_num >= _file_range[0] && file_num <= _file_range[1]) _files->InsertNextValue(split_file.first + "/" + file.name); } } tinydir_close(&dir); } // Error if no files where located if (_files->GetNumberOfValues() == 0) mooseError("No image file(s) located"); #endif }
std::vector<std::string> findFilesInDir(const std::string &directory, const std::vector<std::string> &extensions, bool stripExtension, bool recursive) { std::vector<std::string> files; std::stack<std::string> dirsLeft; dirsLeft.push(directory); tinydir_dir dir; while (!dirsLeft.empty()) { std::string dirPath = dirsLeft.top(); dirsLeft.pop(); // Try to open directory if (tinydir_open_sorted(&dir, dirPath.c_str()) != 0) continue; for (unsigned i = 0; i < dir.n_files; i++) { tinydir_file file; if (tinydir_readfile_n(&dir, &file, i) != 0) { continue; } if (file.is_dir) { if (recursive && file.name != std::string(".") && file.name != std::string("..")) { dirsLeft.push(file.path); } continue; } std::vector<std::string>::const_iterator eiter = std::find(extensions.begin(), extensions.end(), file.extension); if (eiter == extensions.end()) { continue; } std::string path(file.path); if (stripExtension) { size_t lastindex = path.find_last_of("."); std::string raw = path.substr(0, lastindex); files.push_back(raw); } else { files.push_back(path); } } tinydir_close(&dir); } return files; }
void Renderer::setupEmptyDirectory(const filesystem::path &path) { FileUtils::createDir(path.str()); tinydir_dir dir; tinydir_open_sorted(&dir, path.str().c_str()); for (size_t i = 0; i < dir.n_files; ++i) { tinydir_file file; tinydir_readfile_n(&dir, &file, i); if (file.is_reg) { FileUtils::deleteFile((path / file.name).str()); } } tinydir_close(&dir); }
/** * @file main.c * @author Milán Unicsovics, u.milan at gmail dot com * @date August, 2013 * @brief Creates JSON array from *.mp3 files in current directory */ int main(void) { tinydir_dir dir; int i; int count = 0; if (tinydir_open_sorted(&dir, ".") == -1) { perror("Error opening file"); goto bail; } for (i = 0; i < dir.n_files; i++) { tinydir_file file; if (tinydir_readfile_n(&dir, &file, i) == -1) { perror("Error getting file"); goto bail; } size_t size = strlen(file.name); if (size >= 4 && file.name[size-4] == '.' && file.name[size-3] == 'm' && file.name[size-2] == 'p' && file.name[size-1] == '3') { count++; if (count == 1) { printf("["); } if (count > 1) { printf(","); } printf(" \"%s\"", file.name); } } if (count > 0) { printf("]"); } bail: tinydir_close(&dir); return 0; }
static void LoadCampaignsFromFolder( campaign_list_t *list, const char *name, const char *path, const GameMode mode) { tinydir_dir dir; int i; CSTRDUP(list->Name, name); if (tinydir_open_sorted(&dir, path) == -1) { printf("Cannot load campaigns from path %s\n", path); return; } for (i = 0; i < (int)dir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&dir, &file, i); // Ignore campaigns that start with a ~ // These are autosaved const bool isArchive = strcmp(file.extension, "cdogscpn") == 0 || strcmp(file.extension, "CDOGSCPN") == 0; if (file.is_dir && !isArchive && strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0) { campaign_list_t subFolder; CampaignListInit(&subFolder); LoadCampaignsFromFolder(&subFolder, file.name, file.path, mode); CArrayPushBack(&list->subFolders, &subFolder); } else if ((file.is_reg || isArchive) && file.name[0] != '~') { CampaignEntry entry; if (CampaignEntryTryLoad(&entry, file.path, mode)) { CArrayPushBack(&list->list, &entry); } } } tinydir_close(&dir); }
std::list<std::string> dataHandler::filesIn(const char* folderName) { std::list<std::string> filesList; tinydir_dir dir; unsigned int i; tinydir_open_sorted(&dir, folderName); for (i = 0; i < dir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&dir, &file, i); if (!file.is_dir) { filesList.push_back(file.name); } } tinydir_close(&dir); return filesList; }
void dataHandler::tinydirExample() { tinydir_dir dir; unsigned int i; tinydir_open_sorted(&dir, "../data/test/bedroom"); for (i = 0; i < dir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&dir, &file, i); printf("%s", file.name); if (file.is_dir) { printf("/"); } printf("\n"); } tinydir_close(&dir); }
std::list<std::string> dataHandler::foldersIN(const char *folderName) { std::list<std::string> foldersList; tinydir_dir dir; unsigned int i; tinydir_open_sorted(&dir, folderName); // starts from 2 to get rid of ./ and ../ for (i = 2; i < dir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&dir, &file, i); if (file.is_dir) { foldersList.push_back(file.name); } } tinydir_close(&dir); return foldersList; }
void BuildFromPath(FilePackerBuilder& builder, bool compress, const std::string& base, const std::string& path) { tinydir_dir dir; std::string full_path = base + "/" + path; if (tinydir_open_sorted(&dir, full_path.c_str()) == -1) { FILEPACKER_LOGE("Error opening directory"); goto bail; } for (uint32_t i = 0; i < dir.n_files; i++) { tinydir_file file; if (tinydir_readfile_n(&dir, &file, i) == -1) { FILEPACKER_LOGE("Error getting file"); goto bail; } if (file.name[0] != '.') { if (file.is_dir) { std::string newpath = path + file.name + "/"; BuildFromPath(builder, compress, base, newpath); } else if (file.is_reg) { BuildAddFile(builder, compress, base, path, file.name); } } else { FILEPACKER_LOGV(" - IGNORED: %s%s\n", path.c_str(), file.name); } } bail: tinydir_close(&dir); }
void LoadCampaignsFromFolder( campaign_list_t *list, const char *name, const char *path, campaign_mode_e mode) { tinydir_dir dir; int i; strcpy(list->name, name); if (tinydir_open_sorted(&dir, path) == -1) { printf("Cannot load campaigns from path %s\n", path); return; } for (i = 0; i < dir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&dir, &file, i); if (file.is_dir && strcmp(file.name, ".") != 0 && strcmp(file.name, "..") != 0) { campaign_list_t *subFolder; list->numSubFolders++; CREALLOC(list->subFolders, sizeof(campaign_list_t)*list->numSubFolders); subFolder = &list->subFolders[list->numSubFolders-1]; CampaignListInit(subFolder); LoadCampaignsFromFolder(subFolder, file.name, file.path, mode); } else if (file.is_reg) { char title[256]; if (IsCampaignOK(file.path, title)) { AddCustomCampaignEntry(list, file.name, file.path, title, mode); } } } tinydir_close(&dir); }
FileRangeBuilder::FileRangeBuilder(const InputParameters & params) : _status(0) { bool has_file = params.isParamValid("file"), has_file_base = params.isParamValid("file_base"), has_file_range = params.isParamValid("file_range"), has_file_suffix = params.isParamValid("file_suffix"); // Variables to be (possibly) used below... std::string file; std::string file_base; std::vector<unsigned int> file_range; if (has_file) { file = params.get<FileName>("file"); // Set the file_suffix parameter in the passed-in params object based on the input filename _file_suffix = file.substr(file.find_last_of(".") + 1); } if (has_file_base) file_base = params.get<FileNameNoExtension>("file_base"); if (has_file_range) file_range = params.get<std::vector<unsigned int>>("file_range"); if (has_file_suffix) _file_suffix = params.get<std::string>("file_suffix"); // Check that the combination of params provided is valid // 1.) Provided both a filename and a file base if (has_file && has_file_base) { _status = 1; return; } // 2.) Provided neither a filename nor a file base if (!has_file && !has_file_base) { _status = 2; return; } // 3.) Provided a file base but not a suffix if (has_file_base && !has_file_suffix) { _status = 3; return; } // 4.) Provided a filename and a range, warn that the range will be ignored. if (has_file && has_file_range) mooseWarning("Warning: file_range was ignored since a filename was provided."); // 5.) Provided a file_base but not a file_range, we'll create one if (has_file_base && !has_file_range) { file_range.push_back(0); file_range.push_back(std::numeric_limits<unsigned int>::max()); } // 6.) Provided a file_range with a single entry, so repeat it for // them. This signifies a single image (i.e. 2D Mesh) is to be // used. if (has_file_range && file_range.size() == 1) file_range.push_back(file_range[0]); // 7.) Provided a file_range with too many entries, print a // warning and truncate the extra values. if (has_file_range && file_range.size() != 2) { mooseWarning("A maximum of two values are allowed in the file_range, extra values truncated."); file_range.resize(2); } // Make sure that the range is in ascending order std::sort(file_range.begin(), file_range.end()); // Build up the filenames parameter, and inject it into the InputParameters object if (has_file) _filenames.push_back(file); else if (has_file_base) { // Separate the file base from the path std::pair<std::string, std::string> split_file = MooseUtils::splitFileName(file_base); // Create directory object tinydir_dir dir; tinydir_open_sorted(&dir, split_file.first.c_str()); // This regex will capture the file_base and any number of digits when used with FullMatch() std::ostringstream oss; oss << "(" << split_file.second << ".*?(\\d+))\\..*"; pcrecpp::RE file_base_and_num_regex(oss.str()); // Loop through the files in the directory for (int i = 0; i < dir.n_files; i++) { // Update the current file tinydir_file file; tinydir_readfile_n(&dir, &file, i); // Store the file if it has proper extension as in numeric range if (!file.is_dir && MooseUtils::hasExtension(file.name, _file_suffix)) { std::string the_base; unsigned int file_num = 0; file_base_and_num_regex.FullMatch(file.name, &the_base, &file_num); if (!the_base.empty() && file_num >= file_range[0] && file_num <= file_range[1]) _filenames.push_back(split_file.first + "/" + file.name); } } tinydir_close(&dir); } else mooseError("We'll never get here!"); // If we made it here, there were no errors }
void Config::updateApplicationList(std::string applicationDirectory) { DEBUG("Updating config with new applications from directory " << applicationDirectory); tinydir_dir appDir; tinydir_open_sorted(&appDir, applicationDirectory.c_str()); auto alreadyExistingApps = * Config::readApplications(); std::unordered_map<std::string, util::Command> associations; for (auto& app : alreadyExistingApps) associations[app.first.name] = app.first; int newApps = 0; for (int i = 0; i < appDir.n_files; i++) { tinydir_file file; tinydir_readfile_n(&appDir, &file, i); if (file.is_dir) continue; std::istringstream contents(readFile( applicationDirectory + "/" + file.name)); std::string line; const std::string nameStr = std::string("Name="); std::string name; const std::string execStr = std::string("Exec="); std::string exec; const std::string iconStr = std::string("Icon="); std::string icon; const std::string typeStr = std::string("Type="); std::string type; auto searchForString = [](const std::string & l, const std::string & search, std::string & output) { if (output.length() == 0 && strncmp(l.c_str(), search.c_str(), search.length()) == 0 && l.length() != search.length()) { output = l.substr(search.length(), l.length()); } }; while (std::getline(contents, line)) { searchForString(line, nameStr, name); searchForString(line, execStr, exec); searchForString(line, iconStr, icon); searchForString(line, typeStr, type); } if (name.length() == 0 || icon.length() == 0 || exec.length() == 0 || type != "Application") continue; std::regex escape_wildcard("(%\\S*)"); if (associations.count(name) > 0) continue; exec = std::regex_replace(exec, escape_wildcard, ""); int numApps = (*appRoot)["applications"].size(); (*appRoot)["applications"][numApps]["name"] = name; (*appRoot)["applications"][numApps]["command"] = exec; (*appRoot)["applications"][numApps]["icon"] = icon; newApps++; } tinydir_close(&appDir); DEBUG("Found " << newApps << " new applications."); Json::StyledWriter writer; Config::writeFile(Config::APP_FILE, writer.write(*appRoot)); }