strfunc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. *****************************************************************************
  3. *
  4. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  5. * R99 Version 3.3.0
  6. * REL-4 Version 4.1.0
  7. *
  8. *****************************************************************************
  9. *
  10. * File : strfunc.c
  11. * Purpose : string <-> mode and string <-> traffic channel
  12. * conversion functions
  13. *
  14. *****************************************************************************
  15. */
  16. /*
  17. *****************************************************************************
  18. * MODULE INCLUDE FILE AND VERSION ID
  19. *****************************************************************************
  20. */
  21. #include "strfunc.h"
  22. const char strfunc_id[] = "@(#)$Id $" strfunc_h;
  23. /*
  24. *****************************************************************************
  25. * INCLUDE FILES
  26. *****************************************************************************
  27. */
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "mode.h"
  31. #include "frame.h"
  32. /*
  33. *****************************************************************************
  34. * LOCAL VARIABLES AND TABLES
  35. *****************************************************************************
  36. */
  37. /*
  38. * generic string <-> id lookup table
  39. */
  40. typedef struct {
  41. char *name; /* name string */
  42. int id; /* integer id */
  43. } conv_table;
  44. /*
  45. * mode name <-> mode id conversion table
  46. */
  47. static const conv_table modetable[] = {
  48. {"MR475", MR475},
  49. {"MR515", MR515},
  50. {"MR59", MR59},
  51. {"MR67", MR67},
  52. {"MR74", MR74},
  53. {"MR795", MR795},
  54. {"MR102", MR102},
  55. {"MR122", MR122},
  56. {"MRDTX", MRDTX},
  57. {NULL, -1}
  58. };
  59. /*
  60. * frame type name <-> frame type id conversion table
  61. */
  62. static const conv_table rxframetable[] = {
  63. {"RX_SPEECH_GOOD", RX_SPEECH_GOOD},
  64. {"RX_SPEECH_DEGRADED", RX_SPEECH_DEGRADED},
  65. {"RX_SPEECH_BAD", RX_SPEECH_BAD},
  66. {"RX_SID_FIRST", RX_SID_FIRST},
  67. {"RX_SID_UPDATE", RX_SID_UPDATE},
  68. {"RX_SID_BAD", RX_SID_BAD},
  69. {"RX_ONSET", RX_ONSET},
  70. {"RX_NO_DATA", RX_NO_DATA},
  71. {NULL, -1}
  72. };
  73. /*
  74. * frame type name <-> frame type id conversion table
  75. */
  76. static const conv_table txframetable[] = {
  77. {"TX_SPEECH_GOOD", TX_SPEECH_GOOD},
  78. {"TX_SPEECH_DEGRADED", TX_SPEECH_DEGRADED},
  79. {"TX_SPEECH_BAD", TX_SPEECH_BAD},
  80. {"TX_SID_FIRST", TX_SID_FIRST},
  81. {"TX_SID_UPDATE", TX_SID_UPDATE},
  82. {"TX_SID_BAD", TX_SID_BAD},
  83. {"TX_ONSET", TX_ONSET},
  84. {"TX_NO_DATA", TX_NO_DATA},
  85. {NULL, -1}
  86. };
  87. /*
  88. *****************************************************************************
  89. * LOCAL PROGRAM CODE
  90. *****************************************************************************
  91. */
  92. /*************************************************************************
  93. *
  94. * Function: search_string
  95. * Purpose: search table for string; return id if found, -1 otherwise
  96. *
  97. **************************************************************************
  98. */
  99. static int search_string(const conv_table *p, const char *str)
  100. {
  101. if (str == NULL)
  102. return -1;
  103. do
  104. {
  105. if (strcmp(p->name, str) == 0)
  106. return p->id;
  107. p++;
  108. } while (p->name != NULL);
  109. return -1;
  110. }
  111. /*************************************************************************
  112. *
  113. * Function: search_id
  114. * Purpose: search table for id; return string if found, NULL otherwise
  115. *
  116. **************************************************************************
  117. */
  118. static char *search_id(const conv_table *p, int id)
  119. {
  120. if (id == -1)
  121. return NULL;
  122. do
  123. {
  124. if (p->id == id)
  125. return p->name;
  126. p++;
  127. } while (p->name != NULL);
  128. return NULL;
  129. }
  130. /*
  131. *****************************************************************************
  132. * PUBLIC PROGRAM CODE
  133. *****************************************************************************
  134. */
  135. /*************************************************************************
  136. *
  137. * Function: str2mode
  138. * Purpose: convert AMR mode string into mode id; return 0 on success,
  139. * 1 otherwise
  140. *
  141. **************************************************************************
  142. */
  143. int str2mode(const char* str, enum Mode *mode)
  144. {
  145. int m;
  146. if ((m = search_string(modetable, str)) == -1)
  147. return 1;
  148. *mode = (enum Mode) m;
  149. return 0;
  150. }
  151. /*************************************************************************
  152. *
  153. * Function: mode2str
  154. * Purpose: convert AMR mode id into mode name string; return 0 on success,
  155. * 1 otherwise
  156. *
  157. **************************************************************************
  158. */
  159. int mode2str(enum Mode mode, char** str)
  160. {
  161. return ((*str = search_id(modetable, (int) mode)) != NULL);
  162. }
  163. /*************************************************************************
  164. *
  165. * Function: frame2str
  166. *
  167. **************************************************************************
  168. */
  169. int rxframe2str(enum RXFrameType ft, char** str)
  170. {
  171. return ((*str = search_id(rxframetable, (int) ft)) != NULL);
  172. }
  173. /*************************************************************************
  174. *
  175. * Function: txframe2str
  176. *
  177. **************************************************************************
  178. */
  179. int txframe2str(enum TXFrameType ft, char** str)
  180. {
  181. return ((*str = search_id(txframetable, (int) ft)) != NULL);
  182. }