首页 > 其他分享 >定义ignore配置文件忽略文件统计

定义ignore配置文件忽略文件统计

时间:2022-12-01 17:05:17浏览次数:46  
标签:node 忽略 return 配置文件 ignore file include ptr size


/*#include <iostream>
#include <string>
#include <regex>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

int main() {
std::regex reg1(".*\\.xml");
bool found = regex_match("value.xml",reg1);
std::cout << found << std::endl;

boost::property_tree::ptree properties;
boost::property_tree::ini_parser::read_ini("./ignore.ini", properties);
boost::property_tree::basic_ptree<std::string, std::string>items = properties.get_child("");
for (auto item = begin(items);item != end(items);++item) {
std::cout << item->first.data() << " " << item->second.data() << std::endl;
}
return 0;
}*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <regex>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include "trie_tree.hpp"
static const int SIZE_UNIT_NUM = 5;
struct file_info {
file_info(const std::string &str) : file_name(str) {
total_size = 0;
detail_size.resize(SIZE_UNIT_NUM, 0);
}
inline void show_info() const {
std::cout << "file name = " << file_name << " size = " << total_size << "B" << " detail size:" << detail_size[4] << "T" << detail_size[3] << "G" << detail_size[2] << "M" << detail_size[1] << "K" << detail_size[0] << "B" << std::endl;
}
std::string file_name;
size_t total_size;
std::vector<size_t>detail_size; // Byte KB MB GB TB
};
class file_system {
public:
file_system() = default;
file_system(const file_system &) = delete;
file_system & operator = (const file_system &) = delete;
~file_system() = default;
public:
bool init(const char *path = "./ignore.ini") {
if (false == trieTree_.init()) {
std::cerr << "trie tree init failed." << std::endl;
return false;
}
parse_ini_file(path);
return true;
}
void stat_file_info(const char *dir_path) {
get_file_info(dir_path);
for (const auto &info : file_info_set_) {
total_size_ += info.total_size;
info.show_info();
}
file_info fileinfo(dir_path);
stat_file_size(fileinfo, total_size_);
fileinfo.show_info();
}
size_t get_total_dir_size() const {
return total_size_;
}
private:
void parse_ini_file(const char *path) {
boost::property_tree::ptree properties;
try {
boost::property_tree::ini_parser::read_ini(path, properties);
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return;
}
boost::property_tree::basic_ptree<std::string, std::string>items = properties.get_child("");
for (auto item = begin(items);item != end(items);++item) {
trieTree_.insert(item->second.data().c_str());
}
}
bool need_to_ignore(const char *name) {
int j = strlen(name) - 1;
for (;j >= 0;j--) {
if ('.' == name[j]) {
break;
}
}
if (j < 0) {
return false;
}
name += (j + 1);
return trieTree_.find(name);
}
void stat_file_size(file_info &fileinfo, size_t bytes) {
fileinfo.total_size = bytes;
for (auto i = 0;i < SIZE_UNIT_NUM;i++) {
auto x = bytes / 1024;
auto y = bytes % 1024;
fileinfo.detail_size[i] = y;
if (0 == x) {
break;
}
bytes = x;
}
}
void get_file_info(const char *dir_path) {
DIR *dirp = nullptr;
struct stat stat_buf;
dirp = ::opendir(dir_path);
if (nullptr == dirp) {
std::cerr << dir_path << " opendir failed." << std::endl;
::exit(-1);
}
::chdir(dir_path);
struct dirent *entry = nullptr;
while (nullptr != (entry = readdir(dirp))) {
::lstat(entry->d_name, &stat_buf);
if (true == need_to_ignore(entry->d_name)) {
continue;
}
if (!S_ISDIR(stat_buf.st_mode)) {
file_info fileinfo(entry->d_name);
stat_file_size(fileinfo, stat_buf.st_size);
file_info_set_.emplace_back(fileinfo);
continue;
}
if (0 == strcmp(entry->d_name, ".") || 0 == strcmp(entry->d_name, "..")) {
continue;
}
file_info fileinfo(entry->d_name);
stat_file_size(fileinfo, stat_buf.st_size);
file_info_set_.emplace_back(fileinfo);
get_file_info(entry->d_name);
}
::chdir("..");
::closedir(dirp);
}

private:
size_t total_size_ = 0;
std::vector<file_info>file_info_set_;
trie_tree trieTree_;
};

int main() {
file_system filesystem;
if (false == filesystem.init()) {
return -1;
}
filesystem.stat_file_info("/root/rel/log");


return 0;
}
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>
#include <string>
#include <thread>
#define SAFE_DEL(p) do { if (nullptr != p) { delete p; p = nullptr; }} while (0)
enum class TrieNodeType {
UNCOMPLETED,
COMPLETED
};
struct trie_node {
static const size_t MAX_NODE_NUM = 256;
TrieNodeType type = TrieNodeType::UNCOMPLETED;
trie_node *links[MAX_NODE_NUM] = { nullptr };
};
class trie_tree {
public:
trie_tree() = default;
trie_tree(const trie_tree &) = delete;
trie_tree & operator = (const trie_tree &) = delete;
virtual ~trie_tree() {
destory(root_);
}
public:
bool init() {
try {
root_ = new trie_node;
}
catch (...) {
return false;
}
init_succ = true;
return true;
}
bool insert(const char *word) {
char ch = 0;
trie_node *ptr = nullptr;
if (false == init_succ) {
return false;
}
auto cur_node_ptr = root_;
for (int i = 0;word[i];i++) {
ch = word[i];
if (false == charater_is_valid(ch)) {
return false;
}
auto &link = cur_node_ptr->links[ch];
if (nullptr == link) {
ptr = create_node();
link = ptr;
}
cur_node_ptr = link;
}
if (TrieNodeType::COMPLETED == cur_node_ptr->type) {
std::cerr << "word:" << word << " reapted." << std::endl;
return false;
}
cur_node_ptr->type = TrieNodeType::COMPLETED;
return true;
}
bool find(const char *word) {
char ch = 0;
trie_node *ptr = nullptr;
if (false == init_succ) {
return false;
}
auto cur_node_ptr = root_;
for (int i = 0;word[i];i++) {
ch = word[i];
if (false == charater_is_valid(ch)) {
return false;
}
auto &link = cur_node_ptr->links[ch];
if (nullptr == link) {
return false;
}
cur_node_ptr = link;
}
return TrieNodeType::COMPLETED == cur_node_ptr->type;
}
bool find(const char *prefix_word, std::vector<std::string>&words) {
char ch = 0;
trie_node *ptr = nullptr;
if (false == init_succ) {
return false;
}
auto cur_node_ptr = root_;
std::string str;
for (int i = 0;prefix_word[i];i++) {
ch = prefix_word[i];
if (false == charater_is_valid(ch)) {
return false;
}
auto &link = cur_node_ptr->links[ch];
if (nullptr == link) {
return false;
}
str += ch;
cur_node_ptr = link;
}
find_all_words(cur_node_ptr, str, words);
return true;
}
private:
inline trie_node *create_node() {
trie_node *ptr = nullptr;
try {
ptr = new trie_node;
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
::exit(-1);
}
return ptr;
}
bool charater_is_valid(char ch) {
return (ch >= 0 && ch < trie_node::MAX_NODE_NUM);
}
void find_all_words(const trie_node *ptr, std::string &str, std::vector<std::string>&words) {
if (nullptr == ptr) {
return;
}
char ch = 0;
for (size_t index = 0;index < trie_node::MAX_NODE_NUM;index++) {
auto &link = ptr->links[index];
if (nullptr == link) {
continue;
}
ch = index;
str += ch;
find_all_words(link, str, words);
}
if (TrieNodeType::COMPLETED == ptr->type) {
words.emplace_back(str);
}
str.erase(end(str) - 1);
}
void destory(trie_node *&ptr) {
if (nullptr == ptr) {
return;
}
for (size_t index = 0;index < trie_node::MAX_NODE_NUM;index++) {
destory(ptr->links[index]);
}
SAFE_DEL(ptr);
}
private:
trie_node *root_ = nullptr;
bool init_succ = false;
};
XML=xml
JSON=json
TXT=txt

 

标签:node,忽略,return,配置文件,ignore,file,include,ptr,size
From: https://blog.51cto.com/u_15899033/5902963

相关文章

  • spring boot配置文件之properties的编写与配置文件编码问题
    1、将yml文件中的配置   ctrl+/  注释掉#server:#port:8090##person:#age:18#boss:false#birth:2017/12/12#maps:{k1:v1,k2:12}......
  • [Bug0062] Hexo的 yml配置文件报错FATAL YAMLException: bad indentation of a mappin
    问题Hexo博客中配置文件yml报错FATALYAMLException:badindentationofamappingentryatxxxFATALYAMLException:badindentationofamappingentryatline26......
  • springboot配置之配置文件加载位置
    1、springboot启动会扫描以下位置的application.properties或者application.yml文件作为springboot的配置文件,按照优先级从高到低的顺序。所有位置的文件都会被加载,高优先级......
  • maven配置文件
    maven配置文件<build><finalName>${project.artifactId}</finalName><resources><resource><directory>src/main/java</directory><i......
  • spring配置文件beans中报错
     碰到了这个报错:Referencedfilecontainserrors(http://www.springframework.org/schema/tx/spring-tx.xsd).可以用下面步骤清除错误:右键xml文件,选择validate。参......
  • 用ClassLoader读取配置文件
    packageday2;importorg.junit.Test;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.InputStream;importjava.util.Propert......
  • Sharding-JDBC 原理和spring boot 核心配置文件说明
    1、分库分表策略:如每个数据记录属于一个集团ID、属于一个店铺ID列,可以根据集团ID%数据库数量取余设置规则分片键,及设置用哪个数据库和哪个表。同现可以使用数据记录中的......
  • SpringCloud之Config分布式配置文件中心
    分布式系统面临的配置问题:微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息......
  • Spring的ioc介绍,配置文件和基本使用
    1.什么是spring,它能够做什么?  Spring是一个开源框架,它由RodJohnson创建。它是为了解决企业应用开发的复杂性而创建的。  Spring使用基本的JavaBean来完成以前只可......
  • (4)SpringMVC核心配置文件详解
    在上一篇文章中,我们从DispatcherServlet谈起,最终为读者详细分析了SpringMVC的初始化主线的全部过程。整个初始化主线的研究,其实始终围绕着DispatcherServlet、WebApplicatio......