Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

418 rader
20 KiB

  1. /*
  2. *********************************************************************************************************
  3. * uC/LIB
  4. * Custom Library Modules
  5. *
  6. * Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
  7. *
  8. * SPDX-License-Identifier: APACHE-2.0
  9. *
  10. * This software is subject to an open source license and is distributed by
  11. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  12. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  13. *
  14. *********************************************************************************************************
  15. */
  16. /*
  17. *********************************************************************************************************
  18. *
  19. * ASCII STRING MANAGEMENT
  20. *
  21. * Filename : lib_str.h
  22. * Version : V1.39.01
  23. *********************************************************************************************************
  24. * Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
  25. *
  26. * (a) ALL standard library functions are implemented in the custom library modules :
  27. *
  28. * (1) \<Custom Library Directory>\lib_*.*
  29. *
  30. * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
  31. *
  32. * where
  33. * <Custom Library Directory> directory path for custom library software
  34. * <cpu> directory name for specific processor (CPU)
  35. * <compiler> directory name for specific compiler
  36. *
  37. * (b) Product-specific library functions are implemented in individual products.
  38. *********************************************************************************************************
  39. */
  40. /*
  41. *********************************************************************************************************
  42. * MODULE
  43. *
  44. * Note(s) : (1) This string library header file is protected from multiple pre-processor inclusion through
  45. * use of the string library module present pre-processor macro definition.
  46. *********************************************************************************************************
  47. */
  48. #ifndef LIB_STR_MODULE_PRESENT /* See Note #1. */
  49. #define LIB_STR_MODULE_PRESENT
  50. /*
  51. *********************************************************************************************************
  52. * ASCII STRING CONFIGURATION DEFINES
  53. *
  54. * Note(s) : (1) Some ASCII string configuration #define's MUST be available PRIOR to including any
  55. * application configuration (see 'INCLUDE FILES Note #1a').
  56. *********************************************************************************************************
  57. */
  58. /*
  59. *********************************************************************************************************
  60. * STRING FLOATING POINT DEFINES
  61. *
  62. * Note(s) : (1) (a) (1) The maximum accuracy for 32-bit floating-point numbers :
  63. *
  64. *
  65. * Maximum Accuracy log [Internal-Base ^ (Number-Internal-Base-Digits)]
  66. * 32-bit Floating-point Number = -----------------------------------------------------
  67. * log [External-Base]
  68. *
  69. * log [2 ^ 24]
  70. * = --------------
  71. * log [10]
  72. *
  73. * < 7.225 Base-10 Digits
  74. *
  75. * where
  76. * Internal-Base Internal number base of floating-
  77. * point numbers (i.e. 2)
  78. * External-Base External number base of floating-
  79. * point numbers (i.e. 10)
  80. * Number-Internal-Base-Digits Number of internal number base
  81. * significant digits (i.e. 24)
  82. *
  83. * (2) Also, since some 32-bit floating-point calculations are converted to 32-bit
  84. * unsigned numbers, the maximum accuracy is limited to the maximum accuracy
  85. * for 32-bit unsigned numbers of 9 digits.
  86. *
  87. * (b) Some CPUs' &/or compilers' floating-point implementations MAY further reduce the
  88. * maximum accuracy.
  89. *********************************************************************************************************
  90. */
  91. #define LIB_STR_FP_MAX_NBR_DIG_SIG_MIN 1u
  92. #define LIB_STR_FP_MAX_NBR_DIG_SIG_MAX 9u /* See Note #1a2. */
  93. #define LIB_STR_FP_MAX_NBR_DIG_SIG_DFLT 7u /* See Note #1a1. */
  94. /*
  95. *********************************************************************************************************
  96. * INCLUDE FILES
  97. *
  98. * Note(s) : (1) The custom library software files are located in the following directories :
  99. *
  100. * (a) \<Your Product Application>\lib_cfg.h
  101. *
  102. * (b) \<Custom Library Directory>\lib_*.*
  103. *
  104. * where
  105. * <Your Product Application> directory path for Your Product's Application
  106. * <Custom Library Directory> directory path for custom library software
  107. *
  108. * (2) CPU-configuration software files are located in the following directories :
  109. *
  110. * (a) \<CPU-Compiler Directory>\cpu_*.*
  111. * (b) \<CPU-Compiler Directory>\<cpu>\<compiler>\cpu*.*
  112. *
  113. * where
  114. * <CPU-Compiler Directory> directory path for common CPU-compiler software
  115. * <cpu> directory name for specific processor (CPU)
  116. * <compiler> directory name for specific compiler
  117. *
  118. * (3) Compiler MUST be configured to include as additional include path directories :
  119. *
  120. * (a) '\<Your Product Application>\' directory See Note #1a
  121. *
  122. * (b) '\<Custom Library Directory>\' directory See Note #1b
  123. *
  124. * (c) (1) '\<CPU-Compiler Directory>\' directory See Note #2a
  125. * (2) '\<CPU-Compiler Directory>\<cpu>\<compiler>\' directory See Note #2b
  126. *
  127. * (4) NO compiler-supplied standard library functions SHOULD be used.
  128. *
  129. * #### The reference to standard library header files SHOULD be removed once all custom
  130. * library functions are implemented WITHOUT reference to ANY standard library function(s).
  131. *
  132. * See also 'STANDARD LIBRARY MACRO'S Note #1'.
  133. *********************************************************************************************************
  134. */
  135. #include <cpu.h>
  136. #include <lib_def.h>
  137. #include <lib_ascii.h>
  138. #include <lib_cfg.h>
  139. #if 0 /* See Note #4. */
  140. #include <stdio.h>
  141. #endif
  142. /*
  143. *********************************************************************************************************
  144. * EXTERNS
  145. *********************************************************************************************************
  146. */
  147. #ifdef LIB_STR_MODULE
  148. #define LIB_STR_EXT
  149. #else
  150. #define LIB_STR_EXT extern
  151. #endif
  152. /*
  153. *********************************************************************************************************
  154. * DEFAULT CONFIGURATION
  155. *********************************************************************************************************
  156. */
  157. /*
  158. *********************************************************************************************************
  159. * STRING FLOATING POINT CONFIGURATION
  160. *
  161. * Note(s) : (1) Configure LIB_STR_CFG_FP_EN to enable/disable floating point string function(s).
  162. *
  163. * (2) Configure LIB_STR_CFG_FP_MAX_NBR_DIG_SIG to configure the maximum number of significant
  164. * digits to calculate &/or display for floating point string function(s).
  165. *
  166. * See also 'STRING FLOATING POINT DEFINES Note #1'.
  167. *********************************************************************************************************
  168. */
  169. /* Configure floating point feature(s) [see Note #1] : */
  170. #ifndef LIB_STR_CFG_FP_EN
  171. #define LIB_STR_CFG_FP_EN DEF_DISABLED
  172. /* DEF_DISABLED Floating point functions DISABLED */
  173. /* DEF_ENABLED Floating point functions ENABLED */
  174. #endif
  175. /* Configure floating point feature(s)' number of ... */
  176. /* ... significant digits (see Note #2). */
  177. #ifndef LIB_STR_CFG_FP_MAX_NBR_DIG_SIG
  178. #define LIB_STR_CFG_FP_MAX_NBR_DIG_SIG LIB_STR_FP_MAX_NBR_DIG_SIG_DFLT
  179. #endif
  180. /*
  181. *********************************************************************************************************
  182. * DEFINES
  183. *********************************************************************************************************
  184. */
  185. #define STR_CR_LF "\r\n"
  186. #define STR_LF_CR "\n\r"
  187. #define STR_NEW_LINE STR_CR_LF
  188. #define STR_PARENT_PATH ".."
  189. #define STR_CR_LF_LEN (sizeof(STR_CR_LF) - 1)
  190. #define STR_LF_CR_LEN (sizeof(STR_LF_CR) - 1)
  191. #define STR_NEW_LINE_LEN (sizeof(STR_NEW_LINE) - 1)
  192. #define STR_PARENT_PATH_LEN (sizeof(STR_PARENT_PATH) - 1)
  193. /*
  194. *********************************************************************************************************
  195. * DATA TYPES
  196. *********************************************************************************************************
  197. */
  198. /*
  199. *********************************************************************************************************
  200. * GLOBAL VARIABLES
  201. *********************************************************************************************************
  202. */
  203. /*
  204. *********************************************************************************************************
  205. * MACRO'S
  206. *********************************************************************************************************
  207. */
  208. /*
  209. *********************************************************************************************************
  210. * STANDARD LIBRARY MACRO'S
  211. *
  212. * Note(s) : (1) NO compiler-supplied standard library functions SHOULD be used.
  213. *
  214. * #### The reference to standard memory functions SHOULD be removed once all custom library
  215. * functions are implemented WITHOUT reference to ANY standard library function(s).
  216. *
  217. * See also 'INCLUDE FILES Note #3'.
  218. *********************************************************************************************************
  219. */
  220. /* See Note #1. */
  221. #define Str_FmtPrint snprintf
  222. #define Str_FmtScan sscanf
  223. /*
  224. *********************************************************************************************************
  225. * FUNCTION PROTOTYPES
  226. *********************************************************************************************************
  227. */
  228. /* ------------------ STR LEN FNCTS ------------------ */
  229. CPU_SIZE_T Str_Len (const CPU_CHAR *pstr);
  230. CPU_SIZE_T Str_Len_N (const CPU_CHAR *pstr,
  231. CPU_SIZE_T len_max);
  232. /* ------------------ STR COPY FNCTS ------------------ */
  233. CPU_CHAR *Str_Copy ( CPU_CHAR *pstr_dest,
  234. const CPU_CHAR *pstr_src);
  235. CPU_CHAR *Str_Copy_N ( CPU_CHAR *pstr_dest,
  236. const CPU_CHAR *pstr_src,
  237. CPU_SIZE_T len_max);
  238. CPU_CHAR *Str_Cat ( CPU_CHAR *pstr_dest,
  239. const CPU_CHAR *pstr_cat);
  240. CPU_CHAR *Str_Cat_N ( CPU_CHAR *pstr_dest,
  241. const CPU_CHAR *pstr_cat,
  242. CPU_SIZE_T len_max);
  243. /* ------------------ STR CMP FNCTS ------------------ */
  244. CPU_INT16S Str_Cmp (const CPU_CHAR *p1_str,
  245. const CPU_CHAR *p2_str);
  246. CPU_INT16S Str_Cmp_N (const CPU_CHAR *p1_str,
  247. const CPU_CHAR *p2_str,
  248. CPU_SIZE_T len_max);
  249. CPU_INT16S Str_CmpIgnoreCase (const CPU_CHAR *p1_str,
  250. const CPU_CHAR *p2_str);
  251. CPU_INT16S Str_CmpIgnoreCase_N(const CPU_CHAR *p1_str,
  252. const CPU_CHAR *p2_str,
  253. CPU_SIZE_T len_max);
  254. /* ------------------ STR SRCH FNCTS ------------------ */
  255. CPU_CHAR *Str_Char (const CPU_CHAR *pstr,
  256. CPU_CHAR srch_char);
  257. CPU_CHAR *Str_Char_N (const CPU_CHAR *pstr,
  258. CPU_SIZE_T len_max,
  259. CPU_CHAR srch_char);
  260. CPU_CHAR *Str_Char_Last (const CPU_CHAR *pstr,
  261. CPU_CHAR srch_char);
  262. CPU_CHAR *Str_Char_Last_N (const CPU_CHAR *pstr,
  263. CPU_SIZE_T len_max,
  264. CPU_CHAR srch_char);
  265. CPU_CHAR *Str_Char_Replace ( CPU_CHAR *pstr,
  266. CPU_CHAR char_srch,
  267. CPU_CHAR char_replace);
  268. CPU_CHAR *Str_Char_Replace_N ( CPU_CHAR *pstr,
  269. CPU_CHAR char_srch,
  270. CPU_CHAR char_replace,
  271. CPU_SIZE_T len_max);
  272. CPU_CHAR *Str_Str (const CPU_CHAR *pstr,
  273. const CPU_CHAR *pstr_srch);
  274. CPU_CHAR *Str_Str_N (const CPU_CHAR *pstr,
  275. const CPU_CHAR *pstr_srch,
  276. CPU_SIZE_T len_max);
  277. /* ------------------ STR FMT FNCTS ------------------ */
  278. CPU_CHAR *Str_FmtNbr_Int32U ( CPU_INT32U nbr,
  279. CPU_INT08U nbr_dig,
  280. CPU_INT08U nbr_base,
  281. CPU_CHAR lead_char,
  282. CPU_BOOLEAN lower_case,
  283. CPU_BOOLEAN nul,
  284. CPU_CHAR *pstr);
  285. CPU_CHAR *Str_FmtNbr_Int32S ( CPU_INT32S nbr,
  286. CPU_INT08U nbr_dig,
  287. CPU_INT08U nbr_base,
  288. CPU_CHAR lead_char,
  289. CPU_BOOLEAN lower_case,
  290. CPU_BOOLEAN nul,
  291. CPU_CHAR *pstr);
  292. #if (LIB_STR_CFG_FP_EN == DEF_ENABLED)
  293. CPU_CHAR *Str_FmtNbr_32 ( CPU_FP32 nbr,
  294. CPU_INT08U nbr_dig,
  295. CPU_INT08U nbr_dp,
  296. CPU_CHAR lead_char,
  297. CPU_BOOLEAN nul,
  298. CPU_CHAR *pstr);
  299. #endif
  300. /* ----------------- STR PARSE FNCTS ------------------ */
  301. CPU_INT32U Str_ParseNbr_Int32U(const CPU_CHAR *pstr,
  302. CPU_CHAR **pstr_next,
  303. CPU_INT08U nbr_base);
  304. CPU_INT32S Str_ParseNbr_Int32S(const CPU_CHAR *pstr,
  305. CPU_CHAR **pstr_next,
  306. CPU_INT08U nbr_base);
  307. /*
  308. *********************************************************************************************************
  309. * CONFIGURATION ERRORS
  310. *********************************************************************************************************
  311. */
  312. #ifndef LIB_STR_CFG_FP_EN
  313. #error "LIB_STR_CFG_FP_EN not #define'd in 'lib_cfg.h'"
  314. #error " [MUST be DEF_DISABLED] "
  315. #error " [ || DEF_ENABLED ] "
  316. #elif ((LIB_STR_CFG_FP_EN != DEF_DISABLED) && \
  317. (LIB_STR_CFG_FP_EN != DEF_ENABLED ))
  318. #error "LIB_STR_CFG_FP_EN illegally #define'd in 'lib_cfg.h'"
  319. #error " [MUST be DEF_DISABLED] "
  320. #error " [ || DEF_ENABLED ] "
  321. #elif (LIB_STR_CFG_FP_EN == DEF_ENABLED)
  322. #ifndef LIB_STR_CFG_FP_MAX_NBR_DIG_SIG
  323. #error "LIB_STR_CFG_FP_MAX_NBR_DIG_SIG not #define'd in 'lib_cfg.h' "
  324. #error " [MUST be >= LIB_STR_FP_MAX_NBR_DIG_SIG_MIN]"
  325. #error " [ && <= LIB_STR_FP_MAX_NBR_DIG_SIG_MAX]"
  326. #elif (DEF_CHK_VAL(LIB_STR_CFG_FP_MAX_NBR_DIG_SIG, \
  327. LIB_STR_FP_MAX_NBR_DIG_SIG_MIN, \
  328. LIB_STR_FP_MAX_NBR_DIG_SIG_MAX) != DEF_OK)
  329. #error "LIB_STR_CFG_FP_MAX_NBR_DIG_SIG illegally #define'd in 'lib_cfg.h' "
  330. #error " [MUST be >= LIB_STR_FP_MAX_NBR_DIG_SIG_MIN]"
  331. #error " [ && <= LIB_STR_FP_MAX_NBR_DIG_SIG_MAX]"
  332. #endif
  333. #endif
  334. /*
  335. *********************************************************************************************************
  336. * MODULE END
  337. *
  338. * Note(s) : (1) See 'lib_str.h MODULE'.
  339. *********************************************************************************************************
  340. */
  341. #endif /* End of lib str module include. */