Rename mu_locus_tracker to mu_linetrack
Showing
8 changed files
with
72 additions
and
64 deletions
... | @@ -17,13 +17,14 @@ struct mu_locus_range | ... | @@ -17,13 +17,14 @@ struct mu_locus_range |
17 | struct mu_locus_point end; | 17 | struct mu_locus_point end; |
18 | }; | 18 | }; |
19 | 19 | ||
20 | typedef struct mu_locus_track *mu_locus_track_t; | 20 | typedef struct mu_linetrack *mu_linetrack_t; |
21 | 21 | ||
22 | struct mu_locus_track_stat | 22 | struct mu_linetrack_stat |
23 | { | 23 | { |
24 | unsigned start_line; | 24 | unsigned start_line; /* Start line number (1-based) */ |
25 | size_t n_lines; | 25 | size_t n_lines; /* Number of lines, including the recent (incomplete) |
26 | size_t n_chars; | 26 | one */ |
27 | size_t n_chars; /* Total number of characters */ | ||
27 | }; | 28 | }; |
28 | 29 | ||
29 | int mu_ident_ref (char const *name, char const **refname); | 30 | int mu_ident_ref (char const *name, char const **refname); |
... | @@ -47,17 +48,15 @@ mu_locus_point_same_line (struct mu_locus_point const *a, | ... | @@ -47,17 +48,15 @@ mu_locus_point_same_line (struct mu_locus_point const *a, |
47 | void mu_lrange_debug (struct mu_locus_range const *loc, | 48 | void mu_lrange_debug (struct mu_locus_range const *loc, |
48 | char const *fmt, ...); | 49 | char const *fmt, ...); |
49 | 50 | ||
50 | int mu_locus_track_create (mu_locus_track_t *ret, | 51 | int mu_linetrack_create (mu_linetrack_t *ret, |
51 | char const *file_name, size_t max_lines); | 52 | char const *file_name, size_t max_lines); |
52 | void mu_locus_track_free (mu_locus_track_t trk); | 53 | void mu_linetrack_free (mu_linetrack_t trk); |
53 | void mu_locus_track_destroy (mu_locus_track_t *trk); | 54 | void mu_linetrack_destroy (mu_linetrack_t *trk); |
54 | size_t mu_locus_track_level (mu_locus_track_t trk); | 55 | void mu_linetrack_advance (mu_linetrack_t trk, |
55 | void mu_locus_tracker_advance (struct mu_locus_track *trk, | ||
56 | struct mu_locus_range *loc, | 56 | struct mu_locus_range *loc, |
57 | char const *text, size_t leng); | 57 | char const *text, size_t leng); |
58 | int mu_locus_tracker_retreat (struct mu_locus_track *trk, size_t n); | 58 | int mu_linetrack_retreat (mu_linetrack_t trk, size_t n); |
59 | int mu_locus_tracker_stat (struct mu_locus_track *trk, | 59 | int mu_linetrack_stat (mu_linetrack_t trk, struct mu_linetrack_stat *st); |
60 | struct mu_locus_track_stat *st); | ||
61 | 60 | ||
62 | 61 | ||
63 | void mu_stream_print_locus_range (mu_stream_t stream, | 62 | void mu_stream_print_locus_range (mu_stream_t stream, | ... | ... |
... | @@ -20,7 +20,7 @@ noinst_LTLIBRARIES = liblocus.la | ... | @@ -20,7 +20,7 @@ noinst_LTLIBRARIES = liblocus.la |
20 | liblocus_la_SOURCES = \ | 20 | liblocus_la_SOURCES = \ |
21 | ident.c\ | 21 | ident.c\ |
22 | debug.c\ | 22 | debug.c\ |
23 | tracker.c | 23 | linetrack.c |
24 | 24 | ||
25 | AM_CPPFLAGS = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils | 25 | AM_CPPFLAGS = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils |
26 | 26 | ... | ... |
... | @@ -4,22 +4,36 @@ | ... | @@ -4,22 +4,36 @@ |
4 | #include <mailutils/locus.h> | 4 | #include <mailutils/locus.h> |
5 | #include <mailutils/error.h> | 5 | #include <mailutils/error.h> |
6 | 6 | ||
7 | struct mu_locus_track | 7 | /* The line-tracker structure keeps track of the last N lines read from a |
8 | text input file. For each line read it keeps the number of characters | ||
9 | in that line including the newline. This information is stored in a | ||
10 | syclic stack of N elements. Top of stack always represents the current | ||
11 | line. For the purpose of line tracker, current line is the line that is | ||
12 | being visited, such that its final newline character has not yet been | ||
13 | seen. Once the newline is seen, the line is pushed on stack, and a new | ||
14 | current line is assumed. | ||
15 | |||
16 | The value of N must not be less than 2. | ||
17 | */ | ||
18 | struct mu_linetrack | ||
8 | { | 19 | { |
9 | char const *file_name; /* Name of the source file */ | 20 | char const *file_name; /* Name of the source file */ |
10 | size_t max_lines; /* Max. number of lines history kept by tracker */ | 21 | size_t max_lines; /* Max. number of lines history kept by tracker (N) */ |
11 | size_t head; /* Bottom of stack */ | 22 | size_t head; /* Index of the eldest element on stack */ |
12 | size_t level; /* Number of elements on stack */ | 23 | size_t tos; /* Index of the most recent element on stack |
24 | (< max_lines) */ | ||
13 | unsigned hline; /* Number of line corresponding to cols[head] */ | 25 | unsigned hline; /* Number of line corresponding to cols[head] */ |
14 | unsigned *cols; /* Cyclic stack */ | 26 | unsigned *cols; /* Cyclic stack or character counts. |
27 | Number of characters in line (hline + n) is | ||
28 | cols[head + n] (0 <= n <= tos). */ | ||
15 | }; | 29 | }; |
16 | 30 | ||
17 | int | 31 | int |
18 | mu_locus_track_create (mu_locus_track_t *ret, | 32 | mu_linetrack_create (mu_linetrack_t *ret, |
19 | char const *file_name, size_t max_lines) | 33 | char const *file_name, size_t max_lines) |
20 | { | 34 | { |
21 | int rc; | 35 | int rc; |
22 | struct mu_locus_track *trk; | 36 | struct mu_linetrack *trk; |
23 | 37 | ||
24 | trk = malloc (sizeof *trk); | 38 | trk = malloc (sizeof *trk); |
25 | if (!trk) | 39 | if (!trk) |
... | @@ -43,7 +57,7 @@ mu_locus_track_create (mu_locus_track_t *ret, | ... | @@ -43,7 +57,7 @@ mu_locus_track_create (mu_locus_track_t *ret, |
43 | max_lines = 2; | 57 | max_lines = 2; |
44 | trk->max_lines = max_lines; | 58 | trk->max_lines = max_lines; |
45 | trk->head = 0; | 59 | trk->head = 0; |
46 | trk->level = 0; | 60 | trk->tos = 0; |
47 | trk->hline = 1; | 61 | trk->hline = 1; |
48 | trk->cols[0] = 0; | 62 | trk->cols[0] = 0; |
49 | 63 | ||
... | @@ -52,7 +66,7 @@ mu_locus_track_create (mu_locus_track_t *ret, | ... | @@ -52,7 +66,7 @@ mu_locus_track_create (mu_locus_track_t *ret, |
52 | } | 66 | } |
53 | 67 | ||
54 | void | 68 | void |
55 | mu_locus_track_free (mu_locus_track_t trk) | 69 | mu_linetrack_free (mu_linetrack_t trk) |
56 | { | 70 | { |
57 | if (trk) | 71 | if (trk) |
58 | { | 72 | { |
... | @@ -63,54 +77,48 @@ mu_locus_track_free (mu_locus_track_t trk) | ... | @@ -63,54 +77,48 @@ mu_locus_track_free (mu_locus_track_t trk) |
63 | } | 77 | } |
64 | 78 | ||
65 | void | 79 | void |
66 | mu_locus_track_destroy (mu_locus_track_t *trk) | 80 | mu_linetrack_destroy (mu_linetrack_t *trk) |
67 | { | 81 | { |
68 | if (trk) | 82 | if (trk) |
69 | { | 83 | { |
70 | mu_locus_track_free (*trk); | 84 | mu_linetrack_free (*trk); |
71 | *trk = NULL; | 85 | *trk = NULL; |
72 | } | 86 | } |
73 | } | 87 | } |
74 | 88 | ||
75 | size_t | ||
76 | mu_locus_track_level (mu_locus_track_t trk) | ||
77 | { | ||
78 | return trk->level; | ||
79 | } | ||
80 | |||
81 | static inline unsigned * | 89 | static inline unsigned * |
82 | cols_tos_ptr (mu_locus_track_t trk) | 90 | cols_tos_ptr (mu_linetrack_t trk) |
83 | { | 91 | { |
84 | return &trk->cols[(trk->head + trk->level) % trk->max_lines]; | 92 | return &trk->cols[(trk->head + trk->tos) % trk->max_lines]; |
85 | } | 93 | } |
86 | 94 | ||
87 | static inline unsigned | 95 | static inline unsigned |
88 | cols_peek (mu_locus_track_t trk, size_t n) | 96 | cols_peek (mu_linetrack_t trk, size_t n) |
89 | { | 97 | { |
90 | return trk->cols[(trk->head + n) % trk->max_lines]; | 98 | return trk->cols[(trk->head + n) % trk->max_lines]; |
91 | } | 99 | } |
92 | 100 | ||
93 | static inline unsigned * | 101 | static inline unsigned * |
94 | push (mu_locus_track_t trk) | 102 | push (mu_linetrack_t trk) |
95 | { | 103 | { |
96 | unsigned *ptr; | 104 | unsigned *ptr; |
97 | if (trk->level == trk->max_lines) | 105 | if (trk->tos == trk->max_lines - 1) |
98 | { | 106 | { |
99 | trk->head++; | 107 | trk->head++; |
100 | trk->hline++; | 108 | trk->hline++; |
101 | } | 109 | } |
102 | else | 110 | else |
103 | trk->level++; | 111 | trk->tos++; |
104 | *(ptr = cols_tos_ptr (trk)) = 0; | 112 | *(ptr = cols_tos_ptr (trk)) = 0; |
105 | return ptr; | 113 | return ptr; |
106 | } | 114 | } |
107 | 115 | ||
108 | static inline unsigned * | 116 | static inline unsigned * |
109 | pop (mu_locus_track_t trk) | 117 | pop (mu_linetrack_t trk) |
110 | { | 118 | { |
111 | if (trk->level == 0) | 119 | if (trk->tos == 0) |
112 | return NULL; | 120 | return NULL; |
113 | trk->level--; | 121 | trk->tos--; |
114 | return cols_tos_ptr (trk); | 122 | return cols_tos_ptr (trk); |
115 | } | 123 | } |
116 | 124 | ||
... | @@ -119,12 +127,11 @@ pop (mu_locus_track_t trk) | ... | @@ -119,12 +127,11 @@ pop (mu_locus_track_t trk) |
119 | #endif | 127 | #endif |
120 | 128 | ||
121 | int | 129 | int |
122 | mu_locus_tracker_stat (struct mu_locus_track *trk, | 130 | mu_linetrack_stat (struct mu_linetrack *trk, struct mu_linetrack_stat *st) |
123 | struct mu_locus_track_stat *st) | ||
124 | { | 131 | { |
125 | size_t i, nch = 0; | 132 | size_t i, nch = 0; |
126 | 133 | ||
127 | for (i = 0; i <= trk->level; i++) | 134 | for (i = 0; i <= trk->tos; i++) |
128 | { | 135 | { |
129 | unsigned n = cols_peek (trk, i); | 136 | unsigned n = cols_peek (trk, i); |
130 | if (SIZE_MAX - nch < n) | 137 | if (SIZE_MAX - nch < n) |
... | @@ -133,12 +140,14 @@ mu_locus_tracker_stat (struct mu_locus_track *trk, | ... | @@ -133,12 +140,14 @@ mu_locus_tracker_stat (struct mu_locus_track *trk, |
133 | } | 140 | } |
134 | 141 | ||
135 | st->start_line = trk->hline; | 142 | st->start_line = trk->hline; |
136 | st->n_lines = trk->level; | 143 | st->n_lines = trk->tos + 1; |
137 | st->n_chars = nch; | 144 | st->n_chars = nch; |
145 | |||
146 | return 0; | ||
138 | } | 147 | } |
139 | 148 | ||
140 | void | 149 | void |
141 | mu_locus_tracker_advance (struct mu_locus_track *trk, | 150 | mu_linetrack_advance (struct mu_linetrack *trk, |
142 | struct mu_locus_range *loc, | 151 | struct mu_locus_range *loc, |
143 | char const *text, size_t leng) | 152 | char const *text, size_t leng) |
144 | { | 153 | { |
... | @@ -148,7 +157,7 @@ mu_locus_tracker_advance (struct mu_locus_track *trk, | ... | @@ -148,7 +157,7 @@ mu_locus_tracker_advance (struct mu_locus_track *trk, |
148 | return; | 157 | return; |
149 | 158 | ||
150 | loc->beg.mu_file = loc->end.mu_file = trk->file_name; | 159 | loc->beg.mu_file = loc->end.mu_file = trk->file_name; |
151 | loc->beg.mu_line = trk->hline + trk->level; | 160 | loc->beg.mu_line = trk->hline + trk->tos; |
152 | ptr = cols_tos_ptr (trk); | 161 | ptr = cols_tos_ptr (trk); |
153 | loc->beg.mu_col = *ptr + 1; | 162 | loc->beg.mu_col = *ptr + 1; |
154 | while (leng--) | 163 | while (leng--) |
... | @@ -160,14 +169,14 @@ mu_locus_tracker_advance (struct mu_locus_track *trk, | ... | @@ -160,14 +169,14 @@ mu_locus_tracker_advance (struct mu_locus_track *trk, |
160 | } | 169 | } |
161 | if (*ptr) | 170 | if (*ptr) |
162 | { | 171 | { |
163 | loc->end.mu_line = trk->hline + trk->level; | 172 | loc->end.mu_line = trk->hline + trk->tos; |
164 | loc->end.mu_col = *ptr; | 173 | loc->end.mu_col = *ptr; |
165 | } | 174 | } |
166 | else | 175 | else |
167 | { | 176 | { |
168 | /* Text ends with a newline. Keep the previos line number. */ | 177 | /* Text ends with a newline. Keep the previos line number. */ |
169 | loc->end.mu_line = trk->hline + trk->level - 1; | 178 | loc->end.mu_line = trk->hline + trk->tos - 1; |
170 | loc->end.mu_col = cols_peek (trk, trk->level - 1) - 1; | 179 | loc->end.mu_col = cols_peek (trk, trk->tos - 1) - 1; |
171 | if (loc->end.mu_col + 1 == loc->beg.mu_col) | 180 | if (loc->end.mu_col + 1 == loc->beg.mu_col) |
172 | { | 181 | { |
173 | /* This happens if the previous line contained only newline. */ | 182 | /* This happens if the previous line contained only newline. */ |
... | @@ -177,11 +186,11 @@ mu_locus_tracker_advance (struct mu_locus_track *trk, | ... | @@ -177,11 +186,11 @@ mu_locus_tracker_advance (struct mu_locus_track *trk, |
177 | } | 186 | } |
178 | 187 | ||
179 | int | 188 | int |
180 | mu_locus_tracker_retreat (struct mu_locus_track *trk, size_t n) | 189 | mu_linetrack_retreat (struct mu_linetrack *trk, size_t n) |
181 | { | 190 | { |
182 | struct mu_locus_track_stat st; | 191 | struct mu_linetrack_stat st; |
183 | 192 | ||
184 | mu_locus_tracker_stat (trk, &st); | 193 | mu_linetrack_stat (trk, &st); |
185 | if (n > st.n_chars) | 194 | if (n > st.n_chars) |
186 | return ERANGE; | 195 | return ERANGE; |
187 | else | 196 | else | ... | ... |
... | @@ -54,7 +54,7 @@ noinst_PROGRAMS = \ | ... | @@ -54,7 +54,7 @@ noinst_PROGRAMS = \ |
54 | globtest\ | 54 | globtest\ |
55 | imapio\ | 55 | imapio\ |
56 | listop\ | 56 | listop\ |
57 | loctrack\ | 57 | linetrack\ |
58 | logstr\ | 58 | logstr\ |
59 | mailcap\ | 59 | mailcap\ |
60 | mimehdr\ | 60 | mimehdr\ |
... | @@ -107,7 +107,7 @@ TESTSUITE_AT = \ | ... | @@ -107,7 +107,7 @@ TESTSUITE_AT = \ |
107 | inline-comment.at\ | 107 | inline-comment.at\ |
108 | linecon.at\ | 108 | linecon.at\ |
109 | list.at\ | 109 | list.at\ |
110 | loctrack.at\ | 110 | linetrack.at\ |
111 | logstr.at\ | 111 | logstr.at\ |
112 | mailcap.at\ | 112 | mailcap.at\ |
113 | mimehdr.at\ | 113 | mimehdr.at\ | ... | ... |
... | @@ -14,12 +14,12 @@ | ... | @@ -14,12 +14,12 @@ |
14 | # You should have received a copy of the GNU General Public License | 14 | # You should have received a copy of the GNU General Public License |
15 | # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. | 15 | # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | ||
17 | AT_BANNER([Locus tracker]) | 17 | AT_BANNER([Line tracker]) |
18 | 18 | ||
19 | m4_pushdef([TRACKTEST],[ | 19 | m4_pushdef([TRACKTEST],[ |
20 | AT_SETUP([$1]) | 20 | AT_SETUP([$1]) |
21 | AT_KEYWORDS([tracker $2]) | 21 | AT_KEYWORDS([tracker $2]) |
22 | AT_CHECK([loctrack liber $3 <<EOT | 22 | AT_CHECK([linetrack liber $3 <<EOT |
23 | $4[]EOT | 23 | $4[]EOT |
24 | ], | 24 | ], |
25 | [0], | 25 | [0], |
... | @@ -103,7 +103,7 @@ three | ... | @@ -103,7 +103,7 @@ three |
103 | liber:2.1-3: two\n | 103 | liber:2.1-3: two\n |
104 | liber:3.1-5: three | 104 | liber:3.1-5: three |
105 | ], | 105 | ], |
106 | [loctrack: retreat count too big | 106 | [linetrack: retreat count too big |
107 | ]) | 107 | ]) |
108 | 108 | ||
109 | m4_popdef([TRACKTEST]) | 109 | m4_popdef([TRACKTEST]) | ... | ... |
... | @@ -6,7 +6,7 @@ main (int argc, char **argv) | ... | @@ -6,7 +6,7 @@ main (int argc, char **argv) |
6 | { | 6 | { |
7 | unsigned long max_lines; | 7 | unsigned long max_lines; |
8 | char *end; | 8 | char *end; |
9 | mu_locus_track_t trk; | 9 | mu_linetrack_t trk; |
10 | int rc; | 10 | int rc; |
11 | char *buf = NULL; | 11 | char *buf = NULL; |
12 | size_t size, n; | 12 | size_t size, n; |
... | @@ -26,7 +26,7 @@ main (int argc, char **argv) | ... | @@ -26,7 +26,7 @@ main (int argc, char **argv) |
26 | return 1; | 26 | return 1; |
27 | } | 27 | } |
28 | 28 | ||
29 | MU_ASSERT (mu_locus_track_create (&trk, argv[1], max_lines)); | 29 | MU_ASSERT (mu_linetrack_create (&trk, argv[1], max_lines)); |
30 | while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0) | 30 | while ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) == 0 && n > 0) |
31 | { | 31 | { |
32 | struct mu_locus_range lr; | 32 | struct mu_locus_range lr; |
... | @@ -43,17 +43,17 @@ main (int argc, char **argv) | ... | @@ -43,17 +43,17 @@ main (int argc, char **argv) |
43 | mu_error ("bad number"); | 43 | mu_error ("bad number"); |
44 | continue; | 44 | continue; |
45 | } | 45 | } |
46 | rc = mu_locus_tracker_retreat (trk, x); | 46 | rc = mu_linetrack_retreat (trk, x); |
47 | if (rc == ERANGE) | 47 | if (rc == ERANGE) |
48 | mu_error ("retreat count too big"); | 48 | mu_error ("retreat count too big"); |
49 | else if (rc) | 49 | else if (rc) |
50 | mu_diag_funcall (MU_DIAG_ERROR, "mu_locus_tracker_retreat", buf+2, | 50 | mu_diag_funcall (MU_DIAG_ERROR, "mu_linetrack_retreat", buf+2, |
51 | rc); | 51 | rc); |
52 | } | 52 | } |
53 | else | 53 | else |
54 | { | 54 | { |
55 | mu_c_str_unescape (buf, "\\\n", "\\n", &tok); | 55 | mu_c_str_unescape (buf, "\\\n", "\\n", &tok); |
56 | mu_locus_tracker_advance (trk, &lr, tok, strlen (tok)); | 56 | mu_linetrack_advance (trk, &lr, tok, strlen (tok)); |
57 | free (tok); | 57 | free (tok); |
58 | mu_stream_lprintf (mu_strout, &lr, "%s\n", buf); | 58 | mu_stream_lprintf (mu_strout, &lr, "%s\n", buf); |
59 | } | 59 | } | ... | ... |
-
Please register or sign in to post a comment