get_files_name()

master
IoTgod 5 years ago
parent da3a3402e5
commit 126a6750f8
  1. 38
      docs/ovo_file/README.md
  2. 127
      src/ovo.cpp
  3. 17
      src/ovo.h
  4. BIN
      test/a.exe
  5. 127
      test/ovo.cpp
  6. 17
      test/ovo.h
  7. BIN
      test/ovo.h.gch
  8. 9
      test/test.cpp

@ -0,0 +1,38 @@
## Class ovo::file
### file.get_files_name()
This function deals with the name of files. It has 5 params.
- **string path** The path to search (Necessary)
- **vector<string>& files** Vector to store the file names.(Necessary)
- **string format** The file format to search e.g. `*.png` (Default: '*')
- **int isSearchSubfolders** Is the subfolders be searched. (Default: 0)
- **int isShowPath** Is a path before the name. (Default: 0)
The most simple demo for this:
````C++
ovo::file f;
vector<string> fls;
f.get_files_name(".\\",fls);//In Windows
for(int i = 0; i < fls.size(); i++)
std::cout << fls[i] << endl;
````
This will show all the files in current position. Such as:
>README.md
>LICENSE
You can also do like this:
````C++
ovo::file f;
vector<string> fls;
f.get_files_name(".\\",fls,"*.md",1,1);//In Windows
for(int i = 0; i < fls.size(); i++)
std::cout << fls[i] << endl;
````
This will show all the .md files with a path in current and all subfolders.
>.\README.md
>.\docs\ovo_file\README.md
>.\docs\ovo_info\README.md
>.\docs\ovo_string\README.

