~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to src/base/cs_gui_util.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*============================================================================
2
 
 *
3
 
 *     This file is part of the Code_Saturne Kernel, element of the
4
 
 *     Code_Saturne CFD tool.
5
 
 *
6
 
 *     Copyright (C) 1998-2009 EDF S.A., France
7
 
 *
8
 
 *     contact: saturne-support@edf.fr
9
 
 *
10
 
 *     The Code_Saturne Kernel is free software; you can redistribute it
11
 
 *     and/or modify it under the terms of the GNU General Public License
12
 
 *     as published by the Free Software Foundation; either version 2 of
13
 
 *     the License, or (at your option) any later version.
14
 
 *
15
 
 *     The Code_Saturne Kernel is distributed in the hope that it will be
16
 
 *     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17
 
 *     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 *     GNU General Public License for more details.
19
 
 *
20
 
 *     You should have received a copy of the GNU General Public License
21
 
 *     along with the Code_Saturne Kernel; if not, write to the
22
 
 *     Free Software Foundation, Inc.,
23
 
 *     51 Franklin St, Fifth Floor,
24
 
 *     Boston, MA  02110-1301  USA
25
 
 *
26
 
 *============================================================================*/
27
 
 
28
 
/*============================================================================
29
 
 * Management of the GUI parameters file: xpath request and utilities
30
 
 *============================================================================*/
31
 
 
32
 
#if defined(HAVE_CONFIG_H)
33
 
#include "cs_config.h"
34
 
#endif
35
 
 
36
 
/*----------------------------------------------------------------------------
37
 
 * Standard C library headers
38
 
 *----------------------------------------------------------------------------*/
39
 
 
40
 
#include <stdio.h>
41
 
#include <stdlib.h>
42
 
#include <math.h>
43
 
#include <string.h>
44
 
#include <stdarg.h>
45
 
#include <fcntl.h>
46
 
#include <unistd.h>
47
 
#include <assert.h>
48
 
 
49
 
/*----------------------------------------------------------------------------
50
 
 * libxml2 library headers
51
 
 *----------------------------------------------------------------------------*/
52
 
 
53
 
#if defined(HAVE_LIBXML2)
54
 
 
55
 
#include <libxml/tree.h>
56
 
#include <libxml/parser.h>
57
 
#include <libxml/xpath.h>
58
 
#include <libxml/xpathInternals.h>
59
 
 
60
 
#endif
61
 
 
62
 
/*----------------------------------------------------------------------------
63
 
 * BFT library headers
64
 
 *----------------------------------------------------------------------------*/
65
 
 
66
 
#include <bft_mem.h>
67
 
#include <bft_error.h>
68
 
#include <bft_printf.h>
69
 
 
70
 
/*----------------------------------------------------------------------------
71
 
 * Local headers
72
 
 *----------------------------------------------------------------------------*/
73
 
 
74
 
#include "cs_base.h"
75
 
 
76
 
/*----------------------------------------------------------------------------
77
 
 * Header for the current file
78
 
 *----------------------------------------------------------------------------*/
79
 
 
80
 
#include "cs_gui_util.h"
81
 
 
82
 
 
83
 
/*----------------------------------------------------------------------------*/
84
 
 
85
 
BEGIN_C_DECLS
86
 
 
87
 
/*=============================================================================
88
 
 * Local Macro Definitions
89
 
 *============================================================================*/
90
 
 
91
 
#define XML_READER_VERSION 2.0
92
 
 
93
 
/*=============================================================================
94
 
 * Global variables
95
 
 *============================================================================*/
96
 
 
97
 
#if defined(HAVE_LIBXML2)
98
 
xmlDocPtr docxml            = NULL;   /* Pointer on the XML document  */
99
 
xmlXPathContextPtr xpathCtx = NULL;   /* Pointer on the XPath Context */
100
 
xmlNodePtr node             = NULL;   /* Pointer on the root node     */
101
 
const char *xmlRootName     = NULL;   /* Name of the root node        */
102
 
#endif
103
 
 
104
 
/*============================================================================
105
 
 * Public Fortran function definitions
106
 
 *============================================================================*/
107
 
 
108
 
/*-----------------------------------------------------------------------------
109
 
 * Return the information if the requested xml file is missing or not.
110
 
 *
111
 
 * Fortran Interface:
112
 
 *
113
 
 * SUBROUTINE CSIHMP (IIHMPR)
114
 
 * *****************
115
 
 *
116
 
 * INTEGER          IIHMPR   <--   1 if the file exists, 0 otherwise
117
 
 *----------------------------------------------------------------------------*/
118
 
 
119
 
void CS_PROCF (csihmp, CSIHMP) (int *const iihmpr)
120
 
{
121
 
#if defined(HAVE_LIBXML2)
122
 
  if (docxml == NULL)
123
 
    *iihmpr = 0;
124
 
  else
125
 
    *iihmpr = 1;
126
 
#else
127
 
  *iihmpr = 0;
128
 
#endif
129
 
}
130
 
 
131
 
/*============================================================================
132
 
 * Public function definitions
133
 
 *============================================================================*/
