APR函数库

apr的全称是Apache Portable Runtime Project, 其官方网站是http://apr.apache.org/,apr的任务是建一个平台下各种方面的良好编程接口。
其子项目有三个,一个是APR, 一个是APR-util , 最后一个是APR-iconv网站上有专门的文档.
打开apr与apr-util的doc,发现是同一个,不对,下载它们的程序发现,apr是关于专用技术的库,而apr-util是关于通用数据
处理的包,如xml, string,是apr-util的,而memory, poll, network等是apr的。


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

[cpp]  view plain  copy
  1. #define MEM_ALLOC_SZ 1024  
  2. int main(int argc, const char * const argv[])  
  3. {  
  4. apr_pool_t * pool;//内存池  
  5. apr_status_t rv;  
  6. char * buf;  
  7. rv = apr_initialize();//初始化  
  8. if( rv != APR_SUCCESS ){  
  9. return -1;  
  10. }  
  11. rv = apr_pool_create(&pool,NULL);//创建内存池  
  12. if( rv != APR_SUCCESS ){  
  13. return -1;  
  14. }  
  15.   
  16. buf = apr_palloc(pool,MEM_ALLOC_SZ);//分配一个内存块  
  17. //apr_pool_clear(pool);  
  18. strcpy(buf,"test");  
  19. printf("this is a %s\n",buf);  
  20. apr_pool_destroy(pool);//销掉内存池对象  
  21. apr_terminate();//结束  
  22. return APR_SUCCESS;  
  23. }  


2.文件操作

