2010-09-28 12:40:11 +00:00
|
|
|
#include "QF/dstring.h"
|
2010-10-04 10:43:33 +00:00
|
|
|
#include "QF/qfplist.h"
|
2010-10-02 12:00:15 +00:00
|
|
|
#include "QF/script.h"
|
2010-10-04 10:43:33 +00:00
|
|
|
#include "QF/sys.h"
|
2010-09-28 12:40:11 +00:00
|
|
|
#include "QF/va.h"
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-11 08:35:25 +00:00
|
|
|
#include "Dict.h"
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
@implementation Dict
|
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
/*
|
2010-09-29 20:09:11 +00:00
|
|
|
- (id) print
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-29 20:09:11 +00:00
|
|
|
NSUInteger i;
|
|
|
|
dict_t *d;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < numElements; i++) {
|
2010-09-29 20:09:11 +00:00
|
|
|
d = [self elementAt: i];
|
2010-09-11 10:03:41 +00:00
|
|
|
printf ("%s : %s\n", d->key, d->value);
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
2010-10-04 10:43:33 +00:00
|
|
|
*/
|
2003-03-18 19:48:24 +00:00
|
|
|
/*
|
|
|
|
===========
|
|
|
|
copyFromZone
|
|
|
|
|
|
|
|
JDC
|
|
|
|
===========
|
|
|
|
*/
|
2010-09-29 20:09:11 +00:00
|
|
|
- (id) copy
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-04 10:43:33 +00:00
|
|
|
Sys_Printf ("Dict copy: not implemented\n");
|
|
|
|
return 0;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
- (id) initFromFile: (FILE *)fp
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-02 12:00:15 +00:00
|
|
|
dstring_t *text = dstring_newstr ();
|
|
|
|
char *str;
|
|
|
|
size_t read;
|
|
|
|
const size_t readsize = 1024;
|
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
[self init];
|
2010-10-02 12:00:15 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
str = dstring_reservestr (text, readsize);
|
|
|
|
read = fread (str, 1, readsize, fp);
|
|
|
|
if (read)
|
|
|
|
str[read] = 0;
|
|
|
|
} while (read == readsize);
|
|
|
|
|
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
plist = PL_GetPropertyList (text->str);
|
|
|
|
dstring_delete (text);
|
|
|
|
if (!plist)
|
|
|
|
return 0;
|
2010-10-02 12:00:15 +00:00
|
|
|
return self;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (plist)
|
|
|
|
PL_Free (plist);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// ===============================================
|
2003-03-18 19:48:24 +00:00
|
|
|
//
|
2010-09-11 10:03:41 +00:00
|
|
|
// Dictionary pair functions
|
2003-03-18 19:48:24 +00:00
|
|
|
//
|
2010-09-29 20:09:11 +00:00
|
|
|
// ===============================================
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
- (id) writeBlockTo: (FILE *)fp
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-04 10:43:33 +00:00
|
|
|
char *data;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
data = PL_WritePropertyList (plist);
|
|
|
|
fputs (data, fp);
|
|
|
|
free (data);
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
- (id) writeFile: (const char *)path
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-29 20:09:11 +00:00
|
|
|
FILE *fp;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
fp = fopen (path, "w+t");
|
|
|
|
if (fp != NULL) {
|
|
|
|
printf ("Writing dictionary file %s.\n", path);
|
|
|
|
fprintf (fp, "// QE_Project file %s\n", path);
|
2010-09-29 20:09:11 +00:00
|
|
|
[self writeBlockTo: fp];
|
2010-09-11 10:03:41 +00:00
|
|
|
fclose (fp);
|
|
|
|
} else {
|
|
|
|
printf ("Error writing %s!\n", path);
|
2003-03-18 19:48:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// ===============================================
|
2003-03-18 19:48:24 +00:00
|
|
|
//
|
2010-09-11 10:03:41 +00:00
|
|
|
// Utility methods
|
2003-03-18 19:48:24 +00:00
|
|
|
//
|
2010-09-29 20:09:11 +00:00
|
|
|
// ===============================================
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
// Change a keyword's string
|
2010-09-29 20:09:11 +00:00
|
|
|
- (id) changeStringFor: (const char *)key to: (const char *)value
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-04 10:43:33 +00:00
|
|
|
PL_D_AddObject (plist, key, PL_NewString (value));
|
2003-03-18 19:48:24 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
- (plitem_t *) getArrayFor: (const char *)name
|
|
|
|
{
|
|
|
|
plitem_t *item;
|
|
|
|
item = PL_ObjectForKey (plist, name);
|
|
|
|
if (item && PL_Type (item) == QFArray)
|
|
|
|
return item;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
// Search for keyword, return the string *
|
2010-09-29 20:09:11 +00:00
|
|
|
- (const char *) getStringFor: (const char *)name
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-04 10:43:33 +00:00
|
|
|
plitem_t *item;
|
|
|
|
const char *str;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
item = PL_ObjectForKey (plist, name);
|
|
|
|
if (item && (str = PL_String (item)))
|
|
|
|
return str;
|
|
|
|
return "";
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
// Search for keyword, return the value
|
2010-09-29 20:09:11 +00:00
|
|
|
- (unsigned int) getValueFor: (const char *)name
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-04 10:43:33 +00:00
|
|
|
return atol ([self getStringFor: name]);
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
// Return # of units in keyword's value
|
2010-09-29 20:09:11 +00:00
|
|
|
- (int) getValueUnits: (const char *)key
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-10-04 10:43:33 +00:00
|
|
|
plitem_t *item;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
item = PL_ObjectForKey (plist, key);
|
|
|
|
if (!item || PL_Type (item) != QFArray)
|
|
|
|
return 0;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-10-04 10:43:33 +00:00
|
|
|
return PL_A_NumObjects (item);
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|