Permalink
Browse files

Merge branch 'master' of github.com:mikeho/QSUtilities

  • Loading branch information...
2 parents 2a9dd41 + 7ae1faf commit 92908f69d6b8be6391648ab45cd0acd70d4be235 @mikeho committed Oct 8, 2012
Showing with 56 additions and 21 deletions.
  1. +6 −1 QSFileManager.h
  2. +40 −2 QSFileManager.m
  3. +2 −2 QSHttpClient.m
  4. +7 −15 QSStrings.m
  5. +1 −1 QSUtilities.h
View
@@ -31,10 +31,15 @@
}
+ (NSString *)documentsFilePathForFile:(NSString *)strFile;
-
+ (bool)writeDocumentsFile:(NSString *)strFileName WithData:(NSData *)objData;
+ (NSData *)readDocumentsFile:(NSString *)strFileName;
++ (NSString *)offlineContentFilePathForFile:(NSString *)strFile;
++ (bool)writeOfflineContentFile:(NSString *)strFileName WithData:(NSData *)objData;
++ (NSData *)readOfflineContentFile:(NSString *)strFileName;
+
++ (bool)markFileAsDoNotBackup:(NSString *)strFileName;
+
+ (bool)writeFile:(NSString *)strFilePath WithData:(NSData *)objData;
+ (NSData *)readFile:(NSString *)strFilePath;
+ (NSInteger)fileSize:(NSString *)strFilePath;
View
@@ -24,8 +24,10 @@
*/
#import "QSFileManager.h"
+#include <sys/xattr.h>
static NSString * _strDocumentsPath = nil;
+static NSString * _strOfflineContentPath = nil;
@implementation QSFileManager
@@ -35,10 +37,21 @@ + (NSString *)documentsFilePathForFile:(NSString *)strFile {
_strDocumentsPath = [objPaths objectAtIndex:0];
[_strDocumentsPath retain];
}
-
+
return [_strDocumentsPath stringByAppendingPathComponent:strFile];
}
+
++ (NSString *)offlineContentFilePathForFile:(NSString *)strFile {
+ if (_strOfflineContentPath == nil) {
+ NSArray * objPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true);
+ _strOfflineContentPath = [[objPaths objectAtIndex:0] stringByAppendingPathComponent:@"Offline Content"];
+ [_strOfflineContentPath retain];
+ }
+
+ return [_strOfflineContentPath stringByAppendingPathComponent:strFile];
+}
+
+ (bool)writeFile:(NSString *)strFilePath WithData:(NSData *)objData {
return [[NSFileManager defaultManager] createFileAtPath:strFilePath contents:objData attributes:nil];
}
@@ -48,13 +61,38 @@ + (NSData *)readFile:(NSString *)strFilePath {
}
+ (bool)writeDocumentsFile:(NSString *)strFileName WithData:(NSData *)objData {
- return [QSFileManager writeFile:[QSFileManager documentsFilePathForFile:@"qcodo_large.tif"] WithData:objData];
+ return [QSFileManager writeFile:[QSFileManager documentsFilePathForFile:strFileName] WithData:objData];
}
+ (NSData *)readDocumentsFile:(NSString *)strFileName {
return [QSFileManager readFile:[QSFileManager documentsFilePathForFile:strFileName]];
}
++ (bool)writeOfflineContentFile:(NSString *)strFileName WithData:(NSData *)objData {
+ bool blnToReturn;
+ blnToReturn = [QSFileManager writeFile:[QSFileManager offlineContentFilePathForFile:strFileName] WithData:objData];
+
+ // Since we're dealing with Offline Content, we are required to set the "Do Not Back up" Extended Attribute
+ if (blnToReturn) {
+ blnToReturn = [QSFileManager markFileAsDoNotBackup:[QSFileManager offlineContentFilePathForFile:strFileName]];
+ }
+
+ return blnToReturn;
+}
+
++ (bool)markFileAsDoNotBackup:(NSString *)strFileName {
+ const char * strFilePath = [strFileName fileSystemRepresentation];
+ const char * strAttributeName = "com.apple.MobileBackup";
+ u_int8_t attrValue = 1;
+
+ int result = setxattr(strFilePath, strAttributeName, &attrValue, sizeof(attrValue), 0, 0);
+ return (result == 0);
+}
+
++ (NSData *)readOfflineContentFile:(NSString *)strFileName {
+ return [QSFileManager readFile:[QSFileManager offlineContentFilePathForFile:strFileName]];
+}
+
+ (NSInteger)fileSize:(NSString *)strFilePath {
NSDictionary * dctFileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:strFilePath error:NULL];
return [dctFileAttributes fileSize];
View
@@ -112,7 +112,7 @@ - (NSData *)getResponseAsRawData {
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (_objDelegate && [(NSObject *)_objDelegate respondsToSelector:@selector(httpClient:ErrorReceived:)])
- [_objDelegate httpClient:self ErrorReceived:@"Could not connect to the server."];
+ [_objDelegate httpClient:self ErrorReceived:NSLocalizedString(@"Could not connect to the server.", nil)];
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
@@ -139,7 +139,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Oops -- an HTTP status code indicating an issue / error
} else {
if (_objDelegate && [(NSObject *)_objDelegate respondsToSelector:@selector(httpClient:ErrorReceived:)])
- [_objDelegate httpClient:self ErrorReceived:[NSString stringWithFormat:@"Received error status code '%d' from the server.", _intHttpStatusCode]];
+ [_objDelegate httpClient:self ErrorReceived:[NSString stringWithFormat:NSLocalizedString(@"Received error status code '%d' from the server.", nil), _intHttpStatusCode]];
}
}
View
@@ -152,7 +152,7 @@ + (NSString *)encodeBase64WithData:(NSData *)objData {
char * strResult;
// Get the Raw Data length and ensure we actually have data
- int intLength = [objData length];
+ size_t intLength = [objData length];
if (intLength == 0) return nil;
// Setup the String-based Result placeholder and pointer within that placeholder
@@ -184,21 +184,15 @@ + (NSString *)encodeBase64WithData:(NSData *)objData {
*objPointer++ = '=';
}
}
-
- // Terminate the string-based result
- *objPointer = '\0';
-
- // Cleanup
- NSString * strToReturn = [NSString stringWithCString:strResult encoding:NSASCIIStringEncoding];
- free(strResult);
-
- // Return the results as an NSString object
- return [NSString stringWithString:strToReturn];
+
+ NSString *strToReturn = [[NSString alloc] initWithBytesNoCopy:strResult length:objPointer - strResult encoding:NSASCIIStringEncoding freeWhenDone:YES];
+ return [strToReturn autorelease];
}
+ (NSData *)decodeBase64WithString:(NSString *)strBase64 {
const char * objPointer = [strBase64 cStringUsingEncoding:NSASCIIStringEncoding];
- int intLength = strlen(objPointer);
+ if (objPointer == NULL) return nil;
+ size_t intLength = strlen(objPointer);
int intCurrent;
int i = 0, j = 0, k;
@@ -266,9 +260,7 @@ + (NSData *)decodeBase64WithString:(NSString *)strBase64 {
}
// Cleanup and setup the return NSData
- NSData * objData = [[[NSData alloc] initWithBytes:objResult length:j] autorelease];
- free(objResult);
- return objData;
+ return [[[NSData alloc] initWithBytesNoCopy:objResult length:j freeWhenDone:YES] autorelease];
}
@end
View
@@ -31,4 +31,4 @@
#import "QSFileManager.h"
#import "QSHttpClient.h"
-#endif __QSUTILITIES_INCLUDE__
+#endif // __QSUTILITIES_INCLUDE__

0 comments on commit 92908f6

Please sign in to comment.