A tool for adding anime to your anidb list.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

65 行
1.5KB

  1. #define _XOPEN_SOURCE
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "util.h"
  6. void util_byte2hex(const uint8_t* bytes, size_t bytes_len,
  7. bool uppercase, char* out)
  8. {
  9. const char* hex = (uppercase) ? "0123456789ABCDEF" : "0123456789abcdef";
  10. for (size_t i = 0; i < bytes_len; i++) {
  11. *out++ = hex[bytes[i] >> 4];
  12. *out++ = hex[bytes[i] & 0xF];
  13. }
  14. *out = '\0';
  15. }
  16. const char *util_get_home()
  17. {
  18. const char *home_env = getenv("HOME");
  19. return home_env; /* TODO this can be null, use other methods as fallback */
  20. }
  21. char *util_basename(const char *fullpath)
  22. {
  23. char *name_part = strrchr(fullpath, '/');
  24. if (name_part)
  25. name_part++;
  26. else
  27. name_part = (char*)fullpath;
  28. return name_part;
  29. }
  30. uint64_t util_timespec_diff(const struct timespec *past,
  31. const struct timespec *future)
  32. {
  33. int64_t sdiff = future->tv_sec - past->tv_sec;
  34. int64_t nsdiff = future->tv_nsec - past->tv_nsec;
  35. return sdiff * 1000 + (nsdiff / 1000000);
  36. }
  37. uint64_t util_iso2unix(const char *isotime)
  38. {
  39. struct tm tm = {0};
  40. char *ms = strptime(isotime, "%Y-%m-%d", &tm);
  41. if (!ms || (*ms != '\0' && *ms != ' ' && *ms != 'T'))
  42. return 0;
  43. if (*ms == '\0')
  44. ms = "T00:00:00";
  45. ms = strptime(ms + 1, "%H:%M", &tm);
  46. if (!ms || (*ms != '\0' && *ms != ':'))
  47. return 0;
  48. if (*ms == '\0')
  49. ms = ":00";
  50. ms = strptime(ms + 1, "%S", &tm);
  51. if (!ms)
  52. return 0;
  53. return mktime(&tm);
  54. }