aboutsummaryrefslogtreecommitdiffstats
path: root/FICS/multicol.c
blob: d9fa653baa14a1dd794e0171ac76043ecbacbef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* multicol.c
 *
 */

/*
    fics - An internet chess server.
    Copyright (C) 1993  Richard V. Nash

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

/* Revision history:
   name		email		yy/mm/dd	Change
   Richard Nash	              	93/10/22	Created
*/

#include "stdinclude.h"

#include "common.h"
#include "multicol.h"
#include "utils.h"
#include "rmalloc.h"

/* grimm */
#if defined(SGI)
#else
/* char *strchr( char *s, int c); */
#endif
/* added for warning */

PUBLIC multicol *multicol_start(int maxArray)
{
  int i;
  multicol *m;

  m = (multicol *) rmalloc(sizeof(multicol));
  m->arraySize = maxArray;
  m->num = 0;
  m->strArray = (char **) rmalloc(sizeof(char *) * m->arraySize);
  for (i = 0; i < m->arraySize; i++)
    m->strArray[i] = NULL;
  return m;
}

PUBLIC int multicol_store(multicol * m, char *str)
{
  if (m->num >= m->arraySize)
    return -1;
  if (!str)
    return -1;
  m->strArray[m->num] = xstrdup(str);
  m->num++;
  return 0;
}

PUBLIC int multicol_store_sorted(multicol * m, char *str)
/* use this instead of milticol_store to print a list sorted */
{
  int i;
  int found = 0;
  if (m->num >= m->arraySize)
    return -1;
  if (!str)
    return -1;
  for (i = m->num; (i > 0) && (!found); i--) {
    if (strcasecmp(str, m->strArray[i - 1]) >= 0) {
      found = 1;
      m->strArray[i] = xstrdup(str);
    } else {
      m->strArray[i] = m->strArray[i - 1];
    }
  }
  if (!found)
    m->strArray[0] = xstrdup(str);
  m->num++;
  return 0;
}

PUBLIC int multicol_pprint(multicol * m, int player, int cols, int space)
{
  int i;
  int maxWidth = 0;
  int numPerLine;
  int numLines;
  int on, theone, len;
  int done;
  int temp;
  char *tempptr;

  pprintf(player, "\n");
  for (i = 0; i < m->num; i++) {
    tempptr = m->strArray[i];
    temp = strlen(tempptr);	/* loon: yes, this is pathetic */
    for (; *tempptr; tempptr++) {
      if (*tempptr == '\033')
	temp -= 4;
    }
    if (temp > maxWidth)
      maxWidth = temp;
  }
  maxWidth += space;
  numPerLine = cols / maxWidth;
  numLines = m->num / numPerLine;
  if (numLines * numPerLine < m->num)
    numLines++;
  on = 0;
  done = 0;
  while (!done) {
    for (i = 0; i < numPerLine; i++) {
      theone = on + numLines * i;
      if (theone >= m->num) {
	break;
      }
      tempptr = m->strArray[theone];
      temp = strlen(tempptr);	/* loon: yes, still pathetic */
      for (; *tempptr; tempptr++) {
	if (*tempptr == '\033')
	  temp -= 4;
      }
      len = maxWidth - temp;
      if (i == numPerLine - 1)
	len -= space;
      pprintf(player, "%s", m->strArray[theone]);
      while (len) {
	pprintf(player, " ");
	len--;
      }
    }
    pprintf(player, "\n");
    on += 1;
    if (on >= numLines)
      break;
  }
  return 0;
}

PUBLIC int multicol_end(multicol * m)
{
  int i;
  for (i = 0; i < m->num; i++)
    rfree(m->strArray[i]);
  rfree(m->strArray);
  rfree(m);
  return 0;
}