]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil/native/FMUSimulator/include/FMIL/Util/JM/jm_string_set.h
(refs #6283) Introduce FMI interface library
[simantics/fmil.git] / org.simantics.fmil / native / FMUSimulator / include / FMIL / Util / JM / jm_string_set.h
1 /*
2     Copyright (C) 2012 Modelon AB
3
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the BSD style license.
6
7      This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10     FMILIB_License.txt file for more details.
11
12     You should have received a copy of the FMILIB_License.txt file
13     along with this program. If not, contact Modelon AB <http://www.modelon.com>.
14 */
15
16 #ifndef JM_STRING_SET_H
17 #define JM_STRING_SET_H
18
19 #include <string.h>
20
21 #include "jm_types.h"
22 #include "jm_vector.h"
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 /** \file jm_string_set.h Definition of ::jm_string_set and supporting functions
27         *
28         * \addtogroup jm_utils
29         * @{
30         *    \addtogroup jm_string_set_group
31         * @}
32         */
33
34         /** \addtogroup jm_string_set_group A set of strings
35          @{
36         */
37
38 /** 
39         \brief Set of string is based on a vector       
40
41 */
42 typedef struct jm_vector_jm_string jm_string_set; /* equivalent to "typedef jm_vector(jm_string) jm_string_set" which Doxygen does not understand */
43
44 /**
45 \brief Find a string in a set.
46
47 \param s A string set.
48 \param str Search string.
49 \return If found returns a pointer to the string saved in the set. If not found returns NULL.
50 */
51 static jm_string jm_string_set_find(jm_string_set* s, jm_string str) {
52     jm_string* found = jm_vector_find(jm_string)(s,&str,jm_compare_string);
53     if(found) return *found;
54     return 0;
55 }
56
57 /**
58 *  \brief Put an element in the set if it is not there yet.
59 *
60 *  @param s A string set.
61 *  \param str String to put.
62 *  @return A pointer to the inserted (or found) element or zero pointer if failed.
63 */
64 static jm_string jm_string_set_put(jm_string_set* s, jm_string str) {
65     jm_string found = jm_string_set_find(s, str);
66     if(found) return found;
67     {
68         char* newstr = 0;
69         size_t len = strlen(str) + 1;
70         jm_string* pnewstr = jm_vector_push_back(jm_string)(s, newstr);
71         if(pnewstr) *pnewstr = newstr = s->callbacks->malloc(len);
72         if(!pnewstr || !newstr) return 0;
73         memcpy(newstr, str, len);
74         jm_vector_qsort(jm_string)(s, jm_compare_string);
75         found = newstr;
76     }
77     return found;
78 }
79 /** @}
80         */
81
82 #ifdef __cplusplus
83 }
84 #endif
85
86 #endif /* JM_STRING_SET_H */