• apache 的apr函数库简单学习


    1. 基本框架和内存池的使用

    #define MEM_ALLOC_SZ 1024
    int main(int argc, const char * const argv[])
    {
    apr_pool_t * pool;//内存池
    apr_status_t rv;
    char * buf;

    rv = apr_initialize();//初始化
    if( rv != APR_SUCCESS ){
    return -1;
    }
    rv = apr_pool_create(&pool,NULL);//创建内存池
    if( rv != APR_SUCCESS ){
    return -1;
    }

    buf = apr_palloc(pool,MEM_ALLOC_SZ);//分配一个内存块
    //apr_pool_clear(pool);
    strcpy(buf,"test");
    printf("this is a %s\n",buf);

    apr_pool_destroy(pool);//销掉内存池对象
    apr_terminate();//结束

    return APR_SUCCESS;
    }

    2.文件操作

    #define MEM_ALLOC_SIZE 1024
    int main(int argc, const char * const argv[])
    {
    apr_pool_t * pool;//内存池
    apr_status_t rv;
    char * buf;
    char * contentBuf;
    apr_file_t * fin;
    apr_file_t * fout;
    apr_size_t len;
    apr_finfo_t finfo;//用来保存文件信息的结构体

    rv = apr_initialize();//初始化
    rv = apr_pool_create(&pool,NULL);//创建pool
    buf = apr_palloc(pool,MEM_ALLOC_SIZE);//给buf分配空间

    rv = apr_file_open(&fin,"d:\\in.txt",APR_READ|APR_BUFFERED,APR_OS_DEFAULT,pool);//打开可读文件
    if( rv == APR_SUCCESS ){
    strcpy(buf,"fileread_open succeed!");
    printf("this is a test:%s\n",buf);
    }
    apr_file_lock(fin,APR_FLOCK_SHARED);//读锁,允许其他读者进线程对它读,但不能有写者对它操作
    /*
    * 打开可写文件
    * 其中APR_CREATE表示若没有该文件则创建一个,APR_TRUNCATE表示若文件有内容则全部删掉(APR_APPEND为在文件末添加)
    */
    rv = apr_file_open(&fout,"d:\\out.txt",APR_WRITE|APR_BUFFERED|APR_CREATE|APR_TRUNCATE,APR_OS_DEFAULT,pool);
    if( rv == APR_SUCCESS ){
    strcpy(buf,"filewrite_open succeed!");
    printf("this is a test:%s\n",buf);
    }
    apr_file_lock(fout,APR_FLOCK_EXCLUSIVE);//写锁,不允许其他进程对此文件操作
    /*
    * 将in.txt文件中的内容全部复制到out.txt文件中
    */
    contentBuf = apr_palloc(pool,MEM_ALLOC_SIZE);
    do{
    apr_file_read(fin,contentBuf,&len);
    //contentBuf[len] = 0;
    apr_file_write(fout,contentBuf,&len);
    } while( len != 0 );

    //关闭文件in.txt
    apr_file_unlock(fin);
    apr_file_close(fin);

    /*
    * 将复制in.txt为in_bak.txt,同时删掉in.txt文件
    */
    apr_file_copy("d:\\in.txt","d:\\in_bak.txt",APR_OS_DEFAULT,pool);
    apr_file_remove("d:\\in.txt",pool);

    apr_file_info_get(&finfo,APR_FINFO_NAME,fout);//获取文件信息方法1:通过文件对象
    printf("file name is:%s",finfo.fname);

    apr_stat(&finfo,"d:\\in_bak.txt",APR_FINFO_SIZE,pool);//获取文件信息方法2:通过文件路径
    printf("file size is:%d bytes",finfo.size);

    //关闭文件out.txt
    apr_file_unlock(fout);
    apr_file_close(fout);

    apr_pool_destroy(pool);//销掉内存池对象
    apr_terminate();//结束
    return APR_SUCCESS;

    }

    3 目录操作

    #define MEM_ALLOC_SIZE 1024
    void traversal(char * dirPath, apr_pool_t * pool) {//-r遍历该目录下的所有文件

    apr_finfo_t finfo;//用来保存文件信息的结构体
    apr_dir_t *dir;
    if( apr_dir_open(&dir,dirPath,pool) != APR_SUCCESS ) return;//获得目录对象,若不成功则直接返回

    while(apr_dir_read(&finfo,APR_FINFO_NAME|APR_FINFO_TYPE,dir) == APR_SUCCESS ) {//读该目录下的所有文件信息,存到finfo中
    if( finfo.filetype == APR_DIR ) {//该文件是目录对象
    if( strcmp(finfo.name,".") == 0 || strcmp(finfo.name,"..") == 0 ) {//该文件是.或..,则继续
    continue;
    }else {//该文件是目录,且不是.或..,则递归进入
    char * subDirPath;
    subDirPath = apr_pstrcat(pool,dirPath,"\\",finfo.name,NULL);//subDirPath = dirPath + "\\" + finfo.name + NULL
    traversal(subDirPath,pool);
    }
    }else if( finfo.filetype == APR_REG ) {//文件是普通文件,输出
    printf("%s\\%s\n",dirPath,finfo.name);
    }else {
    printf("非正常文件:%s\\%s\n",dirPath,finfo.name);//文件是非普通文件,输出
    }
    }
    }
    int main(int argc, const char * const argv[])
    {
    apr_pool_t * pool;//内存池
    apr_status_t rv;

    rv = apr_initialize();//初始化
    rv = apr_pool_create(&pool,NULL);//创建pool

    traversal("d:",pool);//开始遍历

    apr_pool_destroy(pool);//销掉内存池对象
    apr_terminate();//结束
    return APR_SUCCESS;

    }

    4 自定义命令行参数


    static const apr_getopt_option_t cmd_option[] = {
    {"in",'i',TRUE,"input filename"},
    {"out",'o',TRUE,"output filename"},
    {"help",'h',FALSE,"show help information"},
    {NULL,0,0,NULL},//哨兵
    };
    #define MEM_ALLOC_SIZE 1024
    int main(int argc, const char * const argv[])
    {
    apr_pool_t * pool;//内存池
    apr_status_t rv;
    apr_getopt_t * opt;
    int optch;
    char * optarg;
    rv = apr_initialize();//初始化
    rv = apr_pool_create(&pool,NULL);//创建pool

    apr_getopt_init(&opt,pool,argc,argv);//将命令行内容填入opt结构中

    while( (rv = apr_getopt_long(opt,cmd_option,&optch,&optarg)) == APR_SUCCESS ) {//一个一个解析命令
    switch(optch) {
    case 'i':
    printf("i参数是%s",optarg);
    break;
    case 'o':
    printf("o参数是%s",optarg);
    break;
    case 'h':
    printf("h没有参数");
    break;
    default:
    break;
    }
    }

    if( rv != APR_EOF ){//判断是否全部解析
    printf("解析命令行错误!");
    }

    apr_pool_destroy(pool);//销掉内存池对象
    apr_terminate();//结束
    return APR_SUCCESS;

    }

  • 相关阅读:
    客户端用不用bind的区别
    软件项目开发流程
    很强大的几个网站
    MIS系统的 5个基本模块
    asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excelxls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
    正则快速入门
    asp.net 服务端调用客户端脚本; asp.net 服务端将文件传给客户端; reponse.ContentType的取值;用OutputStream.Write返回文件,效率是WriteFile的10倍;download link click和OutputStream的比较;
    常用编辑
    数据库流程图设计
    scroll位置控制 window的和div的
  • 原文地址:https://www.cnblogs.com/aggavara/p/2938559.html
Copyright © 2020-2023  润新知