当前位置 : 主页 > 编程语言 > c语言 >

实用工具类及测试

来源:互联网 收集:自由互联 发布时间:2023-08-25
实用工具类 // 实用工具类的实现// 避免头文件重复包含#ifndef __M_UTIL_H__#define __M_UTIL_H__#include iostream#include unistd.h#include ctime#include sys/types.h#include sys/stat.h#include unistd.hnamespace nmzlog{ // 在

实用工具类

// 实用工具类的实现
// 避免头文件重复包含
#ifndef __M_UTIL_H__
#define __M_UTIL_H__
#include <iostream>
#include <unistd.h>
#include <ctime>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
namespace nmzlog{
    // 在nmzlog命名空间中进行定义
    namespace util{
        class Date{
        public:
            // 定义获取当前系统时间函数
            // 定义成静态成员函数,不用实例化对象可以直接调用
            static size_t now()
            {
                // 类型转换
                return (size_t)time(nullptr);
            }
        };
        class File{
        public:
            // 都定义成静态成员函数,不用去实例化对象直接调用
            // 判断文件是否存在,参数是传入的路径名
            static bool exist(const std::string& pathname)
            {
                // access函数检测用户对于这个文件是否有操作权限,还可以判断文件是否存在,在头文件unistd.h
                // 文件存在返回0,不存在返回-1
                // return (access(pathname.c_str(),F_OK) == 0);
                // 为了更好的移植性,我们可以使用stat获取文件属性,成功了代表文件存在,不成功表示文件不存在
                // 在头文件<sys/types.h>\<sys/stat.h>\<unistd.h>中
                struct stat st;
                // 如果小于0,返回false
                if(stat(pathname.c_str(),&st) < 0)
                {
                    return false;
                }
                return true;
            }
            // 获取当前文件所在路径,参数是传入的路径名,返回的是文件路径
             static std::string path(const std::string& pathname)
             {
                // 找到最后一个\\/的位置
                size_t pos = pathname.find_last_of("\\/");
                if(pos == std::string::npos)
                {
                    return ".";
                }
                //返回要截取的长度,坐标是从0开始计算的,所以如果想截取/就要+1
                return pathname.substr(0,pos+1);
             }
            // 创建目录
            static void createDirectory(const std::string& pathname)
            {
                // 创建目录时需要一层一层的创建
                // pos用来查找\\/的位置,idx用来标定查找的起始位置
                size_t pos = 0,idx = 0;
                while(idx < pathname.size())
                {
                    // 从开始位置去找
                    pos = pathname.find_first_of("/\\",idx);
                    if(pos == std::string::npos)
                    {
                        // 此时路径中没有\\/,如a.txt,此时就可以直接创建目录
                        // 创建目录时需要使用到mkdir系统接口,在头文件<sys/stat.h>\<sys/types.h>中
                        // mkdir(const char* pathname,mode_t mode);第二个参数是创建出来的文件目录的权限
                        mkdir(pathname.c_str(),0777);
                        break;
                    }
                    // 如果找到了\,那么就要先创建父级目录,pos是\的位置,+1是将\也截取出来了
                    std::string parent_dir = pathname.substr(0,pos+1);
                    // 判断父级目录是否存在,这里的exist也会判断.和..的情况
                    if(exist(parent_dir) == true)
                    {
                        // 如果存在更新idx的位置,跳出本次循环
                        idx = pos + 1;
                        continue;
                    }
                    // 如果父级目录不存在就创建
                    mkdir(parent_dir.c_str(),0777);
                    idx = pos + 1;//创建之后更新idx的位置
                }
            }
        };
    }
}
#endif

实用工具类测试

#include "util.hpp"
// 实用工具类的测试
int main()
{
    // 获取当前系统时间,获取的是时间戳
    std::cout << nmzlog::util::Date::now() << std::endl;
    // 先判断文件是否存在,然后创建目录,再获取路径名
    std::string pathname = "./abc/def/txt.log";
    // 如果文件不存在,创建目录
   if(nmzlog::util::File::exist(pathname) == false)
   {
        nmzlog::util::File::createDirectory(pathname);
        std::cout << "创建成功\n";
   }
    // 获取文件路径名中的路径 
    nmzlog::util::File::path(pathname);
}
上一篇:AcWing 867. 分解质因数
下一篇:没有了
网友评论