Wgetダウンロード進捗バーはどのように実現されますか?

2297 ワード

Wgetダウンロード進捗バーはどのように実現されますか?
wgetを使ったことがあってから、次のような進捗バーをどのように実現するかに興味があり、もちろん流れに入らないプログラマーとして心の中にも自分の考えがあります.
今日はGoogleからwgetの実家へhttps://www.gnu.org/software/wget/ああ、1.9のソースコードをダウンロードしてprogressを見つけました.c.
      /* The progress bar: "[====>      ]" or "[++==>      ]". */
      /* Size of the initial portion. */
      int insz = (double)bp->initial_length / bp->total_length * progress_size;

      /* Size of the downloaded portion. */
      int dlsz = (double)size / bp->total_length * progress_size;

      char *begin;
      int i;

      assert (dlsz <= progress_size);
      assert (insz <= dlsz);

      *p++ = '[';
      begin = p;

      /* Print the initial portion of the download with '+' chars, the
     rest with '=' and one '>'.  */
      for (i = 0; i < insz; i++)
    *p++ = '+';
      dlsz -= insz;
      if (dlsz > 0)
    {
      for (i = 0; i < dlsz - 1; i++)
        *p++ = '=';
      *p++ = '>';
    }

      while (p - begin < progress_size)
    *p++ = ' ';
      *p++ = ']';

上のコードから分かるように、事前に用意したbufferを、対応する進捗文字を埋め込み、最後に出力します.もちろん今回はあまり細かく分析していないので、帯蒙を当てています.