mptsd reads mpegts streams from udp/multicast or http and combines them into one multiple program stream that is suitable for outputting to DVB-C modulator. Tested with Dektec DTE-3114 Quad QAM Modulator and used in production in small DVB-C networks. https://georgi.unixsol.org/programs/mptsd/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inidict.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. Copyright (c) 2000-2007 by Nicolas Devillard.
  3. MIT License
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE.
  19. */
  20. /*-------------------------------------------------------------------------*/
  21. /**
  22. @file dictionary.c
  23. @author N. Devillard
  24. @date Sep 2007
  25. @version Revision: 1.27
  26. @brief Implements a dictionary for string variables.
  27. This module implements a simple dictionary object, i.e. a list
  28. of string/string associations. This object is useful to store e.g.
  29. informations retrieved from a configuration file (ini files).
  30. */
  31. /*--------------------------------------------------------------------------*/
  32. /*
  33. Id: inidict.c,v 1.2 2010/04/26 09:35:52 gf Exp
  34. Revision: 1.2
  35. */
  36. /*---------------------------------------------------------------------------
  37. Includes
  38. ---------------------------------------------------------------------------*/
  39. #include "inidict.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44. /** Maximum value size for integers and doubles. */
  45. #define MAXVALSZ 1024
  46. /** Minimal allocated number of entries in a dictionary */
  47. #define DICTMINSZ 128
  48. /** Invalid key token */
  49. #define DICT_INVALID_KEY ((char*)-1)
  50. /*---------------------------------------------------------------------------
  51. Private functions
  52. ---------------------------------------------------------------------------*/
  53. /* Doubles the allocated size associated to a pointer */
  54. /* 'size' is the current allocated size. */
  55. static void * mem_double(void * ptr, int size)
  56. {
  57. void * newptr ;
  58. newptr = calloc(2*size, 1);
  59. if (newptr==NULL) {
  60. return NULL ;
  61. }
  62. memcpy(newptr, ptr, size);
  63. free(ptr);
  64. return newptr ;
  65. }
  66. /*-------------------------------------------------------------------------*/
  67. /**
  68. @brief Duplicate a string
  69. @param s String to duplicate
  70. @return Pointer to a newly allocated string, to be freed with free()
  71. This is a replacement for strdup(). This implementation is provided
  72. for systems that do not have it.
  73. */
  74. /*--------------------------------------------------------------------------*/
  75. static char * xstrdup(char * s)
  76. {
  77. char * t ;
  78. if (!s)
  79. return NULL ;
  80. t = malloc(strlen(s)+1) ;
  81. if (t) {
  82. strcpy(t,s);
  83. }
  84. return t ;
  85. }
  86. /*---------------------------------------------------------------------------
  87. Function codes
  88. ---------------------------------------------------------------------------*/
  89. /*-------------------------------------------------------------------------*/
  90. /**
  91. @brief Compute the hash key for a string.
  92. @param key Character string to use for key.
  93. @return 1 unsigned int on at least 32 bits.
  94. This hash function has been taken from an Article in Dr Dobbs Journal.
  95. This is normally a collision-free function, distributing keys evenly.
  96. The key is stored anyway in the struct so that collision can be avoided
  97. by comparing the key itself in last resort.
  98. */
  99. /*--------------------------------------------------------------------------*/
  100. unsigned dictionary_hash(char * key)
  101. {
  102. int len ;
  103. unsigned hash ;
  104. int i ;
  105. len = strlen(key);
  106. for (hash=0, i=0 ; i<len ; i++) {
  107. hash += (unsigned)key[i] ;
  108. hash += (hash<<10);
  109. hash ^= (hash>>6) ;
  110. }
  111. hash += (hash <<3);
  112. hash ^= (hash >>11);
  113. hash += (hash <<15);
  114. return hash ;
  115. }
  116. /*-------------------------------------------------------------------------*/
  117. /**
  118. @brief Create a new dictionary object.
  119. @param size Optional initial size of the dictionary.
  120. @return 1 newly allocated dictionary objet.
  121. This function allocates a new dictionary object of given size and returns
  122. it. If you do not know in advance (roughly) the number of entries in the
  123. dictionary, give size=0.
  124. */
  125. /*--------------------------------------------------------------------------*/
  126. dictionary * dictionary_new(int size)
  127. {
  128. dictionary * d ;
  129. /* If no size was specified, allocate space for DICTMINSZ */
  130. if (size<DICTMINSZ) size=DICTMINSZ ;
  131. if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) {
  132. return NULL;
  133. }
  134. d->size = size ;
  135. d->val = (char **)calloc(size, sizeof(char*));
  136. d->key = (char **)calloc(size, sizeof(char*));
  137. d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
  138. return d ;
  139. }
  140. /*-------------------------------------------------------------------------*/
  141. /**
  142. @brief Delete a dictionary object
  143. @param d dictionary object to deallocate.
  144. @return void
  145. Deallocate a dictionary object and all memory associated to it.
  146. */
  147. /*--------------------------------------------------------------------------*/
  148. void dictionary_del(dictionary * d)
  149. {
  150. int i ;
  151. if (d==NULL) return ;
  152. for (i=0 ; i<d->size ; i++) {
  153. if (d->key[i]!=NULL)
  154. free(d->key[i]);
  155. if (d->val[i]!=NULL)
  156. free(d->val[i]);
  157. }
  158. free(d->val);
  159. free(d->key);
  160. free(d->hash);
  161. free(d);
  162. return ;
  163. }
  164. /*-------------------------------------------------------------------------*/
  165. /**
  166. @brief Get a value from a dictionary.
  167. @param d dictionary object to search.
  168. @param key Key to look for in the dictionary.
  169. @param def Default value to return if key not found.
  170. @return 1 pointer to internally allocated character string.
  171. This function locates a key in a dictionary and returns a pointer to its
  172. value, or the passed 'def' pointer if no such key can be found in
  173. dictionary. The returned character pointer points to data internal to the
  174. dictionary object, you should not try to free it or modify it.
  175. */
  176. /*--------------------------------------------------------------------------*/
  177. char * dictionary_get(dictionary * d, char * key, char * def)
  178. {
  179. unsigned hash ;
  180. int i ;
  181. hash = dictionary_hash(key);
  182. for (i=0 ; i<d->size ; i++) {
  183. if (d->key[i]==NULL)
  184. continue ;
  185. /* Compare hash */
  186. if (hash==d->hash[i]) {
  187. /* Compare string, to avoid hash collisions */
  188. if (!strcmp(key, d->key[i])) {
  189. return d->val[i] ;
  190. }
  191. }
  192. }
  193. return def ;
  194. }
  195. /*-------------------------------------------------------------------------*/
  196. /**
  197. @brief Set a value in a dictionary.
  198. @param d dictionary object to modify.
  199. @param key Key to modify or add.
  200. @param val Value to add.
  201. @return int 0 if Ok, anything else otherwise
  202. If the given key is found in the dictionary, the associated value is
  203. replaced by the provided one. If the key cannot be found in the
  204. dictionary, it is added to it.
  205. It is Ok to provide a NULL value for val, but NULL values for the dictionary
  206. or the key are considered as errors: the function will return immediately
  207. in such a case.
  208. Notice that if you dictionary_set a variable to NULL, a call to
  209. dictionary_get will return a NULL value: the variable will be found, and
  210. its value (NULL) is returned. In other words, setting the variable
  211. content to NULL is equivalent to deleting the variable from the
  212. dictionary. It is not possible (in this implementation) to have a key in
  213. the dictionary without value.
  214. This function returns non-zero in case of failure.
  215. */
  216. /*--------------------------------------------------------------------------*/
  217. int dictionary_set(dictionary * d, char * key, char * val)
  218. {
  219. int i ;
  220. unsigned hash ;
  221. if (d==NULL || key==NULL) return -1 ;
  222. /* Compute hash for this key */
  223. hash = dictionary_hash(key) ;
  224. /* Find if value is already in dictionary */
  225. if (d->n>0) {
  226. for (i=0 ; i<d->size ; i++) {
  227. if (d->key[i]==NULL)
  228. continue ;
  229. if (hash==d->hash[i]) { /* Same hash value */
  230. if (!strcmp(key, d->key[i])) { /* Same key */
  231. /* Found a value: modify and return */
  232. if (d->val[i]!=NULL)
  233. free(d->val[i]);
  234. d->val[i] = val ? xstrdup(val) : NULL ;
  235. /* Value has been modified: return */
  236. return 0 ;
  237. }
  238. }
  239. }
  240. }
  241. /* Add a new value */
  242. /* See if dictionary needs to grow */
  243. if (d->n==d->size) {
  244. /* Reached maximum size: reallocate dictionary */
  245. d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
  246. d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
  247. d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
  248. if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
  249. /* Cannot grow dictionary */
  250. return -1 ;
  251. }
  252. /* Double size */
  253. d->size *= 2 ;
  254. }
  255. /* Insert key in the first empty slot */
  256. for (i=0 ; i<d->size ; i++) {
  257. if (d->key[i]==NULL) {
  258. /* Add key here */
  259. break ;
  260. }
  261. }
  262. /* Copy key */
  263. d->key[i] = xstrdup(key);
  264. d->val[i] = val ? xstrdup(val) : NULL ;
  265. d->hash[i] = hash;
  266. d->n ++ ;
  267. return 0 ;
  268. }
  269. /*-------------------------------------------------------------------------*/
  270. /**
  271. @brief Delete a key in a dictionary
  272. @param d dictionary object to modify.
  273. @param key Key to remove.
  274. @return void
  275. This function deletes a key in a dictionary. Nothing is done if the
  276. key cannot be found.
  277. */
  278. /*--------------------------------------------------------------------------*/
  279. void dictionary_unset(dictionary * d, char * key)
  280. {
  281. unsigned hash ;
  282. int i ;
  283. if (key == NULL) {
  284. return;
  285. }
  286. hash = dictionary_hash(key);
  287. for (i=0 ; i<d->size ; i++) {
  288. if (d->key[i]==NULL)
  289. continue ;
  290. /* Compare hash */
  291. if (hash==d->hash[i]) {
  292. /* Compare string, to avoid hash collisions */
  293. if (!strcmp(key, d->key[i])) {
  294. /* Found key */
  295. break ;
  296. }
  297. }
  298. }
  299. if (i>=d->size)
  300. /* Key not found */
  301. return ;
  302. free(d->key[i]);
  303. d->key[i] = NULL ;
  304. if (d->val[i]!=NULL) {
  305. free(d->val[i]);
  306. d->val[i] = NULL ;
  307. }
  308. d->hash[i] = 0 ;
  309. d->n -- ;
  310. return ;
  311. }
  312. /*-------------------------------------------------------------------------*/
  313. /**
  314. @brief Dump a dictionary to an opened file pointer.
  315. @param d Dictionary to dump
  316. @param f Opened file pointer.
  317. @return void
  318. Dumps a dictionary onto an opened file pointer. Key pairs are printed out
  319. as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
  320. output file pointers.
  321. */
  322. /*--------------------------------------------------------------------------*/
  323. void dictionary_dump(dictionary * d, FILE * out)
  324. {
  325. int i ;
  326. if (d==NULL || out==NULL) return ;
  327. if (d->n<1) {
  328. fprintf(out, "empty dictionary\n");
  329. return ;
  330. }
  331. for (i=0 ; i<d->size ; i++) {
  332. if (d->key[i]) {
  333. fprintf(out, "%20s\t[%s]\n",
  334. d->key[i],
  335. d->val[i] ? d->val[i] : "UNDEF");
  336. }
  337. }
  338. return ;
  339. }
  340. /* Test code */
  341. #ifdef TESTDIC
  342. #define NVALS 20000
  343. int main(int argc, char *argv[])
  344. {
  345. dictionary * d ;
  346. char * val ;
  347. int i ;
  348. char cval[90] ;
  349. /* Allocate dictionary */
  350. printf("allocating...\n");
  351. d = dictionary_new(0);
  352. /* Set values in dictionary */
  353. printf("setting %d values...\n", NVALS);
  354. for (i=0 ; i<NVALS ; i++) {
  355. sprintf(cval, "%04d", i);
  356. dictionary_set(d, cval, "salut");
  357. }
  358. printf("getting %d values...\n", NVALS);
  359. for (i=0 ; i<NVALS ; i++) {
  360. sprintf(cval, "%04d", i);
  361. val = dictionary_get(d, cval, DICT_INVALID_KEY);
  362. if (val==DICT_INVALID_KEY) {
  363. printf("cannot get value for key [%s]\n", cval);
  364. }
  365. }
  366. printf("unsetting %d values...\n", NVALS);
  367. for (i=0 ; i<NVALS ; i++) {
  368. sprintf(cval, "%04d", i);
  369. dictionary_unset(d, cval);
  370. }
  371. if (d->n != 0) {
  372. printf("error deleting values\n");
  373. }
  374. printf("deallocating...\n");
  375. dictionary_del(d);
  376. return 0 ;
  377. }
  378. #endif
  379. /* vim: set ts=4 et sw=4 tw=75 */