00001 #include <libxml/encoding.h>
00002 #include <libxml/xmlreader.h>
00003 #include <libxml/xmlwriter.h>
00004 #include <string.h>
00005 #include <stdarg.h>
00006 #include "vapiOptions.h"
00007 #include "vapiOptionsXML.h"
00008 #include "vapiString.h"
00009 #include "vapiXML.h"
00010 #include "vapiAux.h"
00011
00012
00013
00014
00015 #include "vapiOperation.h"
00016
00017 static int
00018 vapiOptionsXMLSetInts (xmlTextReader * xmlInput, vOptions * Options)
00019 {
00020 int countLine = 0, ret;
00021
00022 for (;;)
00023 {
00024
00025 if (strcmp ((char *) xmlTextReaderGetAttribute
00026 (xmlInput, BAD_CAST "type"), "ints") != 0)
00027 {
00028 return -1;
00029 }
00030
00031 xmlTextReaderRead (xmlInput);
00032 Options->ints[countLine] =
00033 atoi ((char *) xmlTextReaderValue (xmlInput));
00034 v_debug ("countline: %d, int: %d, %s", countLine,
00035 Options->ints[countLine],
00036 xmlTextReaderValue (xmlInput));
00037
00038 if (!(ret = vapiXMLNextElementNamed (xmlInput, "option")) > 0)
00039 {
00040 break;
00041 }
00042 ++countLine;
00043 }
00044 return ret;
00045
00046 }
00047
00048 static int
00049 vapiOptionsXMLSetFloats (xmlTextReader * xmlInput, vOptions * Options)
00050 {
00051
00052 int countLine = 0, ret;
00053
00054 for (;;)
00055 {
00056
00057 if (strcmp ((char *) xmlTextReaderGetAttribute
00058 (xmlInput, BAD_CAST "type"), "floats") != 0)
00059 {
00060 return -1;
00061 }
00062
00063 xmlTextReaderRead (xmlInput);
00064 #ifndef VAPI_OS_LINUX
00065 Options->floats[countLine] =
00066 atof (vapiComaToPoint
00067 ((char *) xmlTextReaderValue (xmlInput)));
00068 #else
00069 Options->floats[countLine] =
00070 atof ((char *) xmlTextReaderValue (xmlInput));
00071 #endif
00072
00073 v_debug ("countline: %d, float: %1.2f, %s", countLine,
00074 Options->floats[countLine], (char *)
00075 xmlTextReaderValue (xmlInput));
00076
00077 if (!(ret = vapiXMLNextElementNamed (xmlInput, "option")) > 0)
00078 {
00079 break;
00080 }
00081 ++countLine;
00082 }
00083 return ret;
00084
00085
00086 }
00087
00088 static int
00089 vapiOptionsXMLSetChars (xmlTextReader * xmlInput, vOptions * Options)
00090 {
00091 int countLine = 0, ret;
00092
00093 for (;;)
00094 {
00095
00096 if (strcmp ((char *) xmlTextReaderGetAttribute
00097 (xmlInput, BAD_CAST "type"), "text") != 0)
00098 {
00099 return -1;
00100 }
00101
00102 xmlTextReaderRead (xmlInput);
00103 Options->chars[countLine].ch =
00104 vapiReturnStringPointer ((char *) xmlTextReaderValue
00105 (xmlInput));
00106 v_debug ("countline: %d, chars: %s, %s", countLine,
00107 Options->chars[countLine].ch,
00108 xmlTextReaderValue (xmlInput));
00109
00110 if (!(ret = vapiXMLNextElementNamed (xmlInput, "option")) > 0)
00111 {
00112 break;
00113 }
00114 ++countLine;
00115 }
00116 return ret;
00117 }
00118
00119 static int
00120 vapiOptionsXMLSetChoices (xmlTextReader * xmlInput, vOptions * Options)
00121 {
00122 int countLine = 0, ret;
00123
00124 for (;;)
00125 {
00126 if (strcmp ((char *) xmlTextReaderGetAttribute
00127 (xmlInput, BAD_CAST "type"), "choices") != 0)
00128 {
00129 return -1;
00130 }
00131
00132 xmlTextReaderRead (xmlInput);
00133 Options->choices[countLine] =
00134 atoi ((char *) xmlTextReaderValue (xmlInput));
00135 v_debug ("countline: %d, choice: %d, %s", countLine,
00136 Options->choices[countLine],
00137 xmlTextReaderValue (xmlInput));
00138
00139 if (!(ret = vapiXMLNextElementNamed (xmlInput, "option")) > 0)
00140 {
00141 break;
00142 }
00143 ++countLine;
00144 }
00145 return ret;
00146 }
00147
00148 static int
00149 vapiOptionsXMLSetBooleans (xmlTextReader * xmlInput, vOptions * Options)
00150 {
00151 int countLine = 0, ret;
00152
00153 for (;;)
00154 {
00155
00156 if (strcmp ((char *) xmlTextReaderGetAttribute
00157 (xmlInput, BAD_CAST "type"), "booleans") != 0)
00158 {
00159 return -1;
00160 }
00161
00162 xmlTextReaderRead (xmlInput);
00163 if (!strcmp ((char *) xmlTextReaderValue (xmlInput), "vTRUE"))
00164 {
00165 Options->booleans[countLine] = vTRUE;
00166 }
00167 if (!strcmp
00168 ((char *) xmlTextReaderValue (xmlInput), "vFALSE"))
00169 {
00170 Options->booleans[countLine] = vFALSE;
00171 }
00172
00173 v_debug ("countline-1: %d, int: %d, %s", countLine,
00174 Options->booleans[countLine],
00175 xmlTextReaderValue (xmlInput));
00176
00177 if (!(ret = vapiXMLNextElementNamed (xmlInput, "option")) > 0)
00178 {
00179 break;
00180 }
00181 ++countLine;
00182 }
00183 return ret;
00184
00185 }
00186
00187 int
00188 vapiOptionsXMLCheckOption (xmlTextReader * xmlInput, int ret)
00189 {
00190 char *nodeType;
00191 if ((nodeType =
00192 (char *) xmlTextReaderGetAttribute (xmlInput,
00193 BAD_CAST "type")) == NULL)
00194 {
00195 if (ret == 0)
00196 {
00197 return -1;
00198 }
00199 return 0;
00200 }
00201 return 1;
00202
00203 }
00204
00205 void
00206 vapiOptionsXMLSetOptions (xmlTextReader * xmlInput, vOptions * Options,
00207 vOptionsXMLSetOptionsCustom
00208 OptionsXMLSetOptionsCustom)
00209 {
00210 int ret = 1, bk = 1;
00211
00212
00213 if (xmlTextReaderIsEmptyElement (xmlInput))
00214 {
00215 return;
00216
00217 }
00218 ret = vapiXMLNextElementNamed (xmlInput, "option");
00219
00220 while (bk != -1 && ret != 0)
00221 {
00222
00223 if ((bk = vapiOptionsXMLCheckOption (xmlInput, ret)) == 0)
00224 {
00225 continue;
00226 }
00227 if (bk == -1)
00228 {
00229 break;
00230 }
00231 if (OptionsXMLSetOptionsCustom != NULL)
00232 {
00233 bk = OptionsXMLSetOptionsCustom (xmlInput, Options);
00234
00235 if (bk == 0)
00236 {
00237
00238 continue;
00239 }
00240 if (bk == -1)
00241 {
00242 break;
00243 }
00244 }
00245
00246 if (strcmp
00247 ((char *)
00248 xmlTextReaderGetAttribute (xmlInput, BAD_CAST "type"),
00249 "ints") == 0)
00250 {
00251 ret = vapiOptionsXMLSetInts (xmlInput, Options);
00252 }
00253
00254 if ((bk = vapiOptionsXMLCheckOption (xmlInput, ret)) == 0)
00255 {
00256
00257 continue;
00258 }
00259 if (bk == -1)
00260 {
00261 break;
00262 }
00263
00264 if (strcmp
00265 ((char *)
00266 xmlTextReaderGetAttribute (xmlInput, BAD_CAST "type"),
00267 "text") == 0)
00268 {
00269 ret = vapiOptionsXMLSetChars (xmlInput, Options);
00270 }
00271 if ((bk = vapiOptionsXMLCheckOption (xmlInput, ret)) == 0)
00272 {
00273
00274 continue;
00275 }
00276 if (bk == -1)
00277 {
00278 break;
00279 }
00280 if (strcmp
00281 ((char *)
00282 xmlTextReaderGetAttribute (xmlInput, BAD_CAST "type"),
00283 "floats") == 0)
00284 {
00285 ret = vapiOptionsXMLSetFloats (xmlInput, Options);
00286 }
00287 if ((bk = vapiOptionsXMLCheckOption (xmlInput, ret)) == 0)
00288 {
00289
00290 continue;
00291 }
00292 if (bk == -1)
00293 {
00294 break;
00295 }
00296 if (strcmp
00297 ((char *)
00298 xmlTextReaderGetAttribute (xmlInput, BAD_CAST "type"),
00299 "choices") == 0)
00300 {
00301 ret = vapiOptionsXMLSetChoices (xmlInput, Options);
00302 }
00303 if ((bk = vapiOptionsXMLCheckOption (xmlInput, ret)) == 0)
00304 {
00305
00306 continue;
00307 }
00308 if (bk == -1)
00309 {
00310 break;
00311 }
00312 if (strcmp
00313 ((char *)
00314 xmlTextReaderGetAttribute (xmlInput, BAD_CAST "type"),
00315 "booleans") == 0)
00316 {
00317 ret = vapiOptionsXMLSetBooleans (xmlInput, Options);
00318 }
00319
00320 }
00321 }
00322
00323
00324 static void
00325 vapiOptionsXMLGetFloats (vOptionsSettings * OptionsSettings,
00326 vOptions * Options, xmlTextWriter * xmlOutput)
00327 {
00328
00329 int i;
00330
00331 if (OptionsSettings->floatsNumber)
00332 {
00333 for (i = 0; i < OptionsSettings->floatsNumber; i++)
00334 {
00335 xmlTextWriterStartElement (xmlOutput,
00336 BAD_CAST "option");
00337 xmlTextWriterWriteAttribute (xmlOutput,
00338 BAD_CAST "type",
00339 BAD_CAST "floats");
00340 xmlTextWriterWriteFormatString (xmlOutput,
00341 "%1.2f",
00342 Options->floats[i]);
00343 xmlTextWriterEndElement (xmlOutput);
00344 }
00345
00346 }
00347
00348 }
00349
00350 static void
00351 vapiOptionsXMLGetInts (vOptionsSettings * OptionsSettings,
00352 vOptions * Options, xmlTextWriter * xmlOutput)
00353 {
00354
00355 int i;
00356
00357 if (OptionsSettings->intsNumber)
00358 {
00359 for (i = 0; i < OptionsSettings->intsNumber; i++)
00360 {
00361 xmlTextWriterStartElement (xmlOutput,
00362 BAD_CAST "option");
00363 xmlTextWriterWriteAttribute (xmlOutput,
00364 BAD_CAST "type",
00365 BAD_CAST "ints");
00366 xmlTextWriterWriteFormatString (xmlOutput,
00367 "%d",
00368 Options->ints[i]);
00369 xmlTextWriterEndElement (xmlOutput);
00370 }
00371
00372 }
00373
00374 }
00375
00376 static void
00377 vapiOptionsXMLGetChoices (vOptionsSettings * OptionsSettings,
00378 vOptions * Options, xmlTextWriter * xmlOutput)
00379 {
00380
00381 int i;
00382
00383 if (OptionsSettings->chooseNumber)
00384 {
00385 for (i = 0; i < OptionsSettings->chooseNumber; i++)
00386 {
00387 xmlTextWriterStartElement (xmlOutput,
00388 BAD_CAST "option");
00389 xmlTextWriterWriteAttribute (xmlOutput,
00390 BAD_CAST "type",
00391 BAD_CAST "choices");
00392 xmlTextWriterWriteFormatString (xmlOutput,
00393 "%d",
00394 Options->choices[i]);
00395 xmlTextWriterEndElement (xmlOutput);
00396 }
00397
00398 }
00399
00400 }
00401
00402 static void
00403 vapiOptionsXMLGetChars (vOptionsSettings * OptionsSettings,
00404 vOptions * Options, xmlTextWriter * xmlOutput)
00405 {
00406
00407 int i;
00408
00409 if (OptionsSettings->charsNumber)
00410 {
00411 for (i = 0; i < OptionsSettings->charsNumber; i++)
00412 {
00413 xmlTextWriterStartElement (xmlOutput,
00414 BAD_CAST "option");
00415 xmlTextWriterWriteAttribute (xmlOutput,
00416 BAD_CAST "type",
00417 BAD_CAST "text");
00418 v_debug ("XML CHAR: %s", Options->chars[i].ch);
00419
00420 if (strlen (Options->chars[i].ch) > 0)
00421 {
00422
00423 xmlTextWriterWriteFormatString (xmlOutput,
00424 "%s",
00425 Options->
00426 chars[i].ch);
00427 }
00428
00429 xmlTextWriterEndElement (xmlOutput);
00430 }
00431
00432 }
00433
00434 }
00435
00436 static void
00437 vapiOptionsXMLGetBooleans (vOptionsSettings * OptionsSettings,
00438 vOptions * Options, xmlTextWriter * xmlOutput)
00439 {
00440
00441 int i;
00442
00443 if (OptionsSettings->booleansNumber)
00444 {
00445 for (i = 0; i < OptionsSettings->booleansNumber; i++)
00446 {
00447 xmlTextWriterStartElement (xmlOutput,
00448 BAD_CAST "option");
00449 xmlTextWriterWriteAttribute (xmlOutput,
00450 BAD_CAST "type",
00451 BAD_CAST "booleans");
00452 if (Options->booleans[i])
00453 {
00454 xmlTextWriterWriteFormatString (xmlOutput,
00455 "%s",
00456 "vTRUE");
00457 }
00458 else
00459 {
00460 xmlTextWriterWriteFormatString (xmlOutput,
00461 "%s",
00462 "vFALSE");
00463 }
00464 xmlTextWriterEndElement (xmlOutput);
00465 }
00466
00467 }
00468
00469 }
00470
00471 void
00472 vapiOptionsXMLGetOptions (xmlTextWriter * xmlOutput,
00473 vOptionsSettings * OptionsSettings,
00474 vOptions * Options,
00475 vOptionsXMLGetOptionsCustom OptionsXMLGetOptions)
00476 {
00477
00478
00479 if (OptionsXMLGetOptions != NULL)
00480 {
00481 OptionsXMLGetOptions (xmlOutput, OptionsSettings, Options);
00482 }
00483
00484 vapiOptionsXMLGetFloats (OptionsSettings, Options, xmlOutput);
00485
00486 vapiOptionsXMLGetInts (OptionsSettings, Options, xmlOutput);
00487
00488 vapiOptionsXMLGetChars (OptionsSettings, Options, xmlOutput);
00489
00490 vapiOptionsXMLGetChoices (OptionsSettings, Options, xmlOutput);
00491
00492 vapiOptionsXMLGetBooleans (OptionsSettings, Options, xmlOutput);
00493
00494 }
00495
00496 void
00497 vapiOptionsXMLOptionsElement (xmlTextWriter * xmlOutput,
00498 int NumberOfCustomOptions, ...)
00499 {
00500 va_list CustomOptions;
00501 char *DTDAttlistStart, *DTDAttlistEnd, *TempString, *TempString2,
00502 *FinalString;
00503 int i;
00504 DTDAttlistStart = "type (ints | text | floats | choices | booleans";
00505 DTDAttlistEnd = ") #REQUIRED";
00506 TempString = vapiReturnStringPointer ("");
00507 va_start (CustomOptions, NumberOfCustomOptions);
00508 if (NumberOfCustomOptions > 0)
00509 {
00510 TempString =
00511 v_printf (" | %s", va_arg (CustomOptions, char *));
00512
00513 for (i = 1; i < NumberOfCustomOptions; i++)
00514 {
00515 TempString2 =
00516 v_printf ("%s | %s", TempString,
00517 va_arg (CustomOptions, char *));
00518 free (TempString);
00519 TempString = TempString2;
00520 }
00521 FinalString =
00522 v_printf ("%s %s %s",
00523 DTDAttlistStart, TempString, DTDAttlistEnd);
00524 free (TempString);
00525 }
00526 else
00527 {
00528 FinalString =
00529 v_printf ("%s %s", DTDAttlistStart, DTDAttlistEnd);
00530 }
00531 va_end (CustomOptions);
00532
00533 xmlTextWriterWriteDTDElement (xmlOutput, BAD_CAST "option",
00534 BAD_CAST "(#PCDATA)");
00535 xmlTextWriterWriteDTDAttlist (xmlOutput, BAD_CAST "option",
00536 BAD_CAST FinalString);
00537 free (FinalString);
00538 }