134
 
 
135
 
/*----------------------------------------------------------------------------
136
 
 * Load the xml file in memory. Return an error code for the main program.
137
 
 *
138
 
 * parameter:
139
 
 *   filename            -->  xml file containing the parameters
140
 
 *----------------------------------------------------------------------------*/
141
 
 
142
 
int
143
 
cs_gui_file_loading(const char *const filename)
144
 
{
145
 
#if defined(HAVE_LIBXML2)
146
 
 
147
 
  int file_descriptor = 0;
148
 
  int argerr = 0;
149
 
 
150
 
  assert(filename);
151
 
 
152
 
  /* Verification of the existence of the file while opening it */
153
 
 
154
 
  file_descriptor = open(filename, O_RDONLY);
155
 
 
156
 
  if (file_descriptor ==  -1) {
157
 
 
158
 
    cs_base_warn(__FILE__, __LINE__);
159
 
    bft_printf( _("Unable to open the file: %s\n"), filename);
160
 
    argerr = 2;
161
 
    return argerr;
162
 
 
163
 
  }
164
 
  else {
165
 
 
166
 
    /* If file exists, close it. It will be reopen by xmlParseFile */
167
 
    close(file_descriptor);
168
 
 
169
 
  }
170
 
 
171
 
  /* libxml initialization */
172
 
  xmlInitParser();
173
 
  LIBXML_TEST_VERSION
174
 
 
175
 
  /* Loading the xml file */
176
 
  docxml = xmlParseFile(filename);
177
 
 
178
 
 
179
 
  if (docxml == NULL) {
180
 
 
181
 
    cs_base_warn(__FILE__, __LINE__);
182
 
    bft_printf (_("Unable to parse the file: %s\n"), filename);
183
 
    argerr = 2;
184
 
 
185
 
  }
186
 
  else {
187
 
 
188
 
    /* Contexte definition */
189
 
    xpathCtx = xmlXPathNewContext(docxml);
190
 
 
191
 
    /* Get the root node of the xml document and more particularly
192
 
       of its label */
193
 
    node = xmlDocGetRootElement(docxml);
194
 
    xmlRootName = (const char*) node->name;
195
 
 
196
 
  }
197
 
 
198
 
  /* Check the Interface version */
199
 
  cs_gui_get_version();
200
 
 
201
 
  return argerr;
202
 
 
203
 
#else
204
 
 
205
 
  bft_error(__FILE__, __LINE__, 0,
206
 
            _("Code_Saturne has been compiled without Xml support."));
207
 
 
208
 
  return -1;
209
 
 
210
 
#endif
211
 
}
212
 
 
213
 
/*-----------------------------------------------------------------------------
214
 
 * Check the xml file version.
215
 
 *----------------------------------------------------------------------------*/
216
 
 
217
 
void
218
 
cs_gui_get_version(void)
219
 
{
220
 
  char *path;
221
 
  char *version;
222
 
  double version_number;
223
 
  double version_sat = XML_READER_VERSION;
224
 
  double major, minus;
225
 
  double maj_sat, min_sat;
226
 
 
227
 
  path = cs_xpath_init_path();
228
 
  cs_xpath_add_attribute(&path, "version");
229
 
 
230
 
  version = cs_gui_get_attribute_value(path);
231
 
  version_number = atof(version);
232
 
 
233
 
  minus = modf(version_number, &major);
234
 
  min_sat = modf(version_sat, &maj_sat);
235
 
 
236
 
  if (major != maj_sat)
237
 
     bft_error(__FILE__, __LINE__, 0,
238
 
               _("========================================================\n"
239
 
                 "   ** INVALID VERSION OF THE XML FILE\n"
240
 
                 "      -------------------------------------- \n"
241
 
                 "      XML FILE VERSION: %.1f  \n"
242
 
                 "      XML READER VERSION: %.1f \n"
243
 
                 "========================================================\n"),
244
 
                  version_number, version_sat);
245
 
 
246
 
  if (minus != min_sat) {
247
 
 
248
 
     cs_base_warn(__FILE__, __LINE__);
249
 
     bft_printf(_("========================================================\n"
250
 
                 "   ** INCOMPATIBLE VERSION OF THE XML FILE\n"
251
 
                 "      -------------------------------------- \n"
252
 
                 "      XML FILE VERSION: %.1f  \n"
253
 
                 "      XML READER VERSION: %.1f \n"
254
 
                 "\n"
255
 
                 "      YOU SHOULD RESTART YOUR CALCUL WITH A NEW XML FILE\n"
256
 
                 "========================================================\n"),
257
 
                 version_number, version_sat);
258
 
 
259
 
  }
260
 
 
261
 
  BFT_FREE(version);
262
 
  BFT_FREE(path);
263
 
}
264
 
 
265
 
/*----------------------------------------------------------------------------
266
 
 * Initialize the path for the xpath request with the root node.
267
 
 * Return the root path.
268
 
 *----------------------------------------------------------------------------*/
269
 
 
270
 
char*
271
 
cs_xpath_init_path(void)
272
 
