星期四, 4月 17, 2008

測試

今天中午醒來,思考,如果unix system call都是C 寫成的,是否有辦法用class包起來呢,於是寫了一個小小的class測試一下。

class FileAttribute{
public:
FileAttribute(const char* dir, const char* file){
name = string(file);
path = string(dir) + "/" + name;
stat(path.c_str(), &buf);
};
FileAttribute(string& dir, string& file){
name = file;
path = dir + "/" + file;
stat(path.c_str(), &buf);
};
const char* ab_file_path(){ return path.c_str(); };
const char* file_name(){ return name.c_str(); };
bool is_file(){ return !S_ISDIR(buf.st_mode); };
bool is_dir(){ return !is_file(); };
unsigned int size(){ return buf.st_size; };
private:
string path;
string name;
struct stat buf;
};

看起來成效不錯,其實效率很差,多一層包裝,就要浪費掉很多時間在此包裝上,不過如果在發展大型程式時,還是有其包裝的必要。
---
我真無聊,不過我喜歡XD

沒有留言: