commands.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "../include/commands.hpp"
  2. #include <cstdio>
  3. CMD commands[]{
  4. {CMD_HELP, "help", "show help"},
  5. {CMD_STATUS, "status", "query status of IP"},
  6. {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
  7. {CMD_PUT , "put", "upload file to IP and add to queue"},
  8. {CMD_GET , "get", "retrieve file from IP"},
  9. /* TODO
  10. {CMD_REMOVE , "remove", "remove file from IP and queue (stops xfer
  11. if required)"},
  12. {CMD_SETUP , "setup", "configure server at IP"},
  13. {CMD_LOG , "log", "show log from IP"}
  14. */
  15. };
  16. COMMANDID getCmdIdFromString(const char *str) {
  17. COMMANDID ret = CMD_UNKNOWN;
  18. char temp[COMMANDLEN + 1] = {0};
  19. int len = COMMANDLEN;
  20. if (std::strlen(str) > COMMANDLEN) {
  21. return ret;
  22. }
  23. else len = std::strlen(str);
  24. for (int i = 0; i < len; i++)
  25. temp[i] = std::tolower(str[i]);
  26. for (int i = 0; i < sizeofarr(commands); i++) {
  27. if (!std::strncmp(temp, commands[i].name, COMMANDLEN))
  28. ret = commands[i].cmd;
  29. }
  30. return ret;
  31. }
  32. const char *getCmdStringFromId(COMMANDID id) {
  33. const char *ret = NULL;
  34. for (int i = 0; i < sizeofarr(commands); i++) {
  35. if (commands[i].cmd == id)
  36. ret = commands[i].name;
  37. }
  38. return ret;
  39. }
  40. void printCmds(void) {
  41. for(CMD command : commands) {
  42. printf("%-12s - %s\n", command.name, command.desc);
  43. }
  44. }