silut.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "silut.h"
  4. /* This file contails lookup helper functions that
  5. * look up an integer based on a string or a string based on an integer,
  6. * making use of a lookup table of Silut records.
  7. */
  8. static Silut test_lut[] = {
  9. { 1, "one" },
  10. { 2, "two" },
  11. { 3, "three" },
  12. { -1, NULL }
  13. };
  14. /** Lenght of an array of silut. NULL is the guard clause. */
  15. int silut_len(Silut lut[]) {
  16. int index = 0;
  17. Silut * aid;
  18. if(!lut) return -1;
  19. for(index = 0, aid = lut + index;
  20. aid && aid->string ;
  21. index++, aid = lut + index)
  22. {}
  23. return index;
  24. }
  25. /** Looks up a string with an integer key integer in the lookup table by
  26. * simple linear search. The look up table needs not be sorted in any way,
  27. * but it's somewhat slower.
  28. * The look up table should have NULL as it's final element.
  29. * If not found, NULL is returned. If found the matching record is returned.
  30. */
  31. Silut * silut_lsearchi(Silut lut[], int integer) {
  32. int index = 0;
  33. Silut * aid;
  34. if(!lut) return NULL;
  35. for(index = 0, aid = lut + index;
  36. aid && aid->string ;
  37. index++, aid = lut + index)
  38. {
  39. if(aid->integer == integer) return aid;
  40. }
  41. return NULL;
  42. }
  43. /** Looks up an integer with an integer key integer in the lookup table by
  44. * simple linear search. The look up table needs not be sorted in any way,
  45. * but it's somewhat slower.
  46. * The look up table should have NULL as it's final element.
  47. * strcmp is used to compare the strings.
  48. * If not found, NULL is returned. If found the matching record is returned.
  49. * Returns null if string is null.
  50. */
  51. Silut * silut_lsearchcstr(Silut lut[], const char * string) {
  52. int index = 0;
  53. Silut * aid;
  54. if(!lut) return NULL;
  55. if(!string) return NULL;
  56. for(index = 0, aid = lut + index;
  57. aid && aid->string ;
  58. index++, aid = lut + index)
  59. {
  60. if(!strcmp(aid->string, string)) return aid;
  61. }
  62. return NULL;
  63. }
  64. /** Compare functions for siluts that compares the strings. */
  65. int silut_comparecstr(const void * one, const void * two) {
  66. Silut * lut1 = (Silut*) one;
  67. Silut * lut2 = (Silut*) two;
  68. return strcmp(lut1->string, lut2->string);
  69. }
  70. /** Compare functions for siluts that compares the integers. */
  71. int silut_compareint(const void * one, const void * two) {
  72. Silut * lut1 = (Silut*) one;
  73. Silut * lut2 = (Silut*) two;
  74. return lut2->integer - lut1->integer;
  75. }
  76. /** Looks up a Silut with an string in the lookup table by
  77. * applying bsearch. The look up table must be sorted with the strings ascending.
  78. *
  79. * The look up table should have NULL as it's final element.
  80. * If not found, NULL is returned. If found the matching record is returned.
  81. */
  82. Silut * silut_bsearchcstr(Silut lut[], const char * string) {
  83. Silut key = { -1, string };
  84. return (Silut *)
  85. bsearch(&key, lut, silut_len(lut), sizeof(Silut), silut_comparecstr);
  86. }
  87. /** Looks up a Silut with an integer in the lookup table by
  88. * applying bsearch. The look up table must be sorted with the integers ascending.
  89. *
  90. * The look up table should have NULL as it's final element.
  91. * If not found, NULL is returned. If found the matching record is returned.
  92. */
  93. Silut * silut_bsearchint(Silut lut[], int integer) {
  94. Silut key = { integer, "NOT FOUND" };
  95. return (Silut *)
  96. bsearch(&key, lut, silut_len(lut), sizeof(Silut), silut_compareint);
  97. }