tools.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef _PCITOOL_TOOLS_H
  2. #define _PCITOOL_TOOLS_H
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. #include <pcilib.h>
  6. #define BIT_MASK(bits) ((1ll << (bits)) - 1)
  7. #define min2(a, b) (((a)<(b))?(a):(b))
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /**
  12. * Check if provided string is a decimal integer
  13. * @param[in] str - string to check
  14. * @return - 1 if string is a number and 0 - otherwise
  15. */
  16. int pcilib_isnumber(const char *str);
  17. /**
  18. * Check if provided string is a hexdecimal integer, optionally prefexed with 0x
  19. * @param[in] str - string to check
  20. * @return - 1 if string is a number and 0 - otherwise
  21. */
  22. int pcilib_isxnumber(const char *str);
  23. /**
  24. * Check if first \p len bytes of the provided string is a decimal integer
  25. * @param[in] str - string to check
  26. * @param[in] len - size of the string
  27. * @return - 1 if string is a number and 0 - otherwise
  28. */
  29. int pcilib_isnumber_n(const char *str, size_t len);
  30. /**
  31. * Check if first \p len bytes of the provided string is a hexdecimal integer, optionally prefexed with 0x
  32. * @param[in] str - string to check
  33. * @param[in] len - size of the string
  34. * @return - 1 if string is a number and 0 - otherwise
  35. */
  36. int pcilib_isxnumber_n(const char *str, size_t len);
  37. /**
  38. * Change the endianess of the provided number (between big- and little-endian format)
  39. * @param[in] x - number in little/big endian format
  40. * @return - number in big/little endian format
  41. */
  42. uint16_t pcilib_swap16(uint16_t x);
  43. /**
  44. * Change the endianess of the provided number (between big- and little-endian format)
  45. * @param[in] x - number in little/big endian format
  46. * @return - number in big/little endian format
  47. */
  48. uint32_t pcilib_swap32(uint32_t x);
  49. /**
  50. * Change the endianess of the provided number (between big- and little-endian format)
  51. * @param[in] x - number in little/big endian format
  52. * @return - number in big/little endian format
  53. */
  54. uint64_t pcilib_swap64(uint64_t x);
  55. /**
  56. * Change the endianess of the provided array
  57. * @param[out] dst - the destination memory region, can be equal to \p src
  58. * @param[in] src - the source memory region
  59. * @param[in] access - the size of word in bytes (1, 2, 4, or 8)
  60. * @param[in] n - the number of words to copy (\p n * \p access bytes are copied).
  61. */
  62. void pcilib_swap(void *dst, void *src, size_t access, size_t n);
  63. #ifdef __cplusplus
  64. }
  65. #endif
  66. #endif /* _PCITOOL_TOOS_H */