{
273
 
#if defined(HAVE_LIBXML2)
274
 
 
275
 
  char *path = NULL;
276
 
 
277
 
  BFT_MALLOC(path, strlen("/") + strlen(xmlRootName) + 1, char);
278
 
  strcpy(path, "/");
279
 
  strcat(path, xmlRootName);
280
 
 
281
 
  return path;
282
 
 
283
 
#else
284
 
 
285
 
  bft_error(__FILE__, __LINE__, 0,
286
 
            _("Code_Saturne has been compiled without Xml support."));
287
 
 
288
 
  return NULL;
289
 
 
290
 
#endif
291
 
}
292
 
 
293
 
/*----------------------------------------------------------------------------
294
 
 * Initialize the path for the xpath request with a short way.
295
 
 * Return the short path.
296
 
 *----------------------------------------------------------------------------*/
297
 
 
298
 
char*
299
 
cs_xpath_short_path(void)
300
 
{
301
 
  char *path = NULL;
302
 
 
303
 
  BFT_MALLOC(path, strlen("/")+1, char);
304
 
  strcpy(path, "/");
305
 
 
306
 
  return path;
307
 
}
308
 
 
309
 
/*----------------------------------------------------------------------------
310
 
 * Add all element (*) to the path.
311
 
 *
312
 
 * parameter:
313
 
 *   path               <-->  path for the xpath request
314
 
 *----------------------------------------------------------------------------*/
315
 
 
316
 
void
317
 
cs_xpath_add_all_elements(char **path)
318
 
{
319
 
  assert(path);
320
 
 
321
 
  BFT_REALLOC(*path,
322
 
              strlen(*path) +strlen("/*") +1,
323
 
              char);
324
 
 
325
 
  strcat(*path, "/*");
326
 
}
327
 
 
328
 
/*----------------------------------------------------------------------------
329
 
 * Add an element (i.e. markup's label) to the path.
330
 
 *
331
 
 * parameters:
332
 
 *   path               <-->  path for the xpath request
333
 
 *   element             -->  label of the new element in the path
334
 
 *----------------------------------------------------------------------------*/
335
 
 
336
 
void
337
 
cs_xpath_add_element(      char **      path,
338
 
                     const char  *const element)
339
 
{
340
 
  assert(path);
341
 
 
342
 
  if (element != NULL) {
343
 
 
344
 
    BFT_REALLOC(*path,
345
 
                strlen(*path)+ strlen(element)+ strlen("/") +1,
346
 
                char);
347
 
 
348
 
    strcat(*path, "/");
349
 
    strcat(*path, element);
350
 
 
351
 
  }
352
 
}
353
 
 
354
 
/*----------------------------------------------------------------------------
355
 
 * Add a list of elements (i.e. markup's label) to the path.
356
 
 *
357
 
 * parameters:
358
 
 *   path               <-->  path for the xpath request
359
 
 *   nbr                 -->  size of the labels list
360
 
 *   ...                 -->  list of labels of new elements in the path
361
 
 *----------------------------------------------------------------------------*/
362
 
 
363
 
void
364
 
cs_xpath_add_elements(      char **path,
365
 
                      const int    nbr, ...)
366
 
{
367
 
  va_list list;
368
 
  char *elt = NULL;
369
 
  int i;
370
 
 
371
 
  assert(path);
372
 
 
373
 
  va_start(list, nbr);
374
 
 
375
 
  for(i=0; i<nbr; i++) {
376
 
 
377
 
    elt = va_arg(list, char *);
378
 
 
379
 
    if (elt != NULL) {
380
 
 
381
 
      BFT_REALLOC(*path,
382
 
                  strlen(*path)+ strlen(elt)+ strlen("/") +1,
383
 
                  char);
384
 
 
385
 
      strcat(*path, "/");
386
 
      strcat(*path, elt);
387
 
    }
388
 
  }
389
 
 
390
 
  va_end(list);
391
 
}
392
 
 
393
 
/*----------------------------------------------------------------------------
394
 
 * Add an element's attribute to the path.
395
 
 *
396
 
 * parameters:
397
 
 *   path               <-->  path for the xpath request
398
 
 *   attribute_name      -->  label of the new attribute in the path
399
 
 *----------------------------------------------------------------------------*/
400
 
 
401
 
void
402
 
cs_xpath_add_attribute(      char      **path,
403
 
                       const char *const attribute_name)
404
 
{
405
 
  assert(path);
406
 
  assert(attribute_name);
407
 
 
408
 
  BFT_REALLOC(*path,
409
 
              strlen(*path)+ strlen(attribute_name)+ strlen("/@")+1,
410
 
              char);
411
 
 
412
 
  strcat(*path, "/@");
413
 
  strcat(*path, attribute_name);
414
 
}
415
 
 
416
 
/*----------------------------------------------------------------------------
417
 
 * Add the i'st element to the path.
418
 
 *
419
 
 * parameters:
420
 
 *   path               <-->  path for the xpath request
421
 
 *   element             -->  label of the new element in the path
422
 
 *   num                 -->  number of the element's markup
423
 
 *----------------------------------------------------------------------------*/
