Check-in modifications made by HGM so far
[capablanca.git] / lasker-2.2.3 / src / multicol.c
1 /*
2    Copyright (c) 1993 Richard V. Nash.
3    Copyright (c) 2000 Dan Papasian
4    Copyright (C) Andrew Tridgell 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21
22 #include "includes.h"
23
24 struct multicol *multicol_start(int maxArray)
25 {
26   int i;
27   multicol *m;
28
29   m = (multicol *) malloc(sizeof(multicol));
30   m->arraySize = maxArray;
31   m->num = 0;
32   m->strArray = (char **) malloc(sizeof(char *) * m->arraySize);
33   for (i = 0; i < m->arraySize; i++)
34     m->strArray[i] = NULL;
35   return m;
36 }
37
38 int multicol_store(multicol * m, char *str)
39 {
40   if (m->num >= m->arraySize)
41     return -1;
42   if (!str)
43     return -1;
44   m->strArray[m->num] = strdup(str);
45   m->num++;
46   return 0;
47 }
48
49 int multicol_store_sorted(multicol * m, char *str)
50 /* use this instead of multicol_store to print a list sorted */
51 {
52   int i;
53   int found = 0;
54   if (m->num >= m->arraySize)
55     return -1;
56   if (!str)
57     return -1;
58   for (i = m->num; (i > 0) && (!found); i--) {
59     if (strcasecmp(str, m->strArray[i - 1]) >= 0) {
60       found = 1;
61       m->strArray[i] = strdup(str);
62     } else {
63       m->strArray[i] = m->strArray[i - 1];
64     }
65   }
66   if (!found)
67     m->strArray[0] = strdup(str);
68   m->num++;
69   return 0;
70 }
71
72 int multicol_pprint(multicol * m, int player, int cols, int space)
73 {
74   int i;
75   int maxWidth = 0;
76   int numPerLine;
77   int numLines;
78   int on, theone, len;
79   int done;
80   int temp;
81   char *tempptr;
82
83   pprintf(player, "\n");
84   for (i = 0; i < m->num; i++) {
85     tempptr = m->strArray[i];
86     temp = strlen(tempptr);     /* loon: yes, this is pathetic */
87     for (; *tempptr; tempptr++) {
88       if (*tempptr == '\033')
89         temp -= 4;
90     }
91     if (temp > maxWidth)
92       maxWidth = temp;
93   }
94   maxWidth += space;
95   numPerLine = cols / maxWidth;
96   if (!numPerLine)
97     numPerLine = 1; /* stop a division by 0 on large members */
98   numLines = m->num / numPerLine;
99   if (numLines * numPerLine < m->num)
100     numLines++;
101   on = 0;
102   done = 0;
103   while (!done) {
104     for (i = 0; i < numPerLine; i++) {
105       theone = on + numLines * i;
106       if (theone >= m->num) {
107         break;
108       }
109       tempptr = m->strArray[theone];
110       temp = strlen(tempptr);   /* loon: yes, still pathetic */
111       for (; *tempptr; tempptr++) {
112         if (*tempptr == '\033')
113           temp -= 4;
114       }
115       len = maxWidth - temp;
116       if (i == numPerLine - 1)
117         len -= space;
118       pprintf(player, "%s", m->strArray[theone]);
119       while (len) {
120         pprintf(player, " ");
121         len--;
122       }
123     }
124     pprintf(player, "\n");
125     on += 1;
126     if (on >= numLines)
127       break;
128   }
129   return 0;
130 }
131
132 int multicol_end(multicol * m)
133 {
134         int i;
135         for (i = 0; i < m->num; i++)
136                 FREE(m->strArray[i]);
137         FREE(m->strArray);
138         FREE(m);
139         return 0;
140 }