File: gom_info.c

package info (click to toggle)
gom 0.29.10-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 420 kB
  • ctags: 292
  • sloc: ansic: 3,189; sh: 573; makefile: 70
file content (193 lines) | stat: -rw-r--r-- 3,922 bytes parent folder | download | duplicates (2)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * gom_info.c: output handling
 */

/* 
 * This file is part of the package
 *
 * gom, Gom is nOt yet another Mixer
 *
 * (c) Stephan Suerken <suerken@fh-worms.de> 1996, 1997
 */

/*
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * INCLUDES
 */
#include "gom.h"

/*
 * MACROS
 */

/*
 * DECLARATIONS/DEFINITIONS
 */

/* holds verbosity mode for gom_info */
int gom_info_verbosity_var = GOM_INFO_NORMAL;

/* holds scroll mode for gom_info */
int gom_info_scroll_var = 0;

/* shown errors count definition */
int gom_info_errors = 0;

/* holds custom output procedure */
void (*gom_info_custom_var) (enum gom_info_types, char *, va_list) = gom_info_custom_default;

/*
 * FUNCTION PROTOTYPES
 */

/******************/

/*
 * VERBOSITY
 */

int
gom_info_verbosity()
{
  return gom_info_verbosity_var;
}

void
gom_info_verbosity_set(enum gom_info_types std_verb,
		       int verbosity)
{
  int old_verbosity = gom_info_verbosity_var;
  gom_info_verbosity_var = verbosity;

  gom_info(std_verb,
	   "Verbosity set to %i (was %i) [" GOM_INFO_VERBOSITY_PRINTF_MASK "].",
	   gom_info_verbosity_var, old_verbosity,
	   GOM_INFO_VERBOSITY_PRINTF_VARS);
}

/*
 * SCROLL MODE
 */

int
gom_info_scroll()		       
{
  return gom_info_scroll_var;
}

int
gom_info_scroll_set(int scroll)
{
  int old_scroll = gom_info_scroll_var;
  gom_info_scroll_var = scroll;
  return old_scroll;
}

/*
 * CUSTOM OUTPUT PROC
 */

void 
gom_info_custom_default(enum gom_info_types kind, char * fmt, va_list vargs)
{
  FILE* output_stream;
  output_stream = stdout;
  if (kind == GOM_INFO_ERROR)
    output_stream = stderr;
  
  vfprintf(output_stream, fmt, vargs);
  fprintf(output_stream, "\n");
  fflush(output_stream);
}

void 
gom_info_custom_set(void (* proc) (enum gom_info_types, char *, va_list))
{
  if (proc == NULL)
    gom_info_custom_var = gom_info_custom_default;
  else
    gom_info_custom_var = proc;
}

/*
 * GOM INFO
 */

void 
gom_info(enum gom_info_types kind, char * fmt, ...)
{
  va_list vargs;
  char new_fmt[GOM_INFO_FMT_SIZE];
  
  /* error count */
  if ((kind == GOM_INFO_ERROR) && (gom_info_errors < INT_MAX))
    ++gom_info_errors;

  if (kind <= gom_info_verbosity())
    {
      strncpy(new_fmt, (kind == GOM_INFO_ERROR) ? "gom error: " : "gom: ", GOM_INFO_FMT_SIZE);
      strncat(new_fmt, fmt, GOM_INFO_FMT_SIZE); 
      va_start(vargs, fmt);
      gom_info_custom_var(kind, new_fmt, vargs);
      va_end(vargs);
    }
}


/*
 * Two helpers
 */

void 
gom_info_block(enum gom_info_types std_verb,
	       int indent, char * text)
{
  const int line_size = 70;
  char line[line_size];
  int i;
  char * string = text;
  int block_width = line_size-1-indent;

  do
    {
      /* avoid leading spaces */
      while (*string == ' ') string++;
      /* indent */
      strcpy(line, "");
      for (i=0; i < indent; i++)
	strncat(line, " ", line_size-1);
      /* blocked text */
      strncat(line, string, block_width);
      /* show string */
      gom_info(std_verb, line);
      /* calc new string */
      if (strlen(string) > block_width)
	string += block_width;
      else
	break;
    }
  while (1);
}

void 
gom_info_text(enum gom_info_types kind, char * text[])
{
  int i;
  for (i = 0; text[i] != NULL; ++i)
    gom_info(kind, text[i]);
}