[cpp]  view plain  copy
  1. #define MEM_ALLOC_SIZE 1024  
  2. int main(int argc, const char * const argv[])  
  3. {  
  4. apr_pool_t * pool;//内存池  
  5. apr_status_t rv;  
  6. char * buf;  
  7. char * contentBuf;  
  8. apr_file_t * fin;  
  9. apr_file_t * fout;  
  10. apr_size_t len;  
  11. apr_finfo_t finfo;//用来保存文件信息的结构体  
  12.   
  13. rv = apr_initialize();//初始化  
  14. rv = apr_pool_create(&pool,NULL);//创建pool  
  15. buf = apr_palloc(pool,MEM_ALLOC_SIZE);//给buf分配空间  
  16.   
  17. rv = apr_file_open(&fin,"d:\\in.txt",APR_READ|APR_BUFFERED,APR_OS_DEFAULT,pool);//打开可读文件  
  18. if( rv == APR_SUCCESS ){  
  19. strcpy(buf,"fileread_open succeed!");  
  20. printf("this is a test:%s\n",buf);  
  21. }  
  22. apr_file_lock(fin,APR_FLOCK_SHARED);//读锁,允许其他读者进线程对它读,但不能有写者对它操作  
  23. /* 
  24. * 打开可写文件 
  25. * 其中APR_CREATE表示若没有该文件则创建一个,APR_TRUNCATE表示若文件有内容则全部删掉(APR_APPEND为在文件末添加) 
  26. */  
  27. rv = apr_file_open(&fout,"d:\\out.txt",APR_WRITE|APR_BUFFERED|APR_CREATE|APR_TRUNCATE,APR_OS_DEFAULT,pool);  
  28. if( rv == APR_SUCCESS ){  
  29. strcpy(buf,"filewrite_open succeed!");  
  30. printf("this is a test:%s\n",buf);  
  31. }  
  32. apr_file_lock(fout,APR_FLOCK_EXCLUSIVE);//写锁,不允许其他进程对此文件操作  
  33. /* 
  34. * 将in.txt文件中的内容全部复制到out.txt文件中 
  35. */  
  36. contentBuf = apr_palloc(pool,MEM_ALLOC_SIZE);  
  37. do{  
  38. apr_file_read(fin,contentBuf,&len);  
  39. //contentBuf[len] = 0;  
  40. apr_file_write(fout,contentBuf,&len);  
  41. while( len != 0 );  
  42.   
  43. //关闭文件in.txt  
  44. apr_file_unlock(fin);  
  45. apr_file_close(fin);  
  46.   
  47. /* 
  48. * 将复制in.txt为in_bak.txt,同时删掉in.txt文件 
  49. */  
  50. apr_file_copy("d:\\in.txt","d:\\in_bak.txt",APR_OS_DEFAULT,pool);  
  51. apr_file_remove("d:\\in.txt",pool);  
  52.   
  53. apr_file_info_get(&finfo,APR_FINFO_NAME,fout);//获取文件信息方法1:通过文件对象  
  54. printf("file name is:%s",finfo.fname);  
  55.   
  56. apr_stat(&finfo,"d:\\in_bak.txt",APR_FINFO_SIZE,pool);//获取文件信息方法2:通过文件路径  
  57. printf("file size is:%d bytes",finfo.size);  
  58.   
  59. //关闭文件out.txt  
  60. apr_file_unlock(fout);  
  61. apr_file_close(fout);  
  62.   
  63. apr_pool_destroy(pool);//销掉内存池对象  
  64. apr_terminate();//结束  
  65. return APR_SUCCESS;  
  66.   
  67. }  


3 目录操作

[cpp]  view plain  copy
  1. #define MEM_ALLOC_SIZE 1024  
  2. void traversal(char * dirPath, apr_pool_t * pool) {//-r遍历该目录下的所有文件  
  3.   
  4. apr_finfo_t finfo;//用来保存文件信息的结构体  
  5. apr_dir_t *dir;  
  6. if( apr_dir_open(&dir,dirPath,pool) != APR_SUCCESS ) return;//获得目录对象,若不成功则直接返回  
  7.   
  8. while(apr_dir_read(&finfo,APR_FINFO_NAME|APR_FINFO_TYPE,dir) == APR_SUCCESS ) {//读该目录下的所有文件信息,存到finfo中  
  9. if( finfo.filetype == APR_DIR ) {//该文件是目录对象  
  10. if( strcmp(finfo.name,".") == 0 || strcmp(finfo.name,"..") == 0 ) {//该文件是.或..,则继续  
  11. continue;  
  12. }else {//该文件是目录,且不是.或..,则递归进入  
  13. char * subDirPath;  
  14. subDirPath = apr_pstrcat(pool,dirPath,"\\",finfo.name,NULL);//subDirPath = dirPath + "\\" + finfo.name + NULL  
  15. traversal(subDirPath,pool);  
  16. }  
  17. }else if( finfo.filetype == APR_REG ) {//文件是普通文件,输出  
  18. printf("%s\\%s\n",dirPath,finfo.name);  
  19. }else {  
  20. printf("非正常文件:%s\\%s\n",dirPath,finfo.name);//文件是非普通文件,输出  
  21. }  
  22. }  
  23. }  
  24. int main(int argc, const char * const argv[])  
  25. {  
  26. apr_pool_t * pool;//内存池  
  27. apr_status_t rv;  
  28.   
  29. rv = apr_initialize();//初始化  
  30. rv = apr_pool_create(&pool,NULL);//创建pool  
  31.   
  32. traversal("d:",pool);//开始遍历  
  33.   
  34. apr_pool_destroy(pool);//销掉内存池对象  
  35. apr_terminate();//结束  
  36. return APR_SUCCESS;  
  37.   
  38. }  


4 自定义命令行参数


[cpp]  view plain  copy
  1. static const apr_getopt_option_t cmd_option[] = {  
  2. {"in",'i',TRUE,"input filename"},  
  3. {"out",'o',TRUE,"output filename"},  
  4. {"help",'h',FALSE,"show help information"},  
  5. {NULL,0,0,NULL},//哨兵  
  6. };  
  7. #define MEM_ALLOC_SIZE 1024  
  8. int main(int argc, const char * const argv[])  
  9. {  
  10. apr_pool_t * pool;//内存池  
  11. apr_status_t rv;  
  12. apr_getopt_t * opt;  
  13. int optch;  
  14. char * optarg;  
  15. rv = apr_initialize();//初始化  
  16. rv = apr_pool_create(&pool,NULL);//创建pool  
  17.   
  18. apr_getopt_init(&opt,pool,argc,argv);//将命令行内容填入opt结构中  
  19.   
  20. while( (rv = apr_getopt_long(opt,cmd_option,&optch,&optarg)) == APR_SUCCESS ) {//一个一个解析命令  
  21. switch(optch) {  
  22. case 'i':  
  23. printf("i参数是%s",optarg);  
  24. break;  
  25. case 'o':  
  26. printf("o参数是%s",optarg);  
  27. break;  
  28. case 'h':  
  29. printf("h没有参数");  
  30. break;  
  31. default:  
  32. break;  
  33. }  
  34. }  
  35.   
  36. if( rv != APR_EOF ){//判断是否全部解析  
  37. printf("解析命令行错误!");  
  38. }  
  39.   
  40. apr_pool_destroy(pool);//销掉内存池对象  
  41. apr_terminate();//结束  
  42. return APR_SUCCESS;  
  43.   
  44. }  

参考:http://hi.baidu.com/qteqpid_pku/item/c8e89813ad9b4d8988a95633

http://blog.sina.com.cn/s/blog_625cce080100g1jp.html

猜你喜欢

转载自blog.csdn.net/qq_28098067/article/details/79868152