args.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "args.h"
  2. size_t args_get_first_word_length(string_t args) {
  3. size_t ws = string_search_char(args, ' ');
  4. if(ws == STRING_FAILURE) {
  5. ws = strlen(string_get_cstr(args));
  6. }
  7. return ws;
  8. }
  9. size_t args_length(string_t args) {
  10. return strlen(string_get_cstr(args));
  11. }
  12. bool args_read_string_and_trim(string_t args, string_t word) {
  13. size_t cmd_length = args_get_first_word_length(args);
  14. if(cmd_length == 0) {
  15. return false;
  16. }
  17. string_set_n(word, args, 0, cmd_length);
  18. string_right(args, cmd_length);
  19. string_strim(args);
  20. return true;
  21. }
  22. bool args_char_to_hex_nibble(char c, uint8_t* nibble) {
  23. if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
  24. if((c >= '0' && c <= '9')) {
  25. *nibble = c - '0';
  26. } else if((c >= 'A' && c <= 'F')) {
  27. *nibble = c - 'A' + 10;
  28. } else {
  29. *nibble = c - 'a' + 10;
  30. }
  31. return true;
  32. } else {
  33. return false;
  34. }
  35. }
  36. bool args_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte) {
  37. uint8_t hi_nibble_value = 0;
  38. uint8_t low_nibble_value = 0;
  39. bool result = false;
  40. if(args_char_to_hex_nibble(hi_nibble, &hi_nibble_value)) {
  41. if(args_char_to_hex_nibble(low_nibble, &low_nibble_value)) {
  42. result = true;
  43. *byte = (hi_nibble_value << 4) | low_nibble_value;
  44. }
  45. }
  46. return result;
  47. }
  48. bool args_read_hex_bytes(string_t args, uint8_t* bytes, uint8_t bytes_count) {
  49. bool result = true;
  50. const char* str_pointer = string_get_cstr(args);
  51. if(args_get_first_word_length(args) == (bytes_count * 2)) {
  52. for(uint8_t i = 0; i < bytes_count; i++) {
  53. if(!args_char_to_hex(str_pointer[i * 2], str_pointer[i * 2 + 1], &(bytes[i]))) {
  54. result = false;
  55. break;
  56. }
  57. }
  58. } else {
  59. result = false;
  60. }
  61. return result;
  62. }