回精華區
作者itoc.bbs@processor.tfcis.org (核心動力) 看板: plan
標題將 more.c 由 buffered file read 改成 full file read
時間動力核心 (2005/05/22 Thu 23:50:50) Updated: 2005/11/17
: more.c

#define STR_ANSICODE    "[0123456789;"

- static off_t more_off;
+ static uschar *fimage;          /* file image begin */
+ static uschar *fend;            /* file image end */
+ static uschar *foff;            /* 目前讀到哪裡 */

: more.c:more_goto()

  此函式整個刪除

: more.c:more_line()

static int
- more_line(fd, buf)
+ more_line(buf)
- int fd;
  char *buf;
{
- uschar *pool, *base, *tail;
  int ch, len, bytes, in_ansi, in_chi;

- pool = more_pool;
- base = pool + more_base;
- tail = base + more_size;

  len = bytes = in_ansi = in_chi = 0;

  for (;;)
  {
-   if (base >= tail)
-   {
-     ch = read(fd, pool, MORE_BUFSIZE);
-     if (ch <= 0)                      /* end of file or error */
-       break;
-     base = pool;
-     tail = pool + ch;
-   }
+   if (foff >= fend)
+     break;

-   ch = *base;
+   ch = *foff;

    ...
    ...

-   base++;
+   foff++;
    bytes++;

    ...
    ...

-     if ((in_ansi || (base < tail && *base == KEY_ESC)) && bytes < ANSI...)
+     if ((in_ansi || (foff < fend && *foff == KEY_ESC)) && bytes < ANSI...)
        bytes < ANSILINELEN - 1)
        continue;

-     if (base < tail && *base == '\n')
+     if (foff < fend && *foff == '\n')
      {
-       base++;
+       foff++;
        bytes++;
      }

    ...
    ...

- more_base = base - pool;
- more_size = tail - base;
- more_off += bytes;

  return bytes;

: more.c:outs_footer()

static inline void
outs_footer(buf, lino, fsize)
  char *buf;
  int lino;
- off_t fsize;
+ int fsize;
{
  ...
  ...

- /* prints(FOOTER_MORE, ..., (more_off * 100) / fsize); */
+ /* prints(FOOTER_MORE, ..., ((foff - fimage) * 100) / fsize); */

  /* itoc.010821: 為了和 FOOTER 對齊 */
- sprintf(buf, FOOTER_MORE, ..., (more_off * 100) / fsize);
+ sprintf(buf, FOOTER_MORE, ..., (foff - fimage) * 100 / fsize);
  outs(buf);

: more.c:more()

  char buf[ANSILINELEN];
- struct stat st;
- int fd;
  int i;

  ...
  ...

- off_t fsize;                  /* 檔案大小 */
+ int fsize;                    /* 檔案大小 */
  static off_t block[MAXBLOCK]; /*

- if ((fd = open(fpath, O_RDONLY)) < 0)
+ if (!(fimage = f_img(fpath, &fsize)))
    return -1;

- more_base = more_size = 0;
- more_off = 0;
+ foff = fimage;
+ fend = fimage + fsize;

  /* 讀出檔案第一行,來判斷站內信還是站外信 */
- if (fstat(fd, &st) || (fsize = st.st_size) <= 0 || !more_line(fd, buf))
+ if (fsize <= 0 || !more_line(buf))
  {
-   close(fd);
+   free(fimage);
    return -1;
  }

  ...
  ...

  /* 歸零 */
- more_base = 0;
- more_size += more_off;
- more_off = 0;
+ foff = fimage;

  clear();

- while (more_line(fd, buf))
+ while (more_line(buf))
  {
  ...
  ...
      if ((lino % 32 == 0) && ((i = lino >> 5) < MAXBLOCK))
-       block[i] = more_off;
+       block[i] = foff - fimage;
  ...
  ...
        for (i += b_lines; i > 0; i--)
-         more_line(fd, buf);
+         more_line(buf);
  ...
  ...
-   if (more_off >= fsize)      /* 已經讀完全部的檔案 */
+   if (foff >= fend)           /* 已經讀完全部的檔案 */
  ...
  ...
-       if ((shift & END_MASK) && (fsize - more_off >= MORE_BUFSIZE))
+       if ((shift & END_MASK) && (fend - foff >= MORE_BUFSIZE))
  ...
  ...
          /* 先讀到最後一列看看全部有幾列 */
-         while (more_line(fd, buf))
+         while (more_line(buf))
          {
            totallino++;
            if ((totallino % 32 == 0) && ((i = totallino >> 5) < MAXBLOCK))
-             block[i] = more_off;
+             block[i] = foff - fimage;
          }
  ...
  ...
-         more_goto(fd, (off_t) block[i]);
+         foff = fimage + block[i];
          i = i << 5;
          /* 再從上一個 block 的尾端位移到 totallino-b_lines+1 列 */
          for (i = totallino - b_lines - i; i > 0; i--)
-           more_line(fd, buf);
+           more_line(buf);
  ...
  ...
        lino = 0;
-       more_goto(fd, (off_t) 0);
+       foff = fimage;
        clear();
  ...
  ...
-       more_goto(fd, (off_t) block[i]);
+       foff = fimage + block[i];
        i = i << 5;
        /* 再從上一個 block 的尾端位移到 lino-b_lines+1 列 */
        for (i = lino - b_lines - i; i > 0; i--)
-         more_line(fd, buf);
+         more_line(buf);
  ...
  ...
-       more_goto(fd, (off_t) 0);
+       foff = fimage;
        lino = 0;
        shift = b_lines;
  ...
  ...
- close(fd);
+ free(fimage);

--
  Origin:  Maple-itoc˙動力核心  processor.tfcis.org 
  Author: itoc itoc.dorm11.nctu.edu.tw 發表
精華選讀←離開[主題上]主題下(k)上篇(j)下篇S/a搜尋 G串列 TAB精華 ↑↓捲 Pg/Space翻