detect_cotes.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "sh.h"
  2. void init_detect_blocks(t_e *e, char *line)
  3. {
  4. int pos;
  5. int b_pos;
  6. pos = 0;
  7. b_pos = 0;
  8. e->b = (t_block**)malloc(sizeof(t_block*) * 5);
  9. detect_blocks(e, line, pos, b_pos);
  10. }
  11. /*
  12. ** set arbitrary value to 5 blocks max
  13. ** While not scanned all string
  14. ** If found opening and close cotes and non-coted block before:
  15. ** - retrieve coted block
  16. ** Else: no cote found: retrieve non coted block
  17. ** Increment block position
  18. */
  19. void detect_blocks(t_e *e, char *line, int pos, int b_pos)
  20. {
  21. int i;
  22. int coted;
  23. while (line[pos] != '\0')
  24. {
  25. while (line[pos] != '\0' && (line[pos] == ' ' || line[pos] == '\t'))
  26. pos++;
  27. e->b[b_pos] = (t_block*)malloc(sizeof(t_block));
  28. i = -1;
  29. coted = 0;
  30. while (++i < 3)
  31. if (line[pos] == e->par_open[i] && \
  32. ft_strchr(&line[pos + 1], e->par_open[i]) != NULL && \
  33. (b_pos > 0 ? e->b[b_pos - 1]->type == 0 : 1))
  34. {
  35. pos = coted_block(e, line, pos, e->b[b_pos++]);
  36. coted = 1;
  37. }
  38. if (coted == 0)
  39. pos = non_coted_block(e, line, pos, e->b[b_pos++]);
  40. }
  41. e->b[b_pos] = NULL;
  42. }
  43. /*
  44. ** for every coted types ', ", and `:
  45. ** - if there is an open and close coted of same type:
  46. ** - set block type, and begin and end position of the block
  47. ** return closer new position which correspond to end of block
  48. */
  49. int coted_block(t_e *e, char *line, int pos, t_block *blk)
  50. {
  51. int first;
  52. int snd;
  53. int j;
  54. j = -1;
  55. blk->begin = 10000;
  56. while (++j <= 2)
  57. {
  58. first = ft_search_pos_char(&line[pos], e->par_open[j]);
  59. snd = ft_search_pos_char(&line[pos + first + 1], e->par_close[j]);
  60. if (first < blk->begin - pos && first != -1 && snd != -1)
  61. {
  62. blk->type = j + 1;
  63. blk->begin = pos + first;
  64. blk->end = pos + first + snd + 1;
  65. }
  66. }
  67. return (blk->end);
  68. }
  69. /*
  70. ** Set type block to 0, begin position to current position
  71. ** for every cote type:
  72. ** - search for cote type closer from current position:
  73. ** - add end of block position of cote
  74. ** if found no cote
  75. ** return end of block position
  76. */
  77. int non_coted_block(t_e *e, char *line, int pos, t_block *blk)
  78. {
  79. int i;
  80. int end;
  81. blk->type = 0;
  82. blk->begin = pos;
  83. blk->end = 10000;
  84. i = -1;
  85. while (++i <= 2)
  86. {
  87. end = ft_search_pos_char(&line[pos + 1], e->par_close[i]);
  88. if (end != -1 && end < blk->end)
  89. blk->end = pos + end + 1;
  90. }
  91. if (blk->end == 10000)
  92. blk->end = ft_strlen(line);
  93. return (blk->end);
  94. }