foxygit / doom Log in
commit 160ff32f2fb8ba7112938790044684c61a0b07b1
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Mon Jan 11 19:10:42 2010 +0000
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Mon Jan 11 19:10:42 2010 +0000

    Insert new files into the command line at the correct location, allowing
    multiple files to be opened at once.

    Subversion-branch: /trunk/chocolate-doom
    Subversion-revision: 1803
---
 pkg/osx/AppController.h   |  1 +
 pkg/osx/AppController.m   | 15 +++++++++++++++
 pkg/osx/LauncherManager.h |  1 +
 pkg/osx/LauncherManager.m | 47 ++++++++++++++++++++++++++++++++++++++++-------
 4 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/pkg/osx/AppController.h b/pkg/osx/AppController.h
index 6714eb2f..88b59043 100644
--- a/pkg/osx/AppController.h
+++ b/pkg/osx/AppController.h
@@ -30,6 +30,7 @@
 @interface AppController : NSObject
 {
     LauncherManager *launcherManager;
+    BOOL filesAdded;
 }

 + (void)initialize;
diff --git a/pkg/osx/AppController.m b/pkg/osx/AppController.m
index 03a5965b..32afd9c4 100644
--- a/pkg/osx/AppController.m
+++ b/pkg/osx/AppController.m
@@ -46,6 +46,8 @@
     {
     }

+    self->filesAdded = NO;
+
     return self;
 }

@@ -79,6 +81,17 @@
 {
     NSString *extension;

+    // If this is the first file added, clear out the existing
+    // command line.  This allows us to select multiple files
+    // in the finder and open them all together (for TCs, etc).
+
+    if (!self->filesAdded)
+    {
+        [self->launcherManager clearCommandLine];
+    }
+
+    // Add file with appropriate command line option based on extension:
+
     extension = [fileName pathExtension];

     if (![extension caseInsensitiveCompare: @"wad"])
@@ -96,6 +109,8 @@
         return NO;
     }

+    self->filesAdded = YES;
+
     return YES;
 }

diff --git a/pkg/osx/LauncherManager.h b/pkg/osx/LauncherManager.h
index de95265a..eac4cfba 100644
--- a/pkg/osx/LauncherManager.h
+++ b/pkg/osx/LauncherManager.h
@@ -40,6 +40,7 @@
 - (void) launch: (id)sender;
 - (void) runSetup: (id)sender;
 - (void) awakeFromNib;
+- (void) clearCommandLine;
 - (void) addFileToCommandLine: (NSString *) fileName
          forArgument: (NSString *) args;

diff --git a/pkg/osx/LauncherManager.m b/pkg/osx/LauncherManager.m
index a67e5cc9..0cde9f6e 100644
--- a/pkg/osx/LauncherManager.m
+++ b/pkg/osx/LauncherManager.m
@@ -79,6 +79,7 @@ static NSString *GetNextArgument(NSString *commandLine, int *pos, int *arg_pos)

     if (*pos >= [commandLine length])
     {
+        *arg_pos = *pos;
         return nil;
     }

@@ -193,6 +194,42 @@ static int GetFileInsertIndex(NSString *commandLine, NSString *needle)
     return arg_pos;
 }

+// Given the specified string, append a filename, quoted if necessary.
+
+static NSString *AppendQuotedFilename(NSString *str, NSString *fileName)
+{
+    int i;
+
+    // Search the filename for spaces, and quote if necessary.
+
+    for (i=0; i<[fileName length]; ++i)
+    {
+        if (isspace([fileName characterAtIndex: i]))
+        {
+            str = [str stringByAppendingString: @" \""];
+            str = [str stringByAppendingString: fileName];
+            str = [str stringByAppendingString: @"\" "];
+
+            return str;
+        }
+    }
+
+    str = [str stringByAppendingString: @" "];
+    str = [str stringByAppendingString: fileName];
+
+    return str;
+}
+
+// Clear out the existing command line options.
+// Invoked before the first file is added.
+
+- (void) clearCommandLine
+{
+    [self->commandLineArguments setStringValue: @""];
+}
+
+// Add a file to the command line to load with the game.
+
 - (void) addFileToCommandLine: (NSString *) fileName
          forArgument: (NSString *) arg
 {
@@ -215,10 +252,7 @@ static int GetFileInsertIndex(NSString *commandLine, NSString *needle)
     {
         commandLine = [commandLine stringByAppendingString: @" "];
         commandLine = [commandLine stringByAppendingString: arg];
-
-        commandLine = [commandLine stringByAppendingString: @" \""];
-        commandLine = [commandLine stringByAppendingString: fileName];
-        commandLine = [commandLine stringByAppendingString: @"\""];
+        commandLine = AppendQuotedFilename(commandLine, fileName);
     }
     else
     {
@@ -232,9 +266,8 @@ static int GetFileInsertIndex(NSString *commandLine, NSString *needle)

         // Construct new command line:

-        commandLine = [start stringByAppendingString: @" \""];
-        commandLine = [commandLine stringByAppendingString: fileName];
-        commandLine = [commandLine stringByAppendingString: @"\" "];
+        commandLine = AppendQuotedFilename(start, fileName);
+        commandLine = [commandLine stringByAppendingString: @" "];
         commandLine = [commandLine stringByAppendingString: end];
     }