args.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte) {
  24. uint8_t hi_nibble_value = 0;
  25. uint8_t low_nibble_value = 0;
  26. bool result = false;
  27. if(hex_char_to_hex_nibble(hi_nibble, &hi_nibble_value)) {
  28. if(hex_char_to_hex_nibble(low_nibble, &low_nibble_value)) {
  29. result = true;
  30. *byte = (hi_nibble_value << 4) | low_nibble_value;
  31. }
  32. }
  33. return result;
  34. }
  35. bool args_read_hex_bytes(string_t args, uint8_t* bytes, uint8_t bytes_count) {
  36. bool result = true;
  37. const char* str_pointer = string_get_cstr(args);
  38. if(args_get_first_word_length(args) == (bytes_count * 2)) {
  39. for(uint8_t i = 0; i < bytes_count; i++) {
  40. if(!args_char_to_hex(str_pointer[i * 2], str_pointer[i * 2 + 1], &(bytes[i]))) {
  41. result = false;
  42. break;
  43. }
  44. }
  45. } else {
  46. result = false;
  47. }
  48. return result;
  49. }