argv.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /** @file argv.c
  2. * @brief Functions to parse string to arguments.
  3. */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include "private.h"
  10. #include "faux/faux.h"
  11. #include "faux/str.h"
  12. #include "faux/list.h"
  13. #include "faux/argv.h"
  14. /** @brief Allocates new argv object.
  15. *
  16. * Before working with argument list it must be allocated and initialized.
  17. *
  18. * @return Allocated and initialized argument list or NULL on error.
  19. */
  20. faux_argv_t *faux_argv_new(void)
  21. {
  22. faux_argv_t *fargv = NULL;
  23. fargv = faux_zmalloc(sizeof(*fargv));
  24. assert(fargv);
  25. if (!fargv)
  26. return NULL;
  27. // Init
  28. fargv->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  29. NULL, NULL, (void (*)(void *))faux_str_free);
  30. fargv->quotes = NULL;
  31. return fargv;
  32. }
  33. /** @brief Frees the argv object object.
  34. *
  35. * After using the argv object must be freed. Function frees argv object.
  36. */
  37. void faux_argv_free(faux_argv_t *fargv)
  38. {
  39. assert(fargv);
  40. if (!fargv)
  41. return;
  42. faux_list_free(fargv->list);
  43. faux_str_free(fargv->quotes);
  44. faux_free(fargv);
  45. }
  46. /** @brief Initializes iterator to iterate through the entire argv object.
  47. *
  48. * Before iterating with the faux_argv_each() function the iterator must be
  49. * initialized. This function do it.
  50. *
  51. * @param [in] fargv Allocated and initialized argv object.
  52. * @return Initialized iterator.
  53. * @sa faux_argv_each()
  54. */
  55. faux_argv_node_t *faux_argv_iter(const faux_argv_t *fargv)
  56. {
  57. assert(fargv);
  58. if (!fargv)
  59. return NULL;
  60. return (faux_argv_node_t *)faux_list_head(fargv->list);
  61. }
  62. /** @brief Iterate entire argv object for arguments.
  63. *
  64. * Before iteration the iterator must be initialized by faux_argv_iter()
  65. * function. Doesn't use faux_argv_each() with uninitialized iterator.
  66. *
  67. * On each call function returns string (argument) and modifies iterator.
  68. * Stop iteration when function returns NULL.
  69. *
  70. * @param [in,out] iter Iterator.
  71. * @return String.
  72. * @sa faux_argv_iter()
  73. */
  74. const char *faux_argv_each(faux_argv_node_t **iter)
  75. {
  76. return (const char *)faux_list_each((faux_list_node_t **)iter);
  77. }
  78. void faux_argv_quotes(faux_argv_t *fargv, const char *quotes)
  79. {
  80. assert(fargv);
  81. if (!fargv)
  82. return;
  83. faux_str_free(fargv->quotes);
  84. if (!quotes) {
  85. fargv->quotes = NULL; // No additional quotes
  86. return;
  87. }
  88. fargv->quotes = faux_str_dup(quotes);
  89. }
  90. ssize_t faux_argv_parse_str(faux_argv_t *fargv, const char *str)
  91. {
  92. assert(fargv);
  93. if (!fargv)
  94. return -1;
  95. return 0;
  96. }