A bunch more NSString and NSFileManager usage.

It does make for cleaner could, though it would have been nice if NSData
was null terminated :P
This commit is contained in:
Bill Currie 2010-12-26 22:33:04 +09:00
parent 8e13a8327d
commit 97e8ab7f3a
10 changed files with 40 additions and 65 deletions

View file

@ -12,7 +12,7 @@ struct script_s;
struct plitem_s *plist;
}
- (id) initFromFile: (FILE *)fp;
- (id) initFromData: (NSData *)data;
- (int) getValueUnits: (const char *)key;

View file

@ -34,25 +34,19 @@ JDC
return 0;
}
- (id) initFromFile: (FILE *)fp
- (id) initFromData: (NSData *)data
{
dstring_t *text = dstring_newstr ();
char *str;
size_t read;
const size_t readsize = 1024;
size_t len;
[self init];
do {
str = dstring_reservestr (text, readsize);
read = fread (str, 1, readsize, fp);
if (read)
str[read] = 0;
} while (read == readsize);
plist = PL_GetPropertyList (text->str);
dstring_delete (text);
len = [data length];
str = malloc (len + 1);
[data getBytes: str];
str[len] = 0;
plist = PL_GetPropertyList (str);
free (str);
if (!plist)
return 0;
return self;

View file

@ -175,22 +175,20 @@ scanFile
- (void) scanFile: (NSString *)filename
{
int size, line;
char *data;
const char *data;
id cl;
int i;
NSString *path;
QFile *file;
NSData *contents;
NSFileManager *fm = [NSFileManager defaultManager];
path = [source_path stringByAppendingPathComponent: filename];
contents = [fm contentsAtPath: path];
file = Qopen ([path cString], "rt");
if (!file)
if (!contents)
return;
size = Qfilesize (file);
data = malloc (size + 1);
size = Qread (file, data, size);
data[size] = 0;
Qclose (file);
size = [contents length];
data = (const char *) [contents bytes];
line = 1;
for (i = 0; i < size; i++) {
@ -204,8 +202,6 @@ scanFile
line++;
}
}
free (data);
}
/*

View file

@ -21,7 +21,7 @@ extern id map_i;
- (id) writeStats;
- (id) readMapFile: (const char *)fname;
- (id) readMapFile: (NSString *)fname;
- (id) writeMapFile: (const char *)fname useRegion: (BOOL)reg;
- (id) entityConnect: (vec3_t)p1: (vec3_t)p2;

View file

@ -224,7 +224,7 @@ FILE METHODS
readMapFile
=================
*/
- (id) readMapFile: (const char *)fname
- (id) readMapFile: (NSString *)fname
{
char *dat;
const char *wad, *cl;
@ -233,25 +233,25 @@ readMapFile
int i, c;
vec3_t org;
float angle;
QFile *file;
script_t *script;
size_t size;
NSFileManager *fm = [NSFileManager defaultManager];
NSData *contents;
[self saveSelected];
Sys_Printf ("loading %s\n", fname);
NSLog (@"loading %@\n", fname);
file = Qopen (fname, "rt");
if (!file)
contents = [fm contentsAtPath: fname];
if (!contents)
return self;
size = Qfilesize (file);
size = [contents length];
dat = malloc (size + 1);
size = Qread (file, dat, size);
Qclose (file);
[contents getBytes: dat];
dat[size] = 0;
script = Script_New ();
Script_Start (script, fname, dat);
Script_Start (script, [fname cString], dat);
do {
new = [[Entity alloc] initFromScript: script];
@ -291,6 +291,7 @@ readMapFile
break;
}
}
NSLog (@"%@ loaded\n", fname);
return self;
}

View file

@ -65,8 +65,6 @@ extern id project_i;
const char *string_entities; // cmd-line parm
int showDescriptions; // 1 = show map descs in browser
time_t lastModified; // last time project file was modified
}
- (id) initProject;

View file

@ -277,24 +277,20 @@ id project_i;
// Loads and parses a project file
- (id) openProjectFile: (NSString *)path
{
FILE *fp;
struct stat s;
NSFileManager *fm = [NSFileManager defaultManager];
NSData *contents;
Sys_Printf ("openProjectFile: %s\n", [path cString]);
NSLog (@"openProjectFile: %@\n", path);
[path retain];
[path_projectinfo release];
path_projectinfo = path;
projectInfo = NULL;
fp = fopen ([path cString], "r+t");
if (fp == NULL)
contents = [fm contentsAtPath: path];
if (!contents)
return self;
stat ([path cString], &s);
lastModified = s.st_mtime;
projectInfo = [(Dict *)[Dict alloc] initFromFile: fp];
fclose (fp);
projectInfo = [[Dict alloc] initFromData: contents];
return self;
}

View file

@ -455,11 +455,11 @@ App delegate methods
- (id) textCommand: sender
{
char const *t;
NSString *t;
t = [[sender stringValue] cString];
t = [sender stringValue];
if (!strcmp (t, "texname")) {
if (![t isEqualToString: @"texname"]) {
texturedef_t *td;
id b;
@ -788,14 +788,12 @@ Called by open or the project panel
[filename release];
filename = fname;
[map_i readMapFile: [filename cString]];
[map_i readMapFile: filename];
[regionbutton_i setIntValue: 0];
[self setTitleWithRepresentedFilename: fname];
[self updateAll];
Sys_Printf ("%s loaded\n", [fname cString]);
return self;
}

View file

@ -271,14 +271,11 @@ Called when the scaler popup on the window is used
- (id) scaleMenuTarget: sender
{
char const *item;
NSRect rect;
NSPoint mid, org, origin;
float nscale;
item = [[[sender selectedCell] title] cString];
sscanf (item, "%f", &nscale);
nscale /= 100;
nscale = [[[sender selectedCell] title] floatValue] / 100;
if (nscale == scale)
return NULL;
@ -381,11 +378,9 @@ Called when the scaler popup on the window is used
- (id) gridMenuTarget: sender
{
char const *item;
int grid;
item = [[[sender selectedCell] title] cString];
sscanf (item, "grid %d", &grid);
grid = [[[sender selectedCell] title] intValue];
if (grid == gridsize)
return NULL;

View file

@ -153,14 +153,11 @@ Called when the scaler popup on the window is used
*/
- (id) scaleMenuTarget: sender
{
char const *item;
NSRect rect;
NSPoint mid, org, orig;
float nscale;
item = [[sender titleOfSelectedItem] cString];
sscanf (item, "%f", &nscale);
nscale /= 100;
nscale = [[sender titleOfSelectedItem] floatValue] / 100;
if (nscale == scale)
return NULL;