@ -9,13 +9,15 @@
* @version 0.0.1
*/
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cstring>
@ -31,6 +33,7 @@
*/
string ovo::info::version = "Version 0.0.1";
/**
* Display a welcome string
*
@ -47,3 +50,125 @@ void ovo::info::detail()
cout << "copyright Copyright (c) 2019 EEENeko" << endl << endl;
}
/****** Class file ******/
/**
* Get All Files with certain format
*
* @Author yimian
* @access private
* @param string path filePath
* @param vector<string>& files
* @param string format #such as "*.jpg"
* @param int isShowPath #If this is 1, the path would display before every filename
* @return void
*/
void ovo::file::get_all_files_name(string path, vector<string>& files, const string format, const int isShowPath)
{
long hFile = 0;
//File info struct
struct _finddata_t fileInfo;
string p;
#ifdef linux
const string sign = "/";
#else
const string sign = "\\";
#endif
//If the path end with '/'
if(path[path.size()-1] == '/' || path[path.size()-1] == '\\') path.erase(path.end() - 1);
//Deal with files
if((hFile = _findfirst(p.assign(path).append(sign+format).c_str(),&fileInfo)) != -1){
do
{
if(!(fileInfo.attrib & _A_SUBDIR)){
if(isShowPath)
files.push_back(p.assign(path).append(sign).append(fileInfo.name));
else
files.push_back(p.assign(fileInfo.name));
}
}while(_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
}
}
/**
* Get All Folders and subFolders
*
* @Author yimian
* @access private
* @param string path filePath
* @param vector<string>& folders
* @return void
*/
void ovo::file::get_all_folders_name(string path, vector<string>& folders)
{
long hFile = 0;
//File info struct
struct _finddata_t fileInfo;
string p;
#ifdef linux
const string sign = "/";
#else
const string sign = "\\";
#endif
//If the path end with '/'
if(path[path.size()-1] == '/' || path[path.size()-1] == '\\') path.erase(path.end() - 1);
//Deal with folders
if((hFile = _findfirst(p.assign(path).append(sign+"*").c_str(),&fileInfo)) != -1){
do
{
if((fileInfo.attrib & _A_SUBDIR)){
if(strcmp(fileInfo.name,".") != 0 && strcmp(fileInfo.name,"..") != 0){
folders.push_back(p.assign(path).append(sign).append(fileInfo.name));
get_all_folders_name(p.assign(path).append(sign).append(fileInfo.name), folders);
}
}
}while(_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
}
}
/**
* Get files with certain format and settings
*
* @Author yimian
* @access public
* @param string path filePath
* @param vector<string> files
* @param string format #such as "*.jpg"
* @param int isSearchSubfolders ##If this is 1, will search all the sub folders
* @param int isShowPath #If this is 1, the path would display before every filename
* @return void
*/
void ovo::file::get_files_name(const string path, vector<string>& files, const string format, const int isSearchSubfolders, const int isShowPath)
{
#ifdef linux
const string sign = "/";
#else
const string sign = "\\";
#endif
get_all_files_name(path, files, format, isShowPath);
//Search Sub Folders
if(isSearchSubfolders){
vector<string> foldersPath;
get_all_folders_name(path, foldersPath);
for(int i = 0; i < foldersPath.size(); i++){
get_all_files_name(foldersPath[i], files, format, isShowPath);
}
}
}

@ -35,6 +35,23 @@ namespace ovo{
inline void hi(){cout << "Hello OvO~" << endl;};
void detail();
};
/**
* Operate with files
*
* @author yimian
* @category ovo
* @package ovo
*/
class file{
public:
void get_files_name(const string path, vector<string>& files, const string format = "*", const int isSearchSubfolders = 0, const int isShowPath = 0);
private:
void get_all_files_name(string path, vector<string>& files, const string format = "*", const int isShowPath = 0);
void get_all_folders_name(string path, vector<string>& folders);
};
}

Binary file not shown.

@ -9,13 +9,15 @@
* @version 0.0.1
*/
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cstring>
@ -31,6 +33,7 @@
*/
string ovo::info::version = "Version 0.0.1";
/**
* Display a welcome string
*
@ -47,3 +50,125 @@ void ovo::info::detail()
cout << "copyright Copyright (c) 2019 EEENeko" << endl << endl;
}
/****** Class file ******/
/**
* Get All Files with certain format
*
* @Author yimian
* @access private
* @param string path filePath
* @param vector<string>& files
* @param string format #such as "*.jpg"
* @param int isShowPath #If this is 1, the path would display before every filename
* @return void
*/
void ovo::file::get_all_files_name(string path, vector<string>& files, const string format, const int isShowPath)
{
long hFile = 0;
//File info struct
struct _finddata_t fileInfo;
string p;
#ifdef linux
const string sign = "/";
#else
const string sign = "\\";
#endif
//If the path end with '/'
if(path[path.size()-1] == '/' || path[path.size()-1] == '\\') path.erase(path.end() - 1);
//Deal with files
if((hFile = _findfirst(p.assign(path).append(sign+format).c_str(),&fileInfo)) != -1){
do
{
if(!(fileInfo.attrib & _A_SUBDIR)){
if(isShowPath)
files.push_back(p.assign(path).append(sign).append(fileInfo.name));
else
files.push_back(p.assign(fileInfo.name));
}
}while(_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
}
}
/**
* Get All Folders and subFolders
*
* @Author yimian
* @access private
* @param string path filePath
* @param vector<string>& folders
* @return void
*/
void ovo::file::get_all_folders_name(string path, vector<string>& folders)
{
long hFile = 0;
//File info struct
struct _finddata_t fileInfo;
string p;
#ifdef linux
const string sign = "/";
#else
const string sign = "\\";
#endif
//If the path end with '/'
if(path[path.size()-1] == '/' || path[path.size()-1] == '\\') path.erase(path.end() - 1);
//Deal with folders
if((hFile = _findfirst(p.assign(path).append(sign+"*").c_str(),&fileInfo)) != -1){
do
{
if((fileInfo.attrib & _A_SUBDIR)){
if(strcmp(fileInfo.name,".") != 0 && strcmp(fileInfo.name,"..") != 0){
folders.push_back(p.assign(path).append(sign).append(fileInfo.name));
get_all_folders_name(p.assign(path).append(sign).append(fileInfo.name), folders);
}
}
}while(_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
}
}
/**
* Get files with certain format and settings
*
* @Author yimian
* @access public
* @param string path filePath
* @param vector<string> files
* @param string format #such as "*.jpg"
* @param int isSearchSubfolders ##If this is 1, will search all the sub folders
* @param int isShowPath #If this is 1, the path would display before every filename
* @return void
*/
void ovo::file::get_files_name(const string path, vector<string>& files, const string format, const int isSearchSubfolders, const int isShowPath)
{
#ifdef linux
const string sign = "/";
#else
const string sign = "\\";
#endif
get_all_files_name(path, files, format, isShowPath);
//Search Sub Folders
if(isSearchSubfolders){
vector<string> foldersPath;
get_all_folders_name(path, foldersPath);
for(int i = 0; i < foldersPath.size(); i++){
get_all_files_name(foldersPath[i], files, format, isShowPath);
}
}
}

@ -35,6 +35,23 @@ namespace ovo{
inline void hi(){cout << "Hello OvO~" << endl;};
void detail();
};
/**
* Operate with files
*
* @author yimian
* @category ovo
* @package ovo
*/
class file{
public:
void get_files_name(const string path, vector<string>& files, const string format = "*", const int isSearchSubfolders = 0, const int isShowPath = 0);
private:
void get_all_files_name(string path, vector<string>& files, const string format = "*", const int isShowPath = 0);
void get_all_folders_name(string path, vector<string>& folders);
};
}

Binary file not shown.

@ -1,4 +1,6 @@
#include <iostream>
#include <vector>
#include "ovo.h"
int main(int argc, char const *argv[])
@ -8,5 +10,12 @@ int main(int argc, char const *argv[])
i.hi();
std::cout << i.version << std::endl;
i.detail();
ovo::file f;
vector<string> fls;
f.get_files_name(".\\",fls,"*.md",1,1);
for(int i = 0; i < fls.size(); i++)
std::cout << fls[i] << endl;
return 0;
}

Loading…
Cancel
Save