/[cvs]/dary/db_view.cpp
ViewVC logotype

Contents of /dary/db_view.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations)
Fri Apr 1 11:16:29 2005 UTC (13 years, 6 months ago) by riso
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +9 -0 lines
Celkova suma

1 #include "db_view.h"
2 #include "util.h"
3 #include <stdlib.h>
4
5 #define MAX_COMPL 500
6
7 static int inited=0;
8 static int mode=MODE_ENTRIES, sort=SORT_DATE;
9 static int pn=-1, en=-1;
10 static tPersonId *pa=NULL;
11 static tEntryId *ea=NULL;
12
13 static int cmp_entries_date(const void *a, const void *b) {
14 int i,j;
15 i = (*((tEntryId *)a))->date;
16 j = (*((tEntryId *)b))->date;
17 if (i<j) return -1;
18 if (i>j) return 1;
19 return 0;
20 }
21
22 static int cmp_entries_sum(const void *a, const void *b) {
23 int i,j;
24 i = (*((tEntryId *)a))->sum;
25 j = (*((tEntryId *)b))->sum;
26 if (i<j) return -1;
27 if (i>j) return 1;
28 return 0;
29 }
30
31 static int cmp_entries_name(const void *a, const void *b) {
32 int i;
33 i = mystrcmp( (*((tEntryId *)a))->person->surname, (*((tEntryId *)b))->person->surname );
34 if (i!=0) return i;
35 return mystrcmp( (*((tEntryId *)a))->person->name, (*((tEntryId *)b))->person->name );
36 }
37
38 static int cmp_persons_name(const void *a, const void *b) {
39 int i;
40 i = mystrcmp( (*((tPersonId *)a))->surname, (*((tPersonId *)b))->surname );
41 if (i!=0) return i;
42 return mystrcmp( (*((tPersonId *)a))->name, (*((tPersonId *)b))->name );
43 }
44
45 static int cmp_persons_sum(const void *a, const void *b) {
46 int i,j;
47 i = (*((tPersonId *)a))->sum;
48 j = (*((tPersonId *)b))->sum;
49 if (i<j) return -1;
50 if (i>j) return 1;
51 return 0;
52 }
53
54 void UpdateViewPerson(tPersonId id, int action) {}
55 void UpdateViewEntry(tEntryId id, int action) {}
56
57 void ResetView() {
58 int i;
59
60 if (!inited) return;
61
62 if (mode==MODE_PERSONS) {
63 if (pn != persons->n) {
64 if (pa!=NULL) free(pa);
65 pn = persons->n;
66 pa = ALLOC(tPersonId, pn);
67 }
68 for (i=0; i<persons->n; i++)
69 pa[i]=(tPersonId)(persons->data[i]);
70 if (sort==SORT_NAME) {
71 qsort(pa, pn, sizeof(tPersonId), cmp_persons_name);
72 } else if (sort==SORT_DATE) {
73 // invalid option
74 } else if (sort==SORT_SUM) {
75 qsort(pa, pn, sizeof(tPersonId), cmp_persons_sum);
76 }
77 } else if (mode==MODE_ENTRIES) {
78 if (en != entries->n) {
79 if (ea!=NULL) free(ea);
80 en = entries->n;
81 ea = ALLOC(tEntryId, en);
82 }
83 for (i=0; i<entries->n; i++)
84 ea[i]=(tEntryId)(entries->data[i]);
85 if (sort==SORT_NAME) {
86 qsort(ea, en, sizeof(tEntryId), cmp_entries_name);
87 } else if (sort==SORT_DATE) {
88 qsort(ea, en, sizeof(tEntryId), cmp_entries_date);
89 } else if (sort==SORT_SUM) {
90 qsort(ea, en, sizeof(tEntryId), cmp_entries_sum);
91 }
92 }
93 }
94
95 void InitView() {
96 DeleteView();
97 inited=1;
98 }
99
100 static void DeleteView2() {
101 if (pa!=NULL) free(pa);
102 if (ea!=NULL) free(ea);
103 pa=NULL; ea=NULL;
104 pn=en=-1;
105 }
106 void DeleteView() {
107 DeleteView2();
108 inited=0;
109 }
110
111 void SetMode(int _mode) { DeleteView2(); mode=_mode; ResetView(); }
112 void SetSort(int _sort) { DeleteView2(); sort=_sort; ResetView(); }
113
114 int nRows() {
115 if (mode==MODE_PERSONS) {
116 if (pn<0) return 0; else return pn;
117 } else if (mode==MODE_ENTRIES) {
118 if (en<0) return 0; else return en;
119 }
120 return 0;
121 }
122
123 int nPersons() {
124 return persons->n;
125 }
126
127 int nEntries() {
128 return entries->n;
129 }
130
131 long GlobalSum() {
132 int i;
133 long s;
134 for (s=0, i=0; i<entries->n; i++) {
135 s += ((tEntryId)(entries->data[i]))->sum;
136 }
137 return s;
138 }
139
140 void *getRow(int r) {
141 if (mode==MODE_PERSONS) {
142 return (void *)(pa[r]);
143 } else if (mode==MODE_ENTRIES) {
144 return (void *)(ea[r]);
145 }
146 return NULL;
147 }
148
149 static int cmp_comp(const void *a, const void *b) {
150 char **A = (char **)a,**B = (char **)b;
151 int i;
152
153 i = mystrcmp(A[1], B[1]);
154 if (i==0) i = mystrcmp(A[0], B[0]);
155 return i;
156 }
157
158 char **Completion(char *name, char *surname) {
159 int i,n, nlen=strlen(name), snlen=strlen(surname);
160 char **o;
161
162 for (n=i=0; i<persons->n; i++) {
163 if (strncmp(name, ((tPersonId)(persons->data[i]))->name, nlen)==0)
164 if (strncmp(surname, ((tPersonId)(persons->data[i]))->surname, snlen)==0) {
165 n++;
166 if (n>MAX_COMPL) return NULL;
167 }
168 }
169
170 o = ALLOC(char *, 2*n+1);
171
172 for (n=i=0; i<persons->n; i++) {
173 if (strncmp(name, ((tPersonId)(persons->data[i]))->name, nlen)==0)
174 if (strncmp(surname, ((tPersonId)(persons->data[i]))->surname, snlen)==0) {
175 o[n] = ALLOC(char, strlen(((tPersonId)(persons->data[i]))->name)+1);
176 strcpy(o[n], ((tPersonId)(persons->data[i]))->name);
177 n++;
178 o[n] = ALLOC(char, strlen(((tPersonId)(persons->data[i]))->surname)+1);
179 strcpy(o[n], ((tPersonId)(persons->data[i]))->surname);
180 n++;
181 }
182 }
183
184 qsort(o, n/2, sizeof(char *)*2, cmp_comp);
185 o[n] = NULL;
186 return o;
187 }
188

CVS Admin">CVS Admin
ViewVC Help
Powered by ViewVC 1.1.26