mozilla code -nspr 文件I/O - 1

来源:互联网 发布:java 英语单词数据库 编辑:程序博客网 时间:2024/06/05 05:43

This chapter describes the most common NSPR types, enumerations, and structures used with the functions described inChapter 10 "I/O Functions" and Chapter 11 "Network Addresses." These include the types used for system access, normal file I/O, and socket (network) I/O.

在nspr中I/O其实包括了系统访问 ,文件I/O 和socket I/O,这里只学习文件I/O

一.目录访问:

目录结构体 :

1.  PRDir 

2. PRDirEntry用PR_OpenDir,用PR_ReadDir循环遍历,用PR_CloseDir关闭

函数: 

1,PR_OpenDir

//打开指定定的路径

//如果打开成功,则返回一个PRDir 对象,如果打不开指定对象,则返回NULL

//不用了一定要用PR_CloseDir把PR_OpenDir返回的对象关闭.

//对于windows 好像是调用了FindFirstFile

NSPR_API(PRDir*) PR_OpenDir(const char *name);


2.PR_ReadDir
//用PR_OpenDir返回的PRDir 来遍历PR_OpenDir参数所指的目标 (PR_OpenDir要返回正确的PRDir,而不是NULL)

//对于windows好像是调用了FindNextFile()

//返回PRDirEntry*,或者当遍历完or 出错时返回NULL可以调用PR_GetError().

struct PRDirEntry {
    const char *name;        /* name of entry, relative to directory name */
};

typedef enum PRDirFlags {
    PR_SKIP_NONE = 0x0, //遍历所有文件 包括 ./  和.. /
    PR_SKIP_DOT = 0x1, //不遍历 ./
    PR_SKIP_DOT_DOT = 0x2,//不遍历 ../
    PR_SKIP_BOTH = 0x3, //不遍历 ./ 和../
    PR_SKIP_HIDDEN = 0x4   //不遍历隐藏文件(only for windows)
} PRDirFlags;

NSPR_API(PRDirEntry*) PR_ReadDir(PRDir *dir, PRDirFlags flags);

3.PR_CloseDir

//关闭由PR_OpenDir打开的文目录,成功返回PR_SUCCESS,其它返回PR_FAILURE,可用PR_GetError();查找

NSPR_API(PRStatus) PR_CloseDir(PRDir *dir);

4.PR_GetFileInfo

struct PRFileInfo {
    PRFileType type;        /* Type of file */
    PROffset32 size;        /* Size, in bytes, of file's contents */
    PRTime creationTime;    /* Creation time per definition of PRTime */
    PRTime modifyTime;      /* Last modification time per definition of PRTime */
};

struct PRFileInfo64 {
    PRFileType type;        /* Type of file */
    PROffset64 size;        /* Size, in bytes, of file's contents */
    PRTime creationTime;    /* Creation time per definition of PRTime */
    PRTime modifyTime;      /* Last modification time per definition of PRTime */
};

typedef enum PRFileType
{
    PR_FILE_FILE = 1,
    PR_FILE_DIRECTORY = 2,
    PR_FILE_OTHER = 3
} PRFileType;


/****************************************************************************
 * FUNCTION: PR_GetFileInfo, PR_GetFileInfo64
 * DESCRIPTION:
 *     Get the information about the file with the given path name. This is
 *     applicable only to NSFileDesc describing 'file' types (see
 * INPUTS:
 *     const char *fn
 *         path name of the file
 * OUTPUTS:
 *     PRFileInfo *info
 *         Information about the given file is written into the file
 *         information object pointer to by 'info'.
 * RETURN: PRStatus
 *     PR_GetFileInfo returns PR_SUCCESS if file information is successfully
 *     obtained, otherwise it returns PR_FAILURE.
 ***************************************************************************
 */

NSPR_API(PRStatus) PR_GetFileInfo(const char *fn, PRFileInfo *info);
NSPR_API(PRStatus) PR_GetFileInfo64(const char *fn, PRFileInfo64 *info);

//例程(vc2008)

#include "nspr.h"

