2 Copyright (C) 2012 Modelon AB
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the BSD style license.
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.
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>.
16 #ifndef JM_STRING_SET_H
17 #define JM_STRING_SET_H
22 #include "jm_vector.h"
26 /** \file jm_string_set.h Definition of ::jm_string_set and supporting functions
28 * \addtogroup jm_utils
30 * \addtogroup jm_string_set_group
34 /** \addtogroup jm_string_set_group A set of strings
39 \brief Set of string is based on a vector
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 */
45 \brief Find a string in a set.
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.
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;
58 * \brief Put an element in the set if it is not there yet.
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.
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;
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);
86 #endif /* JM_STRING_SET_H */