mxml_ufoai.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "mxml_ufoai.h"
00027 
00034 void mxml_AddString (mxml_node_t *parent, const char *name, const char *value)
00035 {
00036     if (value)
00037         mxmlElementSetAttr(parent, name, value);
00038 }
00039 
00047 void mxml_AddStringValue (mxml_node_t *parent, const char *name, const char *value)
00048 {
00049     if (value && value[0] != '\0')
00050         mxmlElementSetAttr(parent, name, value);
00051 }
00052 
00059 void mxml_AddBool (mxml_node_t *parent, const char *name, qboolean value)
00060 {
00061     mxmlElementSetAttr(parent, name, value ? "true" : "false");
00062 }
00063 
00071 void mxml_AddBoolValue (mxml_node_t *parent, const char *name, qboolean value)
00072 {
00073     if (value)
00074         mxmlElementSetAttr(parent, name, value ? "true" : "false");
00075 }
00076 
00083 void mxml_AddFloat (mxml_node_t *parent, const char *name, float value)
00084 {
00085     mxml_AddDouble(parent, name, value);
00086 }
00087 
00095 void mxml_AddFloatValue (mxml_node_t *parent, const char *name, float value)
00096 {
00097     mxml_AddDoubleValue(parent, name, value);
00098 }
00099 
00106 void mxml_AddDouble (mxml_node_t *parent, const char *name, double value)
00107 {
00108     char txt[50];
00109     snprintf(txt, sizeof(txt), "%f", value);
00110     mxmlElementSetAttr(parent, name, txt);
00111 }
00112 
00120 void mxml_AddDoubleValue (mxml_node_t *parent, const char *name, double value)
00121 {
00122     char txt[50];
00123 
00124     if (value) {
00125         snprintf(txt, sizeof(txt), "%f", value);
00126         mxmlElementSetAttr(parent, name, txt);
00127     }
00128 }
00129 
00136 void mxml_AddByte (mxml_node_t *parent, const char *name, byte value)
00137 {
00138     mxml_AddLong(parent, name, value);
00139 }
00140 
00148 void mxml_AddByteValue (mxml_node_t *parent, const char *name, byte value)
00149 {
00150     mxml_AddLongValue(parent, name, value);
00151 }
00152 
00159 void mxml_AddShort (mxml_node_t *parent, const char *name, short value)
00160 {
00161     mxml_AddLong(parent, name, value);
00162 }
00163 
00171 void mxml_AddShortValue (mxml_node_t *parent, const char *name, short value)
00172 {
00173     mxml_AddLongValue(parent, name, value);
00174 }
00175 
00182 void mxml_AddInt (mxml_node_t *parent, const char *name, int value)
00183 {
00184     mxml_AddLong(parent, name, value);
00185 }
00186 
00194 void mxml_AddIntValue (mxml_node_t *parent, const char *name, int value)
00195 {
00196     mxml_AddLongValue(parent, name, value);
00197 }
00198 
00205 void mxml_AddLong (mxml_node_t *parent, const char *name, long value)
00206 {
00207     char txt[50];
00208     snprintf(txt, sizeof(txt), "%ld", value);
00209     mxmlElementSetAttr(parent, name, txt);
00210 }
00211 
00219 void mxml_AddLongValue (mxml_node_t *parent, const char *name, long value)
00220 {
00221     char txt[50];
00222 
00223     if (value) {
00224         snprintf(txt, sizeof(txt), "%ld", value);
00225         mxmlElementSetAttr(parent, name, txt);
00226     }
00227 }
00228 
00236 void mxml_AddPos3 (mxml_node_t *parent, const char *name, const vec3_t pos)
00237 {
00238     mxml_node_t *t;
00239     t = mxmlNewElement(parent, name);
00240     mxml_AddFloat(t, "x", pos[0]);
00241     mxml_AddFloat(t, "y", pos[1]);
00242     mxml_AddFloat(t, "z", pos[2]);
00243 }
00244 
00245 /*
00246  * @brief add a Pos2 data to the XML Tree
00247  * @param[out] parent XML Node structure to add to
00248  * @param[in] name Name of the node to add
00249  * @param[in] pos Pos2 structure with position data
00250  * @note it creates a new node and adds coordinate(s) to the node as attributes
00251  */
00252 void mxml_AddPos2 (mxml_node_t *parent, const char *name, const vec2_t pos)
00253 {
00254     mxml_node_t *t;
00255     t = mxmlNewElement(parent, name);
00256     mxml_AddFloat(t, "x", pos[0]);
00257     mxml_AddFloat(t, "y", pos[1]);
00258 }
00259 
00260 /*
00261  * @brief add a date data to the XML Tree
00262  * @param[out] parent XML Node structure to add to
00263  * @param[in] name Name of the node to add
00264  * @param[in] day Day part of the date
00265  * @param[in] sec Second part of the date
00266  * @note it creates a new node and adds day/sec attributes to the node
00267  */
00268 void mxml_AddDate (mxml_node_t *parent, const char *name, const int day, const int sec)
00269 {
00270     mxml_node_t *t;
00271     t = mxmlNewElement(parent, name);
00272     mxml_AddInt(t, "day", day);
00273     mxml_AddInt(t, "sec", sec);
00274 }
00275 
00282 mxml_node_t * mxml_AddNode (mxml_node_t *parent, const char *name)
00283 {
00284     return mxmlNewElement(parent,name);
00285 }
00286 
00293 qboolean mxml_GetBool (mxml_node_t *parent, const char *name, const qboolean defaultval)
00294 {
00295     const char *txt;
00296     txt = mxmlElementGetAttr(parent, name);
00297     if (!txt)
00298         return defaultval;
00299 
00300     if (!strcmp(txt, "true"))
00301         return qtrue;
00302     if (!strcmp(txt, "false"))
00303         return qfalse;
00304 
00305     return defaultval;
00306 }
00307 
00314 int mxml_GetInt (mxml_node_t *parent, const char *name, const int defaultval)
00315 {
00316     const char *txt;
00317     txt = mxmlElementGetAttr(parent, name);
00318     if (!txt)
00319         return defaultval;
00320     return atoi(txt);
00321 }
00322 
00329 short mxml_GetShort (mxml_node_t *parent, const char *name, const short defaultval)
00330 {
00331     const char *txt;
00332     txt = mxmlElementGetAttr(parent, name);
00333     if (!txt)
00334         return defaultval;
00335     return atoi(txt);
00336 }
00337 
00344 long mxml_GetLong (mxml_node_t *parent, const char *name, const long defaultval)
00345 {
00346     const char *txt;
00347     txt = mxmlElementGetAttr(parent, name);
00348     if (!txt)
00349         return defaultval;
00350     return atol(txt);
00351 }
00352 
00359 const char * mxml_GetString (mxml_node_t *parent, const char *name)
00360 {
00361     const char *str = mxmlElementGetAttr(parent, name);
00362     if (!str)
00363         return "";
00364     return str;
00365 }
00366 
00373 float mxml_GetFloat (mxml_node_t *parent, const char *name, const float defaultval)
00374 {
00375     const char *txt;
00376     txt = mxmlElementGetAttr(parent, name);
00377     if (!txt)
00378         return defaultval;
00379     return atof(txt);
00380 }
00381 
00388 double mxml_GetDouble (mxml_node_t *parent, const char *name, const double defaultval)
00389 {
00390     const char *txt;
00391     txt = mxmlElementGetAttr(parent, name);
00392     if (!txt)
00393         return defaultval;
00394     return atof(txt);
00395 }
00396 
00405 mxml_node_t * mxml_GetPos2 (mxml_node_t *parent, const char *name, vec2_t pos)
00406 {
00407     mxml_node_t *p = mxml_GetNode(parent, name);
00408     if (!p)
00409         return NULL;
00410     pos[0] = mxml_GetFloat(p, "x", 0);
00411     pos[1] = mxml_GetFloat(p, "y", 0);
00412     return p;
00413 }
00414 
00424 mxml_node_t * mxml_GetNextPos2 (mxml_node_t *actual, mxml_node_t *parent, const char *name, vec2_t pos)
00425 {
00426     mxml_node_t *p = mxml_GetNextNode(actual, parent, name);
00427     if (!p)
00428         return NULL;
00429     pos[0] = mxml_GetFloat(p, "x", 0);
00430     pos[1] = mxml_GetFloat(p, "y", 0);
00431     return p;
00432 }
00433 
00442 mxml_node_t * mxml_GetPos3 (mxml_node_t *parent, const char *name, vec3_t pos)
00443 {
00444     mxml_node_t *p = mxml_GetNode(parent, name);
00445     if (!p)
00446         return NULL;
00447     pos[0] = mxml_GetFloat(p, "x", 0);
00448     pos[1] = mxml_GetFloat(p, "y", 0);
00449     pos[2] = mxml_GetFloat(p, "z", 0);
00450     return p;
00451 }
00452 
00462 mxml_node_t * mxml_GetNextPos3 (mxml_node_t *actual, mxml_node_t *parent, const char *name, vec3_t pos)
00463 {
00464     mxml_node_t *p = mxml_GetNextNode(actual, parent, name);
00465     if (!p)
00466         return NULL;
00467     pos[0] = mxml_GetFloat(p, "x", 0);
00468     pos[1] = mxml_GetFloat(p, "y", 0);
00469     pos[2] = mxml_GetFloat(p, "z", 0);
00470     return p;
00471 }
00472 
00482 mxml_node_t * mxml_GetDate (mxml_node_t *parent, const char *name, int *day, int *sec)
00483 {
00484     mxml_node_t *p = mxml_GetNode(parent, name);
00485     if (!p)
00486         return NULL;
00487     *day = mxml_GetInt(p, "day", 0);
00488     *sec = mxml_GetInt(p, "sec", 0);
00489     return p;
00490 }
00491 
00498 mxml_node_t * mxml_GetNode (mxml_node_t *parent, const char *name)
00499 {
00500     return mxmlFindElement(parent, parent, name, NULL, NULL, MXML_DESCEND_FIRST);
00501 }
00502 
00510 mxml_node_t * mxml_GetNextNode (mxml_node_t *current, mxml_node_t *parent, const char *name)
00511 {
00512     return mxmlFindElement(current, parent, name, NULL, NULL, MXML_NO_DESCEND);
00513 }
00514 
00518 mxml_type_t mxml_ufo_type_cb (mxml_node_t *node)
00519 {
00520     /* You can lookup attributes and/or use the
00521      * element name, hierarchy, etc... */
00522     const char *type = mxmlElementGetAttr(node, "type");
00523     if (type == NULL)
00524         type = node->value.element.name;
00525 
00526     if (!strcmp(type, "int"))
00527         return MXML_INTEGER;
00528     else if (!strcmp(type, "opaque"))
00529         return MXML_OPAQUE;
00530     else if (!strcmp(type, "string"))
00531         return MXML_OPAQUE;
00532     else if (!strcmp(type, "double"))
00533         return MXML_REAL;
00534     else
00535         return MXML_TEXT;
00536 }
00537 

Generated by  doxygen 1.6.2