/lib/libhts/hts_strtab.h

http://github.com/xbmc/xbmc · C Header · 61 lines · 32 code · 11 blank · 18 comment · 5 complexity · cd7f5a16276c4afd3da548008c1db96a MD5 · raw file

  1. /*
  2. * tvheadend
  3. * Copyright (C) 2007 Andreas Öman
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #ifndef STRTAB_H_
  20. #define STRTAB_H_
  21. #include <strings.h>
  22. struct strtab {
  23. const char *str;
  24. int val;
  25. };
  26. static int str2val0(const char *str, struct strtab tab[], int l)
  27. __attribute((unused));
  28. static int
  29. str2val0(const char *str, struct strtab tab[], int l)
  30. {
  31. int i;
  32. for(i = 0; i < l; i++)
  33. if(!strcasecmp(str, tab[i].str))
  34. return tab[i].val;
  35. return -1;
  36. }
  37. #define str2val(str, tab) str2val0(str, tab, sizeof(tab) / sizeof(tab[0]))
  38. static const char * val2str0(int val, struct strtab tab[], int l)
  39. __attribute__((unused));
  40. static const char *
  41. val2str0(int val, struct strtab tab[], int l)
  42. {
  43. int i;
  44. for(i = 0; i < l; i++)
  45. if(tab[i].val == val)
  46. return tab[i].str;
  47. return NULL;
  48. }
  49. #define val2str(val, tab) val2str0(val, tab, sizeof(tab) / sizeof(tab[0]))
  50. #endif /* STRTAB_H_ */