Skip to content

Commit

Permalink
Move basic XML functions out of actor_scripts and into asc.c for more…
Browse files Browse the repository at this point in the history
… general usage
  • Loading branch information
drakos7 committed Apr 11, 2006
1 parent 55e1eff commit 1b3eef0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 69 deletions.
69 changes: 0 additions & 69 deletions actor_scripts.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
#include <time.h>
#include "actors.h"

// Element type and dictionaries for actor definitions
typedef struct {
char *desc;
int index;
} dict_elem;

const dict_elem actor_type_dict[] =
{ { "human female" , human_female },
{ "human male" , human_male },
Expand Down Expand Up @@ -1132,69 +1126,6 @@ void move_self_forward()
}


int find_description_index (const dict_elem dict[], const char *elem, const char *desc) {
int idx = 0;
char *key;

while ((key = dict[idx].desc) != NULL) {
if (strcasecmp (key, elem) == 0)
return dict[idx].index;
idx++;
}

LOG_ERROR("Unknown %s \"%s\"\n", desc, elem);
return -1;
}

void get_string_value (char *buf, size_t maxlen, xmlNode *node) {
if (node->children == NULL)
buf[0] = '\0';
else
my_strncp (buf, node->children->content, maxlen);
}

int get_bool_value (xmlNode *node) {
char *tval;
if (node->children == NULL) return 0;
tval = node->children->content;
return (xmlStrcasecmp (tval, "yes") == 0 || xmlStrcasecmp (tval, "true") == 0 || xmlStrcasecmp (tval, "1") == 0);
}

double get_float_value (xmlNode *node) {
if (node->children == NULL) return 0.0;
return atof (node->children->content);
}

#ifdef NEW_ACTOR_ANIMATION
int get_int_property (xmlNode *node, const char *prop)
{
xmlAttr *attr;

for (attr = node->properties; attr; attr = attr->next)
{
if (attr->type == XML_ATTRIBUTE_NODE && xmlStrcasecmp (attr->name, prop) == 0)
{
return atoi (attr->children->content);
}
}

return -1;
}

#endif
int get_property (xmlNode *node, const char *prop, const char *desc, const dict_elem dict[]) {
xmlAttr *attr;

for (attr = node->properties; attr; attr = attr->next) {
if (attr->type == XML_ATTRIBUTE_NODE && xmlStrcasecmp (attr->name, prop) == 0) {
return find_description_index (dict, attr->children->content, desc);
}
}

LOG_ERROR("Unable to find property %s in node %s\n", prop, node->name);
return -1;
}

int parse_actor_shirt (actor_types *act, xmlNode *cfg) {
xmlNode *item;
int ok, col_idx;
Expand Down
61 changes: 61 additions & 0 deletions asc.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,64 @@ int sane_snprintf (char *str, size_t size, const char *format, ...)
return ret;
}
#endif

int find_description_index (const dict_elem dict[], const char *elem, const char *desc) {
int idx = 0;
char *key;

while ((key = dict[idx].desc) != NULL) {
if (strcasecmp (key, elem) == 0)
return dict[idx].index;
idx++;
}

LOG_ERROR("Unknown %s \"%s\"\n", desc, elem);
return -1;
}

void get_string_value (char *buf, size_t maxlen, xmlNode *node) {
if (node->children == NULL)
buf[0] = '\0';
else
my_strncp (buf, node->children->content, maxlen);
}

int get_bool_value (xmlNode *node) {
Uint8 *tval;
if (node->children == NULL) return 0;
tval = node->children->content;
return (xmlStrcasecmp (tval, (Uint8 *)"yes") == 0 || xmlStrcasecmp (tval, (Uint8 *)"true") == 0 || xmlStrcasecmp (tval, (Uint8 *)"1") == 0);
}

double get_float_value (xmlNode *node) {
if (node->children == NULL) return 0.0;
return atof (node->children->content);
}

int get_int_property (xmlNode *node, const char *prop)
{
xmlAttr *attr;

for (attr = node->properties; attr; attr = attr->next)
{
if (attr->type == XML_ATTRIBUTE_NODE && xmlStrcasecmp (attr->name, (Uint8 *)prop) == 0)
{
return atoi (attr->children->content);
}
}

return -1;
}

int get_property (xmlNode *node, const char *prop, const char *desc, const dict_elem dict[]) {
xmlAttr *attr;

for (attr = node->properties; attr; attr = attr->next) {
if (attr->type == XML_ATTRIBUTE_NODE && xmlStrcasecmp (attr->name, (Uint8 *)prop) == 0) {
return find_description_index (dict, attr->children->content, desc);
}
}

LOG_ERROR("Unable to find property %s in node %s\n", prop, node->name);
return -1;
}
12 changes: 12 additions & 0 deletions asc.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,17 @@ int sane_snprintf (char *buffer, size_t size, const char *format, ...);
void get_file_digest(const Uint8*, Uint8[16]);
void get_string_digest(const Uint8*, Uint8[16]);

// Element type and dictionaries for actor definitions
typedef struct {
char *desc;
int index;
} dict_elem;

int find_description_index (const dict_elem dict[], const char *elem, const char *desc);
void get_string_value (char *buf, size_t maxlen, xmlNode *node);
int get_bool_value (xmlNode *node);
double get_float_value (xmlNode *node);
int get_int_property (xmlNode *node, const char *prop);
int get_property (xmlNode *node, const char *prop, const char *desc, const dict_elem dict[]);

#endif

0 comments on commit 1b3eef0

Please sign in to comment.