424
 
 
425
 
void
426
 
cs_xpath_add_element_num(      char  **     path,
427
 
                         const char  *const element,
428
 
                         const int          num)
429
 
{
430
 
  int   nfigures = 0;
431
 
  char *strnum = NULL;
432
 
 
433
 
  assert(path);
434
 
  assert(element);
435
 
 
436
 
  nfigures = cs_gui_characters_number(num);
437
 
 
438
 
  BFT_MALLOC(strnum, nfigures+1, char);
439
 
 
440
 
  BFT_REALLOC(*path,
441
 
              strlen(*path)+
442
 
              strlen("/")+
443
 
              strlen(element)+
444
 
              strlen("[")+
445
 
              nfigures+
446
 
              strlen("]")+1,
447
 
              char);
448
 
 
449
 
  strcat(*path, "/");
450
 
  strcat(*path, element);
451
 
  sprintf(strnum,"%d", num);
452
 
  strcat(*path, "[");
453
 
  strcat(*path, strnum);
454
 
  strcat(*path, "]");
455
 
 
456
 
  BFT_FREE(strnum);
457
 
}
458
 
 
459
 
/*----------------------------------------------------------------------------
460
 
 * Add a test on a value associated to an attribute to the path.
461
 
 *
462
 
 * parameters:
463
 
 *   path               <-->  path for the xpath request
464
 
 *   attribute_type      -->  label of the attribute for the test in the path
465
 
 *   attribute_value     -->  value of the attribute for the test in the path
466
 
 *----------------------------------------------------------------------------*/
467
 
 
468
 
void
469
 
cs_xpath_add_test_attribute(      char **      path,
470
 
                            const char  *const attribute_type,
471
 
                            const char  *const attribute_value)
472
 
{
473
 
  assert(path);
474
 
  assert(attribute_type);
475
 
  assert(attribute_value);
476
 
 
477
 
  BFT_REALLOC(*path, strlen(*path)+
478
 
              strlen("[@")+
479
 
              strlen(attribute_type)+
480
 
              strlen("='")+
481
 
              strlen(attribute_value)+
482
 
              strlen("']")+1,
483
 
              char);
484
 
 
485
 
  strcat(*path, "[@");
486
 
  strcat(*path, attribute_type);
487
 
  strcat(*path, "='");
488
 
  strcat(*path, attribute_value);
489
 
  strcat(*path, "']");
490
 
}
491
 
 
492
 
/*----------------------------------------------------------------------------
493
 
 * Add the 'text()' xpath function to the path.
494
 
 *
495
 
 * parameter:
496
 
 *   path               <-->  path for the xpath request
497
 
 *----------------------------------------------------------------------------*/
498
 
 
499
 
void
500
 
cs_xpath_add_function_text(char **path)
501
 
{
502
 
  assert(path);
503
 
 
504
 
  BFT_REALLOC(*path,
505
 
              strlen(*path)+ strlen("/text()")+1,
506
 
              char);
507
 
 
508
 
  strcat(*path, "/text()");
509
 
}
510
 
 
511
 
/*----------------------------------------------------------------------------
512
 
 * Return a list of attributes nodes name from the xpath request in an array.
513
 
 * Example: from <a attr="c"/><b attr="d"/> return {c,d}
514
 
 *
515
 
 * parameter:
516
 
 *   path                -->  path for the xpath request
517
 
 *   size               <--   array size
518
 
 *----------------------------------------------------------------------------*/
519
 
 
520
 
char**
521
 
cs_gui_get_attribute_values(char *const path,
522
 
                            int  *const size)
523
 
{
524
 
#if defined(HAVE_LIBXML2)
525
 
 
526
 
  char             **nodes_name = NULL;
527
 
  xmlNodeSetPtr      nodes;
528
 
  xmlNodePtr         cur;
529
 
  xmlXPathObjectPtr  xpathObj;
530
 
  int                i;
531
 
 
532
 
  assert(path);
533
 
 
534
 
  xpathObj = xmlXPathEvalExpression(BAD_CAST path, xpathCtx);
535
 
 
536
 
  if (xpathObj == NULL)
537
 
    bft_error(__FILE__, __LINE__, 0, _("Invalid xpath: %s\n"), path);
538
 
 
539
 
  nodes = xpathObj->nodesetval;
540
 
  *size  = (nodes) ? nodes->nodeNr : 0;
541
 
 
542
 
  if (*size != 0) {
543
 
 
544
 
    BFT_MALLOC(nodes_name, *size, char*);
545
 
 
546
 
    for (i =0; i < *size; i++) {
547
 
      assert(nodes->nodeTab[0]);
548
 
 
549
 
      if (nodes->nodeTab[i]->type == XML_ATTRIBUTE_NODE) {
550
 
        cur = nodes->nodeTab[i];
551
 
        BFT_MALLOC(nodes_name[i],
552
 
                   strlen((char *) cur->children->content)+1,
553
 
                   char);
554
 
        strcpy(nodes_name[i], (char*) cur->children->content);
555
 
      }
556
 
      else
557
 
        bft_error(__FILE__, __LINE__, 0,
558
 
                  _("The node type is not XML_ATTRIBUTE_NODE.\nXpath: %s\n"),
559
 
                  path);
560
 
    }
561
 
  }
562
 
 
563
 
  xmlXPathFreeObject(xpathObj);
564
 
  return nodes_name;
565
 
 
566
 
#else
567
 
 
568
 
  bft_error(__FILE__, __LINE__, 0,
569
 
            _("Code_Saturne has been compiled without Xml support."));
570
 
 
571
 
  return NULL;
572
 
 
573
 
#endif
574
 
}
575
 
 
576
 
