Commits

dergraf83  committed 3bf9d1c

Category on NSDictionary useful for CGI programming

  • Participants
  • Parent commits 9eb9c0e

Comments (0)

Files changed (2)

File CGI/NSDictionary+CGI.h

+/* Copyright (c) 2010 Sven Weidauer
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
+ * documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
+ * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
+ * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
+ */
+
+#import <Foundation/Foundation.h>
+
+@interface NSDictionary (CGIAdditions)
+
++ (NSDictionary *) dictionaryWithEnvironment;
++ (NSDictionary *) dictionaryForQueryString: (NSString *) query;
+
+@end

File CGI/NSDictionary+CGI.m

+/* Copyright (c) 2010 Sven Weidauer
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
+ * documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
+ * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 
+ * Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
+ * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
+ */
+
+
+#import "NSDictionary+CGI.h"
+
+extern char **environ;
+
+
+@implementation NSDictionary (CGIAdditions)
+
++ (NSDictionary *) dictionaryWithEnvironment;
+{	
+	NSMutableDictionary *environDict = [NSMutableDictionary dictionary];
+	for (char **cur = environ; 0 != *cur; cur++) {
+		NSString *item = [NSString stringWithCString: *cur encoding: NSASCIIStringEncoding ];
+		NSRange range = [item rangeOfString: @"="];
+		
+		if (range.location == NSNotFound) continue;
+		
+		NSString *key = [item substringToIndex: range.location];
+		NSString *value = [item substringFromIndex: range.location + 1];
+		
+		[environDict setObject: value forKey: key];
+	}
+	
+	return environDict;
+}
+
++ (NSDictionary *) dictionaryForQueryString: (NSString *) queryString;
+{
+	NSMutableDictionary *query = [NSMutableDictionary dictionary];
+	
+	if (queryString) {
+		NSScanner *queryScanner = [NSScanner scannerWithString: queryString];
+		NSCharacterSet *querySeparatorsSet = [NSCharacterSet characterSetWithCharactersInString: @"&="];
+		
+		while (![queryScanner isAtEnd]) {
+			NSString *key = nil;
+			NSString *value = @"";
+			
+			[queryScanner scanUpToCharactersFromSet: querySeparatorsSet intoString: &key];
+			
+			if ([queryScanner scanString: @"=" intoString: 0]) {
+				[queryScanner scanUpToString: @"&" intoString: &value];
+			} else {
+				[queryScanner scanString: @"&" intoString: 0];
+				value = key;
+			}
+			
+			value = [value stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
+			if (key) [query setObject: value forKey: key];
+		}
+	}
+	
+	return query;
+}
+
+@end