2012-06-06 27 views
1

要做簡單的字符串操作比我要使用的操作要簡單一些。我有一個方法允許用戶複製目錄中的文件。如果他們在表格視圖中選擇它並按下克隆按鈕,則該文件的副本將被保存,並且fileName將會附加子字符串Copy。如果Copy已經存在,那麼我們需要迭代文件名並通過循環迭代附加文件名。例如;在循環中查找和更改字符串中的數字後綴

<filename> Copy 1 
<filename> Copy 2 
until my other method return that the name is unique. 

我需要檢查三個條件傳入的文件名; 它是否已經有「複製」附加到它 它是否已經有一個字符串號碼附加到它 如果是這樣,獲取數值的值迭代它並將其放回。

相當長一段時間後,我只能想出這樣的:

//Tokenize the string 
NSArray *filenameArray =[copyName componentsSeparatedByString:@" "]; 

//Make sure the name is unique 
//Update the namesArray 
[self montageNameList]; 
int i = 1; 
for(NSString * name in self.montageNames){ 

    if([channelSetManager_ checkNoDuplicateName:self.montageNames forThisName:copyName]== YES){ 
     break; 
    }else{ 

     if([[filenameArray lastObject]isEqualToString:@"Copy"]){ 

      //Just add a number to the end of the string 
      copyName = [copyName stringByAppendingFormat:@" %d", i]; 

     }else if(([[filenameArray lastObject]intValue] > -1) && ([[filenameArray lastObject]intValue] < 100)){ 

      i = [[filenameArray lastObject]intValue]+1; 
      NSInteger len = [[filenameArray lastObject]length]+1; 
      copyName = [copyName substringToIndex:[copyName length] - len ]; 
      copyName = [copyName stringByAppendingFormat:@" %d",i]; 
     } 

    } 


} 

這工作,但似乎不是正確的方法。任何意見是極大的讚賞。

+0

這其實不是一個糟糕的實現都:本身是不漂亮的問題,所以,解決它不能既漂亮的算法。 – dasblinkenlight

+0

好的。我很欣賞這些意見 – Miek

回答

0

在過去,我使用mkstemp()創建了一個臨時文件,然後將其鏈接到永久文件的臨時文件,如果鏈接失敗並且存在文件,則嘗試下一個文件名,直到鏈接成功爲止,然後取消鏈接臨時文件。

// Finds next available copy name and creates empty file as place holder 
- (NSString *) createCopyFileWithBasename:(NSString *)baseName andSuffix:(NSString *)suffix 
{ 
    char tmpFile[1024]; 
    char copyFile[1024]; 
    int fd; 
    int count; 

    // create unique temporary file 
    snprintf(tmpFile, 1024, "%s.XXXXXXXX", [baseName UTF8String]); 
    if ((fd = mkstemp(tmpFile)) == -1) 
    { 
     NSLog(@"mkstemp(): %s", strerror(errno)); 
     return(nil); 
    }; 
    close(fd); 

    // attempt to create empty file for file copy 
    snprintf(copyFile, 1024, "%s Copy.%s", [baseName UTF8String], [suffix UTF8String]); 
    switch(link(tmpFile, copyFile)) 
    { 
     case EEXIST: // file exists, skip to next possible name 
     break; 

     case: 0: // excellent, we found an available file name 
     unlink(tmpFile); 
     return([NSString stringWithUTF8String:copyFile]); 

     default: 
     NSLog(@"link(): %s", strerror(errno)); 
     unlink(tmpFile); 
     return(nil); 
    }; 

    // loop through possible iterations of file copy's name 
    for(count = 1; count < 1024; count++) 
    { 
     snprintf(copyFile, 1024, "%s Copy %i.s", [baseName UTF8String], count, [suffix UTF8String]); 
     switch(link(tmpFile, copyFile)) 
     { 
      case EEXIST: // file exists, skip to next possible name 
      break; 

      case: 0: // excellent, we found an available file name 
      unlink(tmpFile); 
      return([NSString stringWithUTF8String:copyFile]); 

      default: 
      NSLog(@"link(): %s", strerror(errno)); 
      unlink(tmpFile); 
      return(nil); 
     }; 
    }; 

    unlink(tmpFile); 
    NSLog(@"link(): %s", strerror(errno)); 

    return(nil); 
} 

用法示例:

NSString copyFileName = [self createCopyFileWithBasename:@"My Data" andSuffix:@".txt"];