#include <iostream>
#include <vector>
#include <string>

#pragma comment(lib,"nspr4.lib")
#pragma comment(lib,"plc4.lib")

using namespace std;
int main(int argc, char* argv[])
{
    std::vector<std::string> vDirName;
    vector<string> vFileName;
    char err[256];
    string strBaseName= "c:\\";
    string strFileName;
    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    PRFileInfo info = {};
    PRDir* dir = PR_OpenDir(strBaseName.c_str());
    PRDirEntry* dirEntry = PR_ReadDir(dir, PR_SKIP_BOTH);
    
    do{
        strFileName.clear();
        strFileName = strBaseName + dirEntry->name;
        if (PR_SUCCESS == PR_GetFileInfo(strFileName.c_str(), &info))
        {        
            if (info.type == PR_FILE_DIRECTORY)
            {
                vDirName.push_back(strFileName);
                cout << vDirName.back().c_str() << "  Dir" << endl;
            }else
            {
                vFileName.push_back(strFileName);
                cout << vFileName.back().c_str() << "  File" << endl;
            }
        }
        else{
            PR_GetErrorText(err);
        }

    }while ((dirEntry = PR_ReadDir(dir, PR_SKIP_BOTH)));
    PR_CloseDir(dir);    
    PR_Cleanup();

    return 0;
}

5.PR_MkDir

//用给定的名称建立一个文件夹,如果成功则返回PR_SUCCESS,反之返回PR_FAILURE,可以用PR_GetError()得到错误.

//第一个参数,为所要建立的文件名,如果只给定一个合法的名称,则在当前目录建立这个文件夹,如果要在其它目录建立文件夹,

\\则要给定完整的路径名,最后一个为想要建立的目录名称,一次只能建立一个目录,

/*
** File modes ....访问模式:PRIntn mode
**
**只针对Unix-like 系统.的ext2,ext3,ext4硬盘文件格式.-rwxrwxrwx
**
**   00400   Read by owner.
**   00200   Write by owner.
**   00100   Execute (search if a directory) by owner.
**   00040   Read by group.
**   00020   Write by group.
**   00010   Execute by group.
**   00004   Read by others.
**   00002   Write by others
**   00001   Execute by others.
**
*/
NSPR_API(PRStatus) PR_MkDir(const char *name, PRIntn mode);

6.PR_MakeDir
//与PR_MkDir类似,但好像就是多了初始化代码(如果没有初始化,则进行初始化,PR_MkDir则没有),

NSPR_API(PRStatus) PR_MakeDir(const char *name, PRIntn mode);

7.PR_RmDir

//删除给定的目录.成功返回PR_SUCCESS,反之返回PR_FAILURE,可以用PR_GetError()得到错误

//要删除的文件夹一定要是空文件夹,如果不是PR_GetError 返回PR_DIRECTORY_NOT_EMPTY_ERROR

//可能给定完整的路径,最后一个合法的名称如果存在而且可以删除(没有被占用,有足够的权限等)的话,将会被删除.

NSPR_API(PRStatus) PR_RmDir(const char *name);


//例程(vc2008)

#include "nspr.h"

#include <iostream>

#pragma comment(lib,"nspr4.lib")
#pragma comment(lib,"plc4.lib")

using namespace std;
int main(int argc, char* argv[])
{

    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    PRErrorCode err;
    if (PR_FAILURE == PR_MkDir("c:\\test_dir", 0))
    {
        err = PR_GetError();
    }
    else
    {
        cout << "succeed to create the dir" <<endl;
    }
    PRFileInfo info;
    PR_GetFileInfo("c:\\test_dir", &info);
    if (info.type == PR_FILE_DIRECTORY) //检查这个目录是不是真的存在
    {
        PR_Sleep( 5000);  // 等待5s,让人看一下有没有的确建立好这个目录.
        if (PR_SUCCESS == PR_RmDir("c:\\test_dir"))
        {
            cout << "succeed to delete th dir" <<endl;
        }
    }
    PR_Cleanup();
    return 0;
}