resume_parse.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <ctype.h>
  2. #ifdef _DEBUG
  3. #include <stdio.h>
  4. #endif
  5. #include <string.h>
  6. #include "resume_parse.h"
  7. static void
  8. ord_insert(const struct field_data *src_arr, struct field_data *out_arr,
  9. unsigned n)
  10. {
  11. int elems = 1;
  12. out_arr[0] = src_arr[0];
  13. int j;
  14. for (unsigned i = 1; i < n; ++ i) {
  15. for (j = 0; j < elems; ++ j)
  16. if (src_arr[i].data_start < out_arr[j].data_start)
  17. break;
  18. if (j != elems)
  19. memmove(out_arr + j + 1, out_arr + j, (elems - j) *
  20. sizeof(struct field_data));
  21. out_arr[j] = src_arr[i];
  22. elems ++;
  23. }
  24. }
  25. static void
  26. strip_char_array(char **left, char **right)
  27. {
  28. char *lp = *left;
  29. char *rp = *right;
  30. if (lp == rp)
  31. return;
  32. while (isspace(*lp)) {
  33. ++ lp;
  34. if (lp == rp)
  35. return;
  36. }
  37. while (isspace(*rp))
  38. -- rp;
  39. *left = lp;
  40. *right = rp;
  41. }
  42. enum search_result
  43. keys_search(const char *markup, struct key_search *query)
  44. {
  45. for (int ck = 0; ck < query->k_count; ++ ck) {
  46. query->fld_srch[ck].data_start =
  47. strstr(markup, query->key_names[ck]);
  48. if (!query->fld_srch[ck].data_start) {
  49. #ifdef _DEBUG
  50. fprintf(stderr, "warning: key \"%s\" not found\n",
  51. query->key_names[ck]);
  52. #endif
  53. return SRCH_NO_KEY;
  54. } else {
  55. query->fld_srch[ck].key_id = ck;
  56. }
  57. query->fld_srch[ck].data_end = NULL;
  58. }
  59. ord_insert(query->fld_srch, query->sorted, query->k_count);
  60. for (int k = 0; k < query->k_count - 1; ++ k) {
  61. query->sorted[k].data_start +=
  62. strlen(query->key_names[query->sorted[k].key_id]);
  63. query->sorted[k].data_end =
  64. query->sorted[k + 1].data_start - 1;
  65. strip_char_array(&(query->sorted[k].data_start),
  66. &(query->sorted[k].data_end));
  67. }
  68. query->sorted[query->k_count - 1].data_start +=
  69. strlen(query->key_names[query->sorted[query->k_count - 1].key_id]);
  70. query->sorted[query->k_count - 1].data_end =
  71. strchr(query->sorted[query->k_count - 1].data_start, '\0') - 1;
  72. strip_char_array(&(query->sorted[query->k_count - 1].data_start),
  73. &(query->sorted[query->k_count - 1].data_end));
  74. for (int i = 0; i < query->k_count; ++ i) {
  75. query->res_dict[query->sorted[i].key_id].beginptr =
  76. query->sorted[i].data_start;
  77. query->res_dict[query->sorted[i].key_id].endptr =
  78. query->sorted[i].data_end;
  79. }
  80. return SRCH_DONE;
  81. }