/*----------------------------------------------------------------------------
577
 
 * Return the value of an element's attribute.
578
 
 * Example: from <a b="c"/> return c
579
 
 *
580
 
 * parameter:
581
 
 *   path                -->  path for the xpath request
582
 
 *----------------------------------------------------------------------------*/
583
 
 
584
 
char*
585
 
cs_gui_get_attribute_value(char *const path)
586
 
{
587
 
  char **array = NULL;
588
 
  char  *attr = NULL;
589
 
  int    size;
590
 
 
591
 
  array = cs_gui_get_attribute_values(path, &size);
592
 
 
593
 
  if ((array == NULL) || (size == 0))
594
 
    return NULL;
595
 
 
596
 
  if (size > 1)
597
 
    bft_error(__FILE__, __LINE__, 0,
598
 
              _("Several attributes found: %i \n"
599
 
                "The first one is %s \nXpath: %s\n"),
600
 
                size, array[0], path);
601
 
 
602
 
  BFT_MALLOC(attr, strlen(array[0])+1, char);
603
 
  strcpy(attr, array[0]);
604
 
 
605
 
  BFT_FREE(array[0]);
606
 
  BFT_FREE(array);
607
 
 
608
 
  return attr;
609
 
}
610
 
 
611
 
/*----------------------------------------------------------------------------
612
 
 * Return a list of children nodes name from the xpath request in an array.
613
 
 * Example: from <a>3<\a><b>4<\b> return {a,b}
614
 
 *
615
 
 * parameters:
616
 
 *   path                -->  path for the xpath request
617
 
 *   size               <--   array size
618
 
 *----------------------------------------------------------------------------*/
619
 
 
620
 
char**
621
 
cs_gui_get_nodes_name(char *const path,
622
 
                      int  *const size)
623
 
{
624
 
#if defined(HAVE_LIBXML2)
625
 
 
626
 
  char             **nodes_name = NULL;
627
 
  xmlXPathObjectPtr  xpathObj;
628
 
  xmlNodeSetPtr      nodes;
629
 
  xmlNodePtr         cur;
630
 
  int                i;
631
 
 
632
 
  assert(path);
633
 
 
634
 
  xpathObj = xmlXPathEvalExpression(BAD_CAST path, xpathCtx);
635
 
 
636
 
  if (xpathObj == NULL)
637
 
    bft_error(__FILE__, __LINE__, 0, _("Invalid xpath: %s\n"), path);
638
 
 
639
 
  nodes = xpathObj->nodesetval;
640
 
  *size = (nodes) ? nodes->nodeNr : 0;
641
 
 
642
 
  if (*size != 0) {
643
 
 
644
 
    BFT_MALLOC(nodes_name, *size, char*);
645
 
 
646
 
    for (i =0; i < *size; i++) {
647
 
      assert(nodes->nodeTab[0]);
648
 
 
649
 
      if (nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
650
 
        cur = nodes->nodeTab[i];
651
 
        BFT_MALLOC(nodes_name[i], strlen((const char *) cur->name)+1, char);
652
 
        strcpy(nodes_name[i], (const char*) cur->name);
653
 
      } else
654
 
        bft_error(__FILE__, __LINE__, 0,
655
 
                  _("The node type is not XML_ELEMENT_NODE.\nXpath: %s\n"),
656
 
                  path);
657
 
    }
658
 
  }
659
 
 
660
 
  xmlXPathFreeObject(xpathObj);
661
 
  return nodes_name;
662
 
 
663
 
#else
664
 
 
665
 
  bft_error(__FILE__, __LINE__, 0,
666
 
            _("Code_Saturne has been compiled without Xml support."));
667
 
 
668
 
  return NULL;
669
 
 
670
 
#endif
671
 
}
672
 
 
673
 
 
674
 
/*----------------------------------------------------------------------------
675
 
 * Return a single node's name from the xpath request.
676
 
 *
677
 
 * parameter:
678
 
 *   path                -->  path for the xpath request
679
 
 *----------------------------------------------------------------------------*/
680
 
 
681
 
char*
682
 
cs_gui_get_node_name(char *const path)
683
 
