/[escript]/branches/doubleplusgood/dudley/src/TagMap.cpp
ViewVC logotype

Contents of /branches/doubleplusgood/dudley/src/TagMap.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4332 - (show annotations)
Thu Mar 21 04:21:14 2013 UTC (6 years ago) by jfenwick
File size: 2740 byte(s)
like sand though the hourglass
1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2013 by University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development since 2012 by School of Earth Sciences
13 *
14 *****************************************************************************/
15
16 /************************************************************************************/
17
18 /* Dudley: simple mapping fram names to tag keys via a linked list */
19
20 /************************************************************************************/
21
22 #include "TagMap.h"
23 #include "esysUtils/mem.h"
24 #include "string.h"
25
26 /************************************************************************************/
27
28 void Dudley_TagMap_insert(Dudley_TagMap ** tag_map, const char *name, index_t tag_key)
29 {
30 Dudley_TagMap *map = NULL;
31 if (strlen(name) < 1)
32 {
33 Dudley_setError(VALUE_ERROR, "empty tag name.");
34 return;
35 }
36 if (strchr(name, 32) != NULL)
37 { /* check for space */
38 Dudley_setError(VALUE_ERROR, "tag name may not contain a space.");
39 return;
40 }
41 if (*tag_map == NULL)
42 {
43 map = new Dudley_TagMap;
44 if (Dudley_checkPtr(map))
45 return;
46 map->name = new char[strlen(name) + 1];
47 if (Dudley_checkPtr(map->name))
48 {
49 delete map;
50 }
51 else
52 {
53 strcpy(map->name, name);
54 map->tag_key = tag_key;
55 map->next = NULL;
56 *tag_map = map;
57 }
58 }
59 else
60 {
61 if (strcmp((*tag_map)->name, name) == 0)
62 {
63 (*tag_map)->tag_key = tag_key;
64 }
65 else
66 {
67 Dudley_TagMap_insert(&((*tag_map)->next), name, tag_key);
68 }
69 }
70 }
71
72 index_t Dudley_TagMap_getTag(Dudley_TagMap * tag_map, const char *name)
73 {
74 char error_msg[LenErrorMsg_MAX];
75 if (tag_map == NULL)
76 {
77 sprintf(error_msg, "Dudley_TagMap_getTag: unknown tag name %s.", name);
78 Dudley_setError(VALUE_ERROR, error_msg);
79 return -1;
80 }
81 else
82 {
83 if (strcmp(tag_map->name, name) == 0)
84 {
85 return tag_map->tag_key;
86 }
87 else
88 {
89 return Dudley_TagMap_getTag(tag_map->next, name);
90 }
91 }
92 }
93
94 bool_t Dudley_TagMap_isValidTagName(Dudley_TagMap * tag_map, const char *name)
95 {
96 if (tag_map == NULL)
97 {
98 return FALSE;
99 }
100 else
101 {
102 if (strcmp(tag_map->name, name) == 0)
103 {
104 return TRUE;
105 }
106 else
107 {
108 return Dudley_TagMap_isValidTagName(tag_map->next, name);
109 }
110 }
111 }
112
113 /* deallocates the Dudley_TagMap in by recursive calls */
114
115 void Dudley_TagMap_free(Dudley_TagMap * in)
116 {
117 if (in != NULL)
118 {
119 Dudley_TagMap_free(in->next);
120 delete[] in->name;
121 delete in;
122 }
123 return;
124 }

  ViewVC Help
Powered by ViewVC 1.1.26