From: Pierre Habouzit Date: Sun, 13 May 2007 16:11:27 +0000 (+0200) Subject: rationalize mh_sequences code. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=e01192eefa02e292a2e9f6e041977f2db03d8db6;p=apps%2Fmadmutt.git rationalize mh_sequences code. Signed-off-by: Pierre Habouzit --- diff --git a/lib-mx/mh.c b/lib-mx/mh.c index 7c2f7e2..c94ef41 100644 --- a/lib-mx/mh.c +++ b/lib-mx/mh.c @@ -35,13 +35,12 @@ struct maildir { struct maildir *next; }; -struct mh_sequences { - int max; - short *flags; -}; +typedef struct mh_sequences { + int size; + char flags[]; +} mh_sequences; /* mh_sequences support */ - #define MH_SEQ_UNSEEN (1 << 0) #define MH_SEQ_REPLIED (1 << 1) #define MH_SEQ_FLAGGED (1 << 2) @@ -51,39 +50,37 @@ static int maildir_check_empty (const char*); static int maildir_check_mailbox (CONTEXT*, int*, int); static int mh_check_mailbox (CONTEXT*, int*, int); -static void mhs_alloc (struct mh_sequences *mhs, int i) +static mh_sequences *mhs_new(void) { - int j; - int newmax; - - if (i > mhs->max || !mhs->flags) { - newmax = i + 128; - p_realloc(&mhs->flags, newmax + 1); - for (j = mhs->max + 1; j <= newmax; j++) - mhs->flags[j] = 0; + mh_sequences *res = xmalloc(sizeof(mh_sequences) + 128); + res->size = 128; + return res; +} - mhs->max = newmax; - } +static void mhs_ensure(mh_sequences *mhs, int i) +{ + if (i > mhs->size) { + xrealloc((void *)&mhs, sizeof(mh_sequences) + mhs->size + 128); + p_clear(mhs->flags + mhs->size, 128); + mhs->size += 128; + } } -static void mhs_free_sequences (struct mh_sequences *mhs) +static void mhs_delete(mh_sequences **mhs) { - p_delete(&mhs->flags); + p_delete(mhs); } static short mhs_check (struct mh_sequences *mhs, int i) { - if (!mhs->flags || i > mhs->max) - return 0; - else - return mhs->flags[i]; + return i > mhs->size ? 0 : mhs->flags[i]; } static short mhs_set (struct mh_sequences *mhs, int i, short f) { - mhs_alloc (mhs, i); - mhs->flags[i] |= f; - return mhs->flags[i]; + mhs_ensure(mhs, i); + mhs->flags[i] |= f; + return mhs->flags[i]; } static void mh_read_token (char *t, int *first, int *last) @@ -143,17 +140,19 @@ static void mh_read_sequences (struct mh_sequences *mhs, const char *path) int mh_buffy (const char *path) { - int i, r = 0; - struct mh_sequences mhs; - - p_clear(&mhs, 1); + mh_sequences *mhs = mhs_new(); + int i; + + mh_read_sequences(mhs, path); + for (i = 0; i <= mhs->size; i++) { + if (mhs_check(mhs, i) & MH_SEQ_UNSEEN) { + mhs_delete(&mhs); + return 1; + } + } - mh_read_sequences (&mhs, path); - for (i = 0; !r && i <= mhs.max; i++) - if (mhs_check (&mhs, i) & MH_SEQ_UNSEEN) - r = 1; - mhs_free_sequences (&mhs); - return r; + mhs_delete(&mhs); + return 0; } static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt) @@ -188,8 +187,8 @@ static int mh_mkstemp (CONTEXT * dest, FILE ** fp, char **tgt) return 0; } -static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs, - short f, const char *tag) +static void mhs_write_one_sequence(FILE * fp, struct mh_sequences *mhs, + short f, const char *tag) { int i; int first, last; @@ -199,7 +198,7 @@ static void mhs_write_one_sequence (FILE * fp, struct mh_sequences *mhs, first = -1; last = -1; - for (i = 0; i <= mhs->max; i++) { + for (i = 0; i <= mhs->size; i++) { if ((mhs_check (mhs, i) & f)) { if (first < 0) first = i; @@ -248,15 +247,11 @@ static void mh_update_sequences (CONTEXT * ctx) char seq_unseen[STRING]; char seq_replied[STRING]; char seq_flagged[STRING]; + mh_sequences *mhs = mhs_new(); - - struct mh_sequences mhs; - - p_clear(&mhs, 1); - - snprintf (seq_unseen, sizeof (seq_unseen), "%s:", NONULL (MhUnseen)); - snprintf (seq_replied, sizeof (seq_replied), "%s:", NONULL (MhReplied)); - snprintf (seq_flagged, sizeof (seq_flagged), "%s:", NONULL (MhFlagged)); + snprintf(seq_unseen, sizeof(seq_unseen), "%s:", NONULL(MhUnseen)); + snprintf(seq_replied, sizeof(seq_replied), "%s:", NONULL(MhReplied)); + snprintf(seq_flagged, sizeof(seq_flagged), "%s:", NONULL(MhFlagged)); if (mh_mkstemp (ctx, &nfp, &tmpfname) != 0) { /* error message? */ @@ -294,29 +289,28 @@ static void mh_update_sequences (CONTEXT * ctx) i = atoi (p); if (!ctx->hdrs[l]->read) { - mhs_set (&mhs, i, MH_SEQ_UNSEEN); + mhs_set(mhs, i, MH_SEQ_UNSEEN); unseen++; } if (ctx->hdrs[l]->flagged) { - mhs_set (&mhs, i, MH_SEQ_FLAGGED); + mhs_set(mhs, i, MH_SEQ_FLAGGED); flagged++; } if (ctx->hdrs[l]->replied) { - mhs_set (&mhs, i, MH_SEQ_REPLIED); + mhs_set(mhs, i, MH_SEQ_REPLIED); replied++; } } /* write out the new sequences */ if (unseen) - mhs_write_one_sequence (nfp, &mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen)); + mhs_write_one_sequence(nfp, mhs, MH_SEQ_UNSEEN, NONULL (MhUnseen)); if (flagged) - mhs_write_one_sequence (nfp, &mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged)); + mhs_write_one_sequence(nfp, mhs, MH_SEQ_FLAGGED, NONULL (MhFlagged)); if (replied) - mhs_write_one_sequence (nfp, &mhs, MH_SEQ_REPLIED, NONULL (MhReplied)); - - mhs_free_sequences (&mhs); + mhs_write_one_sequence(nfp, mhs, MH_SEQ_REPLIED, NONULL (MhReplied)); + mhs_delete(&mhs); /* try to commit the changes - no guarantee here */ m_fclose(&nfp); @@ -786,26 +780,19 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md) */ static int _mh_read_dir (CONTEXT * ctx, const char *subdir) { - struct maildir *md; - struct mh_sequences mhs; - struct maildir **last; - int count; - - - p_clear(&mhs, 1); + struct maildir *md = NULL, **last = &md; + int count = 0; maildir_update_mtime (ctx); - md = NULL; - last = &md; - count = 0; if (maildir_parse_dir (ctx, &last, subdir, &count) == -1) return -1; if (ctx->magic == M_MH) { - mh_read_sequences (&mhs, ctx->path); - mh_update_maildir (md, &mhs); - mhs_free_sequences (&mhs); + mh_sequences *mhs = mhs_new(); + mh_read_sequences(mhs, ctx->path); + mh_update_maildir(md, mhs); + mhs_delete(&mhs); } if (ctx->magic == M_MAILDIR) @@ -1553,9 +1540,8 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib char buf[_POSIX_PATH_MAX]; struct stat st, st_cur; short modified = 0, have_new = 0, occult = 0; - struct maildir *md, *p; - struct maildir **last = NULL; - struct mh_sequences mhs; + struct maildir *md = NULL, **last = &md, *p; + mh_sequences *mhs; hash_t *fnames; int i; @@ -1592,14 +1578,11 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib ctx->mtime_cur = st_cur.st_mtime; ctx->mtime = st.st_mtime; - p_clear(&mhs, 1); - - md = NULL; - last = &md; maildir_parse_dir (ctx, &last, NULL, NULL); - mh_read_sequences (&mhs, ctx->path); - mh_update_maildir (md, &mhs); - mhs_free_sequences (&mhs); + mhs = mhs_new(); + mh_read_sequences(mhs, ctx->path); + mh_update_maildir(md, mhs); + mhs_delete(&mhs); /* check for modifications and adjust flags */ fnames = hash_new (1031, 0); @@ -1638,16 +1621,13 @@ static int mh_check_mailbox (CONTEXT * ctx, int *index_hint, int unused __attrib } - - /* * These functions try to find a message in a maildir folder when it * has moved under our feet. Note that this code is rather expensive, but * then again, it's called rarely. */ - -static FILE *_maildir_open_find_message (const char *folder, const char *unique, - const char *subfolder) +static FILE *_maildir_open_find_message(const char *folder, const char *unique, + const char *subfolder) { char dir[_POSIX_PATH_MAX]; char tunique[_POSIX_PATH_MAX];