{
684
 
  char **array = NULL;
685
 
  char  *name = NULL;
686
 
  int    size;
687
 
 
688
 
  array = cs_gui_get_nodes_name(path, &size);
689
 
 
690
 
  if ((array == NULL) || (size == 0))
691
 
    return NULL;
692
 
 
693
 
  if (size > 1)
694
 
    bft_error(__FILE__, __LINE__, 0,
695
 
              _("Several nodes name found: %i \n"
696
 
                "The first one is %s \nXpath: %s\n"),
697
 
                size, array[0], path);
698
 
 
699
 
  BFT_MALLOC(name, strlen(array[0])+1, char);
700
 
  strcpy(name, array[0]);
701
 
 
702
 
  BFT_FREE(array[0]);
703
 
  BFT_FREE(array);
704
 
 
705
 
  return name;
706
 
}
707
 
 
708
 
/*----------------------------------------------------------------------------
709
 
 * Return a list of children text nodes from the xpath request in an array.
710
 
 * Example: from <a>3<\a><a>4<\a> return {3,4}
711
 
 *
712
 
 * parameters:
713
 
 *   path                -->  path for the xpath request
714
 
 *   size               <--   array size
715
 
 *----------------------------------------------------------------------------*/
716
 
 
717
 
char**
718
 
cs_gui_get_text_values(char   *const path,
719
 
                       int    *const size)
720
 
{
721
 
#if defined(HAVE_LIBXML2)
722
 
 
723
 
  char             **text_name = NULL;
724
 
  xmlXPathObjectPtr  xpathObj;
725
 
  xmlNodeSetPtr      nodes;
726
 
  xmlNodePtr         cur;
727
 
  int                i;
728
 
 
729
 
  assert(path);
730
 
 
731
 
  xpathObj = xmlXPathEvalExpression(BAD_CAST path, xpathCtx);
732
 
 
733
 
  if (xpathObj == NULL)
734
 
    bft_error(__FILE__, __LINE__, 0, _("Invalid xpath: %s\n"), path);
735
 
 
736
 
  nodes = xpathObj->nodesetval;
737
 
  *size = (nodes) ? nodes->nodeNr : 0;
738
 
 
739
 
  if (*size != 0) {
740
 
 
741
 
    BFT_MALLOC(text_name, *size, char*);
742
 
 
743
 
    for (i =0; i < *size; i++) {
744
 
 
745
 
      assert(nodes->nodeTab[0]);
746
 
 
747
 
      if (nodes->nodeTab[i]->type == XML_TEXT_NODE) {
748
 
        cur = nodes->nodeTab[i];
749
 
        BFT_MALLOC(text_name[i], strlen((char *) cur->content)+1, char);
750
 
        strcpy(text_name[i], (char*) cur->content);
751
 
      }
752
 
      else
753
 
        bft_error(__FILE__, __LINE__, 0,
754
 
                  _("The node type is not XML_TEXT_NODE.\nXpath: %s\n"),
755
 
                  path);
756
 
    }
757
 
  }
758
 
 
759
 
  xmlXPathFreeObject(xpathObj);
760
 
  return text_name;
761
 
 
762
 
#else
763
 
 
764
 
  bft_error(__FILE__, __LINE__, 0,
765
 
            _("Code_Saturne has been compiled without Xml support."));
766
 
 
767
 
  return NULL;
768
 
 
769
 
#endif
770
 
}
771
 
 
772
 
/*----------------------------------------------------------------------------
773
 
 * Return a single children text node from the xpath request.
774
 
 *
775
 
 * parameter:
776
 
 *   path                -->  path for the xpath request
777
 
 *----------------------------------------------------------------------------*/
778
 
 
779
 
char*
780
 
cs_gui_get_text_value(char *const path)
781
 
{
782
 
  char **array = NULL;
783
 
  char  *text = NULL;
784
 
  int    size;
785
 
 
786
 
  array = cs_gui_get_text_values(path, &size);
787
 
 
788
 
  if ((array == NULL) || (size == 0))
789
 
    return NULL;
790
 
 
791
 
  if (size > 1)
792
 
    bft_error(__FILE__, __LINE__, 0,
793
 
            _("Several text node found: %i \n"
794
 
              "The first one is %s \nXpath: %s\n"),
795
 
              size, array[0], path);
796
 
 
797
 
  BFT_MALLOC(text, strlen(array[0])+1, char);
798
 
  strcpy(text, array[0]);
799
 
 
800
 
  BFT_FREE(array[0]);
801
 
  BFT_FREE(array);
802
 
 
803
 
  return text;
804
 
}
805
 
 
806
 
/*----------------------------------------------------------------------------
807
 
 * Modify the value parameter and return 1 if the xpath request succeeded,
808
 
 * otherwise just return 0.
809
 
 *
810
 
 * parameters:
811
 
 *   path                -->  path for the xpath request
812
 
 *   value              <--   double result of the xpath request
813
 
 *----------------------------------------------------------------------------*/
814
 
 
815
 
int
816
 
cs_gui_get_double(char   *const path,
817
 
                  double *const value)
818
 
{
819
 
  char *text_name = NULL;
820
 
  int   test;
821
 
 
822
 
  text_name = cs_gui_get_text_value(path);
823
 
 
824
 
  if (text_name == NULL)
825
 
    test = 0;
826
 
  else {
827
 
    *value = atof(text_name);
828
 
    BFT_FREE(text_name);
829
 
    test = 1;
830
 
  }
831
 
  return test;
832
 
}
833
 
 
834
 
