/[LeafOK_CVS]/lbbs/src/user_stat.c
ViewVC logotype

Contents of /lbbs/src/user_stat.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Oct 23 04:09:33 2025 UTC (4 months, 3 weeks ago) by sysadm
Branch: MAIN
Content type: text/x-csrc
Add user_stat

1 /***************************************************************************
2 user_list.c - description
3 -------------------
4 Copyright : (C) 2004-2025 by Leaflet
5 Email : leaflet@leafok.com
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 3 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17 #include "log.h"
18 #include "section_list.h"
19 #include "user_list.h"
20 #include "user_stat.h"
21
22 int user_stat_map_init(USER_STAT_MAP *p_map)
23 {
24 if (p_map == NULL)
25 {
26 log_error("NULL pointer error\n");
27 return -1;
28 }
29
30 p_map->user_count = 0;
31 p_map->last_uid = -1;
32 p_map->last_article_index = -1;
33
34 return 0;
35 }
36
37 int user_stat_map_update(USER_STAT_MAP *p_map)
38 {
39 int32_t uid_list[BBS_max_user_count];
40 int user_cnt = BBS_max_user_count;
41 int article_cnt;
42 int i;
43 ARTICLE *p_article;
44
45 if (p_map == NULL)
46 {
47 log_error("NULL pointer error\n");
48 return -1;
49 }
50
51 // Load uid_list
52 if (get_user_id_list(uid_list, &user_cnt, p_map->last_uid + 1) < 0)
53 {
54 log_error("get_user_id_list(%d, 0) error\n", user_cnt);
55 return -2;
56 }
57
58 for (i = 0; i < user_cnt && p_map->user_count + i < BBS_max_user_count; i++)
59 {
60 p_map->stat_list[p_map->user_count + i].uid = uid_list[i];
61 p_map->stat_list[p_map->user_count + i].article_count = 0;
62 }
63
64 p_map->user_count += i;
65 p_map->last_uid = uid_list[i - 1];
66
67 // Scan articles
68 article_cnt = article_block_article_count();
69 for (i = p_map->last_article_index + 1; i < article_cnt; i++)
70 {
71 p_article = article_block_find_by_index(i);
72 if (p_article == NULL)
73 {
74 log_error("article_block_find_by_index(index=%d) error\n", i);
75 break;
76 }
77
78 if (p_article->uid > p_map->last_uid)
79 {
80 #ifdef _DEBUG
81 log_error("uid=%d of article(aid=%d) is greater than last_uid=%d, waiting for next user list update\n",
82 p_article->uid, p_article->aid, p_map->last_uid);
83 #endif
84 break;
85 }
86
87 if (user_stat_article_cnt_inc(p_map, p_article->uid, 1) < 0)
88 {
89 log_error("user_stat_article_cnt_inc(uid=%d, 1) error\n", p_article->uid);
90 break;
91 }
92 }
93
94 p_map->last_article_index = i - 1;
95
96 return 0;
97 }
98
99 int user_stat_article_cnt_inc(USER_STAT_MAP *p_map, int32_t uid, int n)
100 {
101 int left;
102 int right;
103 int mid;
104
105 if (p_map == NULL)
106 {
107 log_error("NULL pointer error\n");
108 return -1;
109 }
110
111 left = 0;
112 right = p_map->user_count - 1;
113
114 while (left < right)
115 {
116 mid = (left + right) / 2;
117 if (uid < p_map->stat_list[mid].uid)
118 {
119 right = mid;
120 }
121 else if (uid > p_map->stat_list[mid].uid)
122 {
123 left = mid + 1;
124 }
125 else // if (uid == p_map->stat_list[mid].uid)
126 {
127 left = mid;
128 break;
129 }
130 }
131
132 if (uid == p_map->stat_list[left].uid) // found
133 {
134 p_map->stat_list[left].article_count += n;
135 return 1;
136 }
137
138 return 0; // not found
139 }
140
141 int user_stat_get(USER_STAT_MAP *p_map, int32_t uid, const USER_STAT **pp_stat)
142 {
143 int left;
144 int right;
145 int mid;
146
147 if (p_map == NULL)
148 {
149 log_error("NULL pointer error\n");
150 return -1;
151 }
152
153 left = 0;
154 right = p_map->user_count - 1;
155
156 while (left < right)
157 {
158 mid = (left + right) / 2;
159 if (uid < p_map->stat_list[mid].uid)
160 {
161 right = mid;
162 }
163 else if (uid > p_map->stat_list[mid].uid)
164 {
165 left = mid + 1;
166 }
167 else // if (uid == p_map->stat_list[mid].uid)
168 {
169 left = mid;
170 break;
171 }
172 }
173
174 if (uid == p_map->stat_list[left].uid) // found
175 {
176 *pp_stat = &(p_map->stat_list[left]);
177 return 1;
178 }
179
180 // not found
181 *pp_stat = NULL;
182 return 0;
183 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1