lite-utils.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2017-2019 Tencent Group. All rights reserved.
  3. * License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /**
  19. * Edit by shockcao@tencent.com 2018/3/15
  20. */
  21. #ifndef __LITE_UTILS_H__
  22. #define __LITE_UTILS_H__
  23. #include <stdarg.h>
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #if defined(_PLATFORM_IS_LINUX_)
  30. #include <assert.h>
  31. #endif
  32. #include "lite-list.h"
  33. #include "qcloud_iot_import.h"
  34. #define LITE_TRUE (1)
  35. #define LITE_FALSE (0)
  36. #ifndef container_of
  37. #define container_of(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))
  38. #endif
  39. #define LITE_MINIMUM(a, b) (((a) <= (b)) ? (a) : (b))
  40. #define LITE_MAXIMUM(a, b) (((a) >= (b)) ? (a) : (b))
  41. #define LITE_isdigit(c) (((c) <= '9' && (c) >= '0') ? (LITE_TRUE) : (LITE_FALSE))
  42. #if defined(_PLATFORM_IS_LINUX_)
  43. #define LITE_ASSERT(expr) assert(expr)
  44. #else
  45. #define LITE_ASSERT(expr) \
  46. do { \
  47. if (!(expr)) { \
  48. HAL_Printf("### %s | %s(%d): ASSERT FAILED ###: %s is FALSE\r\n", __FILE__, __func__, __LINE__, #expr); \
  49. } \
  50. } while (0)
  51. #endif
  52. char *LITE_strdup(const char *src);
  53. char *LITE_format_string(const char *fmt, ...);
  54. char *LITE_format_nstring(const int len, const char *fmt, ...);
  55. void LITE_hexbuf_convert(unsigned char *digest, char *out, int buflen, int uppercase);
  56. void LITE_hexstr_convert(char *hexstr, uint8_t *out_buf, int len);
  57. void LITE_replace_substr(char orig[], char key[], char swap[]);
  58. void LITE_str_strip_char(char *src, char destCh);
  59. char * LITE_json_value_of(char *key, char *src);
  60. list_head_t *LITE_json_keys_of(char *src, char *prefix);
  61. void LITE_json_keys_release(list_head_t *keylist);
  62. char * LITE_json_string_value_strip_transfer(char *key, char *src);
  63. int LITE_get_int32(int32_t *value, char *src);
  64. int LITE_get_int16(int16_t *value, char *src);
  65. int LITE_get_int8(int8_t *value, char *src);
  66. int LITE_get_uint32(uint32_t *value, char *src);
  67. int LITE_get_uint16(uint16_t *value, char *src);
  68. int LITE_get_uint8(uint8_t *value, char *src);
  69. int LITE_get_float(float *value, char *src);
  70. int LITE_get_double(double *value, char *src);
  71. int LITE_get_boolean(bool *value, char *src);
  72. int LITE_get_string(int8_t *value, char *src, uint16_t max_len);
  73. typedef struct _json_key_t {
  74. char * key;
  75. list_head_t list;
  76. } json_key_t;
  77. #define foreach_json_keys_in(src, iter_key, keylist, pos) \
  78. for (keylist = (void *)LITE_json_keys_of((char *)src, ""), \
  79. pos = (void *)list_first_entry((list_head_t *)keylist, json_key_t, list), iter_key = ((json_key_t *)pos)->key; \
  80. (iter_key = ((json_key_t *)pos)->key); pos = list_next_entry((json_key_t *)pos, list, json_key_t))
  81. #endif /* __LITE_UTILS_H__ */