/*----------------------------------------------------------------------------
835
 
 * Modify the value parameter and return 1 if the xpath request succeeded,
836
 
 * otherwise just return 0.
837
 
 *
838
 
 * parameters:
839
 
 *   path                -->  path for the xpath request
840
 
 *   value              <--   integer result of the xpath request
841
 
 *----------------------------------------------------------------------------*/
842
 
 
843
 
int
844
 
cs_gui_get_int(char *const path,
845
 
               int  *const value)
846
 
{
847
 
  char *text_name = NULL;
848
 
  int   test;
849
 
 
850
 
  text_name = cs_gui_get_text_value(path);
851
 
 
852
 
  if (text_name == NULL)
853
 
    test = 0;
854
 
  else {
855
 
    *value = atoi(text_name);
856
 
    BFT_FREE(text_name);
857
 
    test = 1;
858
 
  }
859
 
  return test;
860
 
}
861
 
 
862
 
/*----------------------------------------------------------------------------
863
 
 * Return the number of elements (i.e. the number of xml markups)
864
 
 * from a xpath request.
865
 
 * Example: from <a>3<\a><a>4<\a> return 2
866
 
 *
867
 
 * parameter:
868
 
 *   path                -->  path for the xpath request
869
 
 *----------------------------------------------------------------------------*/
870
 
 
871
 
int
872
 
cs_gui_get_nb_element(char *const path)
873
 
{
874
 
#if defined(HAVE_LIBXML2)
875
 
 
876
 
  xmlXPathObjectPtr xpathObj;
877
 
  int nb;
878
 
 
879
 
  assert(path);
880
 
 
881
 
  xpathObj = xmlXPathEvalExpression(BAD_CAST path, xpathCtx);
882
 
 
883
 
  if (xpathObj == NULL)
884
 
    bft_error(__FILE__, __LINE__, 0, _("Invalid xpath: %s\n"), path);
885
 
 
886
 
  nb = (xpathObj->nodesetval) ? xpathObj->nodesetval->nodeNr : 0;
887
 
 
888
 
  xmlXPathFreeObject(xpathObj);
889
 
 
890
 
  return nb;
891
 
 
892
 
#else
893
 
 
894
 
  bft_error(__FILE__, __LINE__, 0,
895
 
            _("Code_Saturne has been compiled without Xml support."));
896
 
 
897
 
  return 0;
898
 
 
899
 
#endif
900
 
}
901
 
 
902
 
/*----------------------------------------------------------------------------
903
 
 * Return the integer max value from a list, which is a xpath request result.
904
 
 * Example: from <a>3<\a><a>4<\a> return 4
905
 
 *
906
 
 * parameter:
907
 
 *   path                -->  path for the xpath request
908
 
 *----------------------------------------------------------------------------*/
909
 
 
910
 
int
911
 
cs_gui_get_max_value(char *const path)
912
 
{
913
 
#if defined(HAVE_LIBXML2)
914
 
 
915
 
  xmlXPathObjectPtr xpathObj;
916
 
  xmlNodeSetPtr     nodes;
917
 
  xmlNodePtr        cur;
918
 
  int               max_val=0;
919
 
  int               size;
920
 
  int               i;
921
 
 
922
 
  assert(path);
923
 
 
924
 
  xpathObj = xmlXPathEvalExpression(BAD_CAST path, xpathCtx);
925
 
 
926
 
  if (xpathObj == NULL)
927
 
    bft_error(__FILE__, __LINE__, 0, _("Invalid xpath: %s\n"), path);
928
 
 
929
 
  nodes=xpathObj->nodesetval;
930
 
  size = (nodes) ? nodes->nodeNr : 0;
931
 
 
932
 
  if (size == 0)
933
 
    bft_error (__FILE__, __LINE__, 0, _("No markup found: %s \n"), path);
934
 
 
935
 
  else {
936
 
    for (i=0; i <size; i++) {
937
 
      assert(nodes->nodeTab[i]);
938
 
      if(nodes->nodeTab[i]->type == XML_TEXT_NODE) {
939
 
        cur = nodes->nodeTab[i];
940
 
        max_val = CS_MAX(max_val, atoi((char*) cur->content));
941
 
      }
942
 
      else {
943
 
        bft_error(__FILE__, __LINE__, 0,
944
 
                  _("The node type is not XML_TEXT_NODE.\nXpath: %s\n"), path);
945
 
      }
946
 
    }
947
 
  }
948
 
 
949
 
  xmlXPathFreeObject(xpathObj);
950
 
 
951
 
  return max_val;
952
 
 
953
 
#else
954
 
 
955
 
  bft_error(__FILE__, __LINE__, 0,
956
 
            _("Code_Saturne has been compiled without Xml support."));
957
 
 
958
 
  return 0;
959
 
 
960
 
#endif
961
 
}
962
 
 
963
 
