diff options
-rw-r--r-- | src/ChangeLog | 9 | ||||
-rw-r--r-- | src/message.c | 41 | ||||
-rw-r--r-- | src/write-po.c | 60 |
3 files changed, 72 insertions, 38 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index d8fa0ea..120b655 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,12 @@ +2001-07-21 Bruno Haible <haible@clisp.cons.org> + + * message.c (message_comment_filepos): Don't keep the filepos[] array + sorted. + * write-po.c (cmp_by_msgid): Renamed from msgid_cmp. + (cmp_filepos, msgdomain_list_sort_filepos): New functions. + (cmp_by_filepos): Renamed from filepos_cmp. + (msgdomain_list_sort_by_filepos): Call msgdomain_list_sort_filepos. + 2001-07-12 Bruno Haible <haible@clisp.cons.org> * msgexec.c: New file. diff --git a/src/message.c b/src/message.c index 96c08f6..c19f1f3 100644 --- a/src/message.c +++ b/src/message.c @@ -150,47 +150,24 @@ message_comment_filepos (mp, name, line) const char *name; size_t line; { + size_t j; size_t nbytes; lex_pos_ty *pp; - int min, max; - int j; - - /* See if we have this position already. They are kept in sorted - order, so use a binary chop. */ - /* FIXME: use bsearch */ - min = 0; - max = (int) mp->filepos_count - 1; - while (min <= max) + + /* See if we have this position already. */ + for (j = 0; j < mp->filepos_count; j++) { - int mid; - int cmp; - - mid = (min + max) / 2; - pp = &mp->filepos[mid]; - cmp = strcmp (pp->file_name, name); - if (cmp == 0) - cmp = (int) pp->line_number - line; - if (cmp == 0) + pp = &mp->filepos[j]; + if (strcmp (pp->file_name, name) == 0 && pp->line_number == line) return; - if (cmp < 0) - min = mid + 1; - else - max = mid - 1; } - /* Extend the list so that we can add an position to it. */ + /* Extend the list so that we can add a position to it. */ nbytes = (mp->filepos_count + 1) * sizeof (mp->filepos[0]); mp->filepos = xrealloc (mp->filepos, nbytes); - /* Shuffle the rest of the list up one, so that we can insert the - position at 'min'. */ - /* FIXME: use memmove */ - for (j = mp->filepos_count; j > min; --j) - mp->filepos[j] = mp->filepos[j - 1]; - mp->filepos_count++; - - /* Insert the postion into the empty slot. */ - pp = &mp->filepos[min]; + /* Insert the position at the end. Don't sort the file positions here. */ + pp = &mp->filepos[mp->filepos_count++]; pp->file_name = xstrdup (name); pp->line_number = line; } diff --git a/src/write-po.c b/src/write-po.c index 05b0df9..f1c9e38 100644 --- a/src/write-po.c +++ b/src/write-po.c @@ -64,8 +64,10 @@ static void message_print PARAMS ((const message_ty *mp, FILE *fp, static void message_print_obsolete PARAMS ((const message_ty *mp, FILE *fp, const char *charset, bool blank_line)); -static int msgid_cmp PARAMS ((const void *va, const void *vb)); -static int filepos_cmp PARAMS ((const void *va, const void *vb)); +static int cmp_by_msgid PARAMS ((const void *va, const void *vb)); +static int cmp_filepos PARAMS ((const void *va, const void *vb)); +static void msgdomain_list_sort_filepos PARAMS ((msgdomain_list_ty *mdlp)); +static int cmp_by_filepos PARAMS ((const void *va, const void *vb)); /* This variable controls the page width when printing messages. @@ -922,7 +924,7 @@ msgdomain_list_print (mdlp, filename, force, debug) static int -msgid_cmp (va, vb) +cmp_by_msgid (va, vb) const void *va; const void *vb; { @@ -946,13 +948,55 @@ msgdomain_list_sort_by_msgid (mdlp) message_list_ty *mlp = mdlp->item[k]->messages; if (mlp->nitems > 0) - qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), msgid_cmp); + qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), cmp_by_msgid); } } +/* Sort the file positions of every message. */ + +static int +cmp_filepos (va, vb) + const void *va; + const void *vb; +{ + const lex_pos_ty *a = (const lex_pos_ty *) va; + const lex_pos_ty *b = (const lex_pos_ty *) vb; + int cmp; + + cmp = strcmp (a->file_name, b->file_name); + if (cmp == 0) + cmp = (int) a->line_number - (int) b->line_number; + + return cmp; +} + +static void +msgdomain_list_sort_filepos (mdlp) + msgdomain_list_ty *mdlp; +{ + size_t j, k; + + for (k = 0; k < mdlp->nitems; k++) + { + message_list_ty *mlp = mdlp->item[k]->messages; + + for (j = 0; j < mlp->nitems; j++) + { + message_ty *mp = mlp->item[j]; + + if (mp->filepos_count > 0) + qsort (mp->filepos, mp->filepos_count, sizeof (mp->filepos[0]), + cmp_filepos); + } + } +} + + +/* Sort the messages according to the file position. */ + static int -filepos_cmp (va, vb) +cmp_by_filepos (va, vb) const void *va; const void *vb; { @@ -993,11 +1037,15 @@ msgdomain_list_sort_by_filepos (mdlp) { size_t k; + /* It makes sense to compare filepos[0] of different messages only after + the filepos[] array of each message has been sorted. Sort it now. */ + msgdomain_list_sort_filepos (mdlp); + for (k = 0; k < mdlp->nitems; k++) { message_list_ty *mlp = mdlp->item[k]->messages; if (mlp->nitems > 0) - qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), filepos_cmp); + qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), cmp_by_filepos); } } |