/src/rotz-search.c

https://bitbucket.org/hroptatyr/rotz · C · 122 lines · 68 code · 13 blank · 41 comment · 6 complexity · 24a8cce7ac6ee7b8ac3a2c8c78d85c7e MD5 · raw file

  1. /*** rotz-search.c -- rotz tag search
  2. *
  3. * Copyright (C) 2013-2014 Sebastian Freundt
  4. *
  5. * Author: Sebastian Freundt <freundt@ga-group.nl>
  6. *
  7. * This file is part of rotz.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * 3. Neither the name of the author nor the names of any contributors
  21. * may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
  25. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  33. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  34. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ***/
  37. #if defined HAVE_CONFIG_H
  38. # include "config.h"
  39. #endif /* HAVE_CONFIG_H */
  40. #include <stdlib.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include "rotz.h"
  45. #include "rotz-cmd-api.h"
  46. #include "rotz-umb.h"
  47. #include "nifty.h"
  48. struct iter_clo_s {
  49. rotz_t ctx;
  50. rtz_const_buf_t prfx;
  51. size_t ntop;
  52. unsigned int show_numbers;
  53. };
  54. static int
  55. iter_cb(rtz_const_buf_t k, rtz_const_buf_t v, void *clo)
  56. {
  57. static size_t iter;
  58. const struct iter_clo_s *cp = clo;
  59. /* just print the name and stats */
  60. fputs(rotz_massage_name(k.d), stdout);
  61. if (cp->show_numbers) {
  62. /* get the number also */
  63. const rtz_vtx_t *vp = (const void*)v.d;
  64. fputc('\t', stdout);
  65. fprintf(stdout, "%zu", rotz_get_nedges(cp->ctx, *vp));
  66. }
  67. fputc('\n', stdout);
  68. iter++;
  69. return 0;
  70. }
  71. static void
  72. iter(rotz_t ctx, struct iter_clo_s *clo)
  73. {
  74. rotz_iter(ctx, clo->prfx, iter_cb, clo);
  75. return;
  76. }
  77. #if defined STANDALONE
  78. int
  79. rotz_cmd_search(const struct yuck_cmd_search_s argi[static 1U])
  80. {
  81. static struct iter_clo_s clo[1];
  82. rotz_t ctx;
  83. if (argi->nargs < 1) {
  84. fputs("Error: need a search string\n", stderr);
  85. return 1;
  86. }
  87. if (UNLIKELY((ctx = make_rotz(db)) == NULL)) {
  88. fputs("Error opening rotz datastore\n", stderr);
  89. return 1;
  90. }
  91. /* cloud all tags mode, undocumented prefix feature */
  92. clo->ctx = ctx;
  93. clo->prfx.d = rotz_tag(argi->args[0U]);
  94. clo->prfx.z = strlen(clo->prfx.d);
  95. clo->show_numbers = 1;
  96. if (argi->top_arg) {
  97. clo->ntop = strtoul(argi->top_arg, NULL, 0);
  98. } else {
  99. clo->ntop = -1UL;
  100. }
  101. iter(ctx, clo);
  102. /* big rcource freeing */
  103. free_rotz(ctx);
  104. return 0;
  105. }
  106. #endif /* STANDALONE */
  107. /* rotz-search.c ends here */