2015-08-03 63 views
0

我有一個詢問外部進程的工具,我想添加一些在Info.plist文件上工作的objective-c查詢。OS X如何獲取應用程序的基本路徑,超出可執行的完整路徑

爲了做到這一點,我使用的是應用程序的基本路徑下面的代碼作爲輸入(即/Applications/Notes.app/)

NSDictionary* infoDict = [[NSBundle bundleWithPath:vlcFilePath] infoDictionary]; 

然而,我只能得到可執行文件路徑從遠程過程(即/Applications/Notes.app/Contents/MacOS/Notes),我想轉換爲基本路徑。

我使用的std ::字符串與RFIND爲了得到我所需要的,但我不知道是否有是還可以在一些有效的非convensional路徑編隊運行(我的代碼是脆弱的路徑更傳統的方式它包含幾個連續的斜槓,如/Application/Notes.app/Contents/MacOS//Notes)。

我的代碼(正好等於倒退到父目錄3次):

for (int i=0;i<3;i++) { 
    const size_t last_slash_idx = executable.rfind('/'); 
    if (std::string::npos != last_slash_idx) { 
     executable = executable.substr(0, last_slash_idx); 
    } 
} 

回答

0

好,我已經使用,它可以查找最後一次出現string.rfind方法發現了一些巧妙的解決辦法多次嘗試後「.app」,然後從該索引開始截斷字符串。

std::string app_suffix(".app"); 
int split_idx = executable.rfind(app_suffix); 
executable = executable.substr(0,split_idx+app_suffix.size());