source: trunk/libtransmission/history.c @ 13625

Last change on this file since 13625 was 13625, checked in by jordan, 9 years ago

Follow more common whitespace style conventions in the C code (libtransmission, daemon, utils, cli, gtk).

  • Property svn:keywords set to Date Rev Author Id
File size: 1.3 KB
Line 
1/*
2 * This file Copyright (C) Mnemosyne LLC
3 *
4 * This file is licensed by the GPL version 2. Works owned by the
5 * Transmission project are granted a special exemption to clause 2 (b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 *
10 * $Id: history.c 13625 2012-12-05 17:29:46Z jordan $
11 */
12
13#include <assert.h>
14#include <string.h> /* memset () */
15
16#include "transmission.h"
17#include "history.h"
18#include "utils.h"
19
20void
21tr_historyAdd (tr_recentHistory * h, time_t now, unsigned int n)
22{
23  if (h->slices[h->newest].date == now)
24    {
25      h->slices[h->newest].n += n;
26    }
27  else
28    {
29      if (++h->newest == TR_RECENT_HISTORY_PERIOD_SEC)
30        h->newest = 0;
31      h->slices[h->newest].date = now;
32      h->slices[h->newest].n = n;
33    }
34}
35
36unsigned int
37tr_historyGet (const tr_recentHistory * h, time_t now, unsigned int sec)
38{
39  unsigned int n = 0;
40  const time_t cutoff = (now?now:tr_time ()) - sec;
41  int i = h->newest;
42
43  for (;;)
44    {
45      if (h->slices[i].date <= cutoff)
46        break;
47
48      n += h->slices[i].n;
49
50      if (--i == -1)
51        i = TR_RECENT_HISTORY_PERIOD_SEC - 1; /* circular history */
52
53      if (i == h->newest)
54        break; /* we've come all the way around */
55    }
56
57  return n;
58}
Note: See TracBrowser for help on using the repository browser.