obj-c編程10:Foundation庫中類的使用(4)[文件管理,查詢當前進程信息]
接上一篇文件管理博文.我們可以用NSPathUtilities.h中包含的NSString函數和分類擴展來以兼容的方式處理路徑.下麵凡是有係統編程經驗的童鞋都知道是啥意思了:
#import <Foundation/Foundation.h>
#define msg(...) NSLog(__VA_ARGS__)
int main(int argc, char *argv[]){
@autoreleasepool {
NSString *short_name = @"love.m";
NSArray *componets;
NSString *tmp_dir = NSTemporaryDirectory();
msg(@"tmp_dir : %@",tmp_dir);
NSString *full_name = [tmp_dir stringByAppendingPathComponent:short_name];
msg(@"full_name : %@",full_name);
NSString *ext_name = [full_name pathExtension];
msg(@"ext_name : %@",ext_name);
NSString *home_dir = NSHomeDirectory();
msg(@"home_dir : %@",home_dir);
componets = [@"/usr/local/src/linux/xxx.img" pathComponents];
msg(@"every components : %@",componets);
NSString *path = [@"~/src/objc_src/love.m" stringByExpandingTildeInPath];
msg(@"expand path : %@",path);
path = [path stringByStandardizingPath];
msg(@"expand_way2 path : %@",path);
msg(@"user:%@ , full_user:%@",NSUserName(),NSFullUserName());
//last is longer...
NSArray *list = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,\
NSUserDomainMask,YES);
msg(@"%@",list);
list = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,\
NSSystemDomainMask|NSLocalDomainMask|NSNetworkDomainMask,YES);
msg(@"%@",list);
NSString *full_path = [list objectAtIndex:0];
full_path = [full_path stringByAppendingPathComponent:@"love.m"];
msg(@"%@",full_path);
}
return 0;
}
以上代碼含義都很清楚就不一一解釋了,值得說明的是最後一個函數NSSearchPathForDirectoriesInDomains,如果第二個參數為多個單體枚舉的聚集,則結果會是一個數組,代碼說的很清楚了,上運行結果:
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f
2014-07-02 15:00:14.756 f[12608] tmp_dir : /tmp/GNUstepSecure1000
2014-07-02 15:00:14.758 f[12608] full_name : /tmp/GNUstepSecure1000/love.m
2014-07-02 15:00:14.758 f[12608] ext_name : m
2014-07-02 15:00:14.759 f[12608] home_dir : /home/wisy
2014-07-02 15:00:14.759 f[12608] every components : ("/", usr, local, src, linux, "xxx.img")
2014-07-02 15:00:14.759 f[12608] expand path : /home/wisy/src/objc_src/love.m
2014-07-02 15:00:14.759 f[12608] expand_way2 path : /home/wisy/src/objc_src/love.m
2014-07-02 15:00:14.759 f[12608] user:wisy , full_user:wisy,,,
2014-07-02 15:00:14.759 f[12608] ("/home/wisy/GNUstep/Applications")
2014-07-02 15:00:14.759 f[12608] ("/usr/local/lib/GNUstep/Applications", "/usr/lib/GNUstep/Applications")
2014-07-02 15:00:14.759 f[12608] /usr/local/lib/GNUstep/Applications/love.m
接下來好玩的來鳥,何曾幾時本貓都對入侵其他進程有著變態的偏執狂欲望(這在本貓的其他博文中略有體現),雖然下麵代碼隻是查詢本進程的信息,但不積跬步,何以致千裏,我們慢慢來吧:
#import <Foundation/Foundation.h>
#define msg(...) NSLog(__VA_ARGS__)
int main(int argc, char *argv[]){
@autoreleasepool {
NSProcessInfo *proc = [NSProcessInfo processInfo];
NSArray *args = [proc arguments];
//msg(@"all_infos : %@",proc);
msg(@"args : %@",args);
NSDictionary *env = [proc environment];
msg(@"env : %@",env);
msg(@"pid : %d",[proc processIdentifier]);
msg(@"process name :%@",[proc processName]);
msg(@"hostname : %@",[proc hostName]);
msg(@"OS type : %d",(unsigned)[proc operatingSystem]);
msg(@"OS name : %@",[proc operatingSystemName]);
msg(@"rand string : %@",[proc globallyUniqueString]);
msg(@"OS ver : %@",[proc operatingSystemVersionString]);
}
return 0;
}
運行結果如下:
wisy@wisy-ThinkPad-X61:~/src/objc_src$ ./f 11 22 33 "aa bb cc"
2014-07-02 15:26:34.711 f[12767] args : ("./f", 11, 22, 33, "aa bb cc")
2014-07-02 15:26:34.713 f[12767] env : {"CLUTTER_IM_MODULE" = xim; COLORTERM = "gnome-terminal"; "DBUS_SESSION_BUS_ADDRESS" =
"unix:abstract=/tmp/dbus-obtBgG4wMe"; "DEFAULTS_PATH" = "/usr/share/gconf/xfce.default.path"; "DESKTOP_SESSION" = xfce;
DISPLAY = ":0.0"; GDMSESSION = xfce; "GDM_LANG" = "zh_CN"; "GLADE_CATALOG_PATH" = ":"; "GLADE_MODULE_PATH" = ":";
"GLADE_PIXMAP_PATH" = ":"; "GNOME_KEYRING_CONTROL" = "/run/user/1000/keyring-1BJWxI"; "GNOME_KEYRING_PID" = 1714;
"GTK_IM_MODULE" = fcitx; "GTK_MODULES" = "overlay-scrollbar:unity-gtk-module"; HOME = "/home/wisy"; "IM_CONFIG_PHASE" = 1;
INSTANCE = ""; JOB = dbus; LANG = "zh_CN.UTF-8"; LANGUAGE = "zh_CN:zh"; LESSCLOSE = "/usr/bin/lesspipe %s %s";
LESSOPEN = "| /usr/bin/lesspipe %s"; LOGNAME = wisy; "LS_COLORS" = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;300;36:/";
"UBUNTU_MENUPROXY" = 1; "UPSTART_EVENTS" = "started xsession"; "UPSTART_INSTANCE" = ""; "UPSTART_JOB" = startxfce4;
"UPSTART_SESSION" = "unix:abstract=/com/ubuntu/upstart-session/1000/1717"; USER = wisy; "VTE_VERSION" = 3409;
WINDOWID = 62914568; XAUTHORITY = "/home/wisy/.Xauthority"; "XDG_CONFIG_DIRS" = "/etc/xdg/xdg-xfce:/usr/share/upstart/xdg:/etc/xdg:/etc/xdg";
"XDG_CURRENT_DESKTOP" = XFCE; "XDG_DATA_DIRS" = "/usr/share/xfce:/usr/share/xfce4:/usr/local/share/:/usr/share/:/usr/share";
"XDG_GREETER_DATA_DIR" = "/var/lib/lightdm-data/wisy"; "XDG_MENU_PREFIX" = "xfce-"; "XDG_RUNTIME_DIR" = "/run/user/1000";
"XDG_SEAT" = seat0; "XDG_SEAT_PATH" = "/org/freedesktop/DisplayManager/Seat0"; "XDG_SESSION_ID" = c2; "XDG_SESSION_PATH" =
"/org/freedesktop/DisplayManager/Session0"; "XDG_VTNR" = 7; XMODIFIERS = "@im=fcitx"; "_" = "./f";(太長截取一部分!) }
2014-07-02 15:26:34.714 f[12767] pid : 12767
2014-07-02 15:26:34.714 f[12767] process name :f
2014-07-02 15:26:34.715 f[12767] hostname : wisy-ThinkPad-X61
2014-07-02 15:26:34.715 f[12767] OS type : 100
2014-07-02 15:26:34.715 f[12767] OS name : GSGNULinuxOperatingSystem
2014-07-02 15:26:34.715 f[12767] rand string : wisy-ThinkPad-X61_31df_1963ebaa_0
2014-07-02 15:26:34.715 f[12767] OS ver : 3.13.0-24-generic
第三種文件拷貝方式,我們使用了NSFileHandle類,該類功能的一個簡單流程就是:
1 打開文件
2 對打開得文件執行I/O操作
3 關閉文件
#import <Foundation/Foundation.h>
#define msg(...) NSLog(__VA_ARGS__)
int main(int argc,char *argv[])
{
@autoreleasepool{
NSFileHandle *in_file,*out_file;
NSData *buf;
in_file = [NSFileHandle fileHandleForReadingAtPath:@"x.db"];
if(in_file == nil){
msg(@"open in_file failed");
return 1;
}
[[NSFileManager defaultManager] createFileAtPath:@"out.db" \
contents:nil attributes:nil];
out_file = [NSFileHandle fileHandleForWritingAtPath:@"out.db"];
if(out_file == nil){
msg(@"open out_file failed");
return 2;
}
//可能包含數據,先截斷。
[out_file truncateFileAtOffset:0];
buf = [in_file readDataToEndOfFile];
[out_file writeData:buf];
[in_file closeFile];
[out_file closeFile];
msg(@"%@",[NSString stringWithContentsOfFile:@"out.db" \
encoding:NSUTF8StringEncoding error:NULL]);
}
return 0;
}
NSURL類可以從網頁上讀取對象,這個前麵有一個例子,這裏不再展開說了,自己度娘哦。
最後還有一個NSBundle類,我簡單看了下,類似於windows中的對程序中資源文件的定位操作,不過os x和ios資源的自包含特性更強一些;在ios中進程都在沙盒中運行讀取的/目錄統統都是虛擬出來的,否則全亂套鳥。貌似扯多鳥...
最後更新:2017-04-03 05:38:54