args.c 2.0 KB

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