lite-utils.h 3.2 KB

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