/*-----------------------------------------------------------------------------
964
 
 * Evaluate the "status" attribute value.
965
 
 * Return 1 if the xpath request has succeeded, 0 otherwise.
966
 
 *
967
 
 * parameter:
968
 
 *   path                -->  path for the xpath request
969
 
 *   result             <--   status="on" return 1, status="off" return 0
970
 
 *----------------------------------------------------------------------------*/
971
 
 
972
 
int
973
 
cs_gui_get_status(char *const path,
974
 
                  int  *const result)
975
 
{
976
 
  char *status;
977
 
  int   istatus;
978
 
 
979
 
  status = cs_gui_get_attribute_value(path);
980
 
 
981
 
  if (status == NULL)
982
 
    istatus = 0;
983
 
  else {
984
 
    istatus = 1;
985
 
 
986
 
    if (cs_gui_strcmp(status, "on"))
987
 
      *result = 1;
988
 
    else if (cs_gui_strcmp(status, "off"))
989
 
      *result = 0;
990
 
    else
991
 
      bft_error(__FILE__, __LINE__, 0,
992
 
                _("Invalid attribute value: %s \nXpath: %s\n"), status, path);
993
 
 
994
 
    BFT_FREE(status);
995
 
  }
996
 
 
997
 
  return istatus;
998
 
}
999
 
 
1000
 
/*-----------------------------------------------------------------------------
1001
 
 * Return the xml markup quantity
1002
 
 *
1003
 
 * parameters:
1004
 
 *   markup             -->  path for the markup
1005
 
 *   flag               -->  1: initialize the path with the root node;
1006
 
 *                           0: initialize the path with a short way
1007
 
 *----------------------------------------------------------------------------*/
1008
 
 
1009
 
int
1010
 
cs_gui_get_tag_number(const char *const markup, const int flag)
1011
 
{
1012
 
  char *path = NULL;
1013
 
  int   number = 0;
1014
 
 
1015
 
  if (flag) {
1016
 
    path = cs_xpath_init_path();
1017
 
  } else {
1018
 
    BFT_MALLOC(path, strlen("/")+1, char);
1019
 
    strcpy(path, "/");
1020
 
  }
1021
 
  cs_xpath_add_element(&path, markup);
1022
 
  number = cs_gui_get_nb_element(path);
1023
 
 
1024
 
  BFT_FREE(path);
1025
 
 
1026
 
  return number;
1027
 
}
1028
 
 
1029
 
/*-----------------------------------------------------------------------------
1030
 
 * Return the number of characters needed to write an integer number
1031
 
 *
1032
 
 * parameter:
1033
 
 *   num                -->  integer number
1034
 
 *----------------------------------------------------------------------------*/
1035
 
 
1036
 
int
1037
 
cs_gui_characters_number(const int num)
1038
 
{
1039
 
  int i      = 1;
1040
 
  int number = 0;
1041
 
 
1042
 
  assert(num > 0);
1043
 
 
1044
 
  if (num == 0)
1045
 
    number ++;
1046
 
  else
1047
 
    for (i=1; i <= num; i *= 10)
1048
 
      number++;
1049
 
 
1050
 
  return number;
1051
 
}
1052
 
 
1053
 
/*-----------------------------------------------------------------------------
1054
 
 * Comparison between two string: return 1 if the two string are equal, 0
1055
 
 * otherwise.
1056
 
 *
1057
 
 * parameters:
1058
 
 *   s1                -->  first string
1059
 
 *   s2                -->  second string
1060
 
 *----------------------------------------------------------------------------*/
1061
 
 
1062
 
int
1063
 
cs_gui_strcmp(const char *const s1,
1064
 
              const char *const s2)
1065
 
{
1066
 
  if (s1 == NULL || s2 == NULL) return 0;
1067
 
  if ( strlen(s1) != strlen(s2)) return 0;
1068
 
  if (!strncmp(s1, s2, strlen(s1))) return 1;
1069
 
  return 0;
1070
 
}
1071
 
 
1072
 
/*-----------------------------------------------------------------------------
1073
 
 * Copy a C string into a Fortran string.
1074
 
 *
1075
 
 * parameters:
1076
 
 *   chainef          <--> Fortran string
1077
 
 *   chainc            -->  C string
1078
 
 *   lstrF             -->  maximum length of the Fortran string
1079
 
 *----------------------------------------------------------------------------*/
1080
 
 
1081
 
void
1082
 
cs_gui_strcpy_c2f(      char *const chainef,
1083
 
                  const char *const chainec,
1084
 
                  const int         lstrF)
1085
 
{
1086
 
  int i;
1087
 
 
1088
 
  assert(chainec != NULL);
1089
 
  assert(lstrF > 0);
1090
 
 
1091
 
  strncpy(chainef, chainec, strlen(chainec));
1092
 
 
1093
 
  for (i = strlen(chainec); i < lstrF ; i++)
1094
 
    chainef[i] = ' ';
1095
 
}
1096
 
 
1097
 
/*----------------------------------------------------------------------------*/
1098
 
 
1099
 
END_C_DECLS