Skip to content

Commit f85df63

Browse files
committed
C++-ify lib.error.c
1 parent 911f4e9 commit f85df63

File tree

5 files changed

+66
-92
lines changed

5 files changed

+66
-92
lines changed

judge/evict.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414

1515
#include "lib.misc.h"
1616

17-
extern "C" {
1817
#include "lib.error.h"
19-
}
2018

2119
#define PROGRAM "evict"
2220
#define VERSION DOMJUDGE_VERSION "/" REVISION

judge/runguard.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ struct option const long_opts[] = {
188188
{ nullptr, 0, nullptr, 0 }
189189
};
190190

191-
void warning( const char *, ...) __attribute__((format (printf, 1, 2)));
192-
void verbose( const char *, ...) __attribute__((format (printf, 1, 2)));
193-
void error(int, const char *, ...) __attribute__((format (printf, 2, 3)));
191+
static void warning( const char *, ...) __attribute__((format (printf, 1, 2)));
192+
static void verbose( const char *, ...) __attribute__((format (printf, 1, 2)));
193+
static void error(int, const char *, ...) __attribute__((format (printf, 2, 3)));
194194
void write_meta(const char *, const char *, ...) __attribute__((format (printf, 2, 3)));
195195

196-
void warning(const char *format, ...)
196+
static void warning(const char *format, ...)
197197
{
198198
va_list ap;
199199
va_start(ap,format);
@@ -207,7 +207,7 @@ void warning(const char *format, ...)
207207
va_end(ap);
208208
}
209209

210-
void verbose(const char *format, ...)
210+
static void verbose(const char *format, ...)
211211
{
212212
va_list ap;
213213
va_start(ap,format);
@@ -244,7 +244,7 @@ void warning_from_signalhandler(const char* msg)
244244
}
245245
}
246246

247-
void error(int errnum, const char *format, ...)
247+
static void error(int errnum, const char *format, ...)
248248
{
249249
// Silently ignore errors that happen while handling other errors.
250250
if (in_error_handling) return;

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ OBJECTS = $(addsuffix $(OBJEXT),lib.error lib.misc)
88

99
build: $(OBJECTS)
1010

11-
lib.error$(OBJEXT): lib.error.c lib.error.h
11+
lib.error$(OBJEXT): lib.error.cc lib.error.h
1212
lib.misc$(OBJEXT): lib.misc.cc lib.misc.h
1313

1414
clean-l:

lib/lib.error.c renamed to lib/lib.error.cc

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,17 @@
1818

1919
#include "lib.error.h"
2020

21-
#include <stdlib.h>
22-
#include <string.h>
21+
#include <cstdlib>
22+
#include <cstring>
23+
#include <cstdarg>
24+
#include <cstdio>
25+
#include <ctime>
26+
#include <vector>
27+
#include <string>
28+
#include <iostream>
2329
#include <unistd.h>
24-
#include <time.h>
2530
#include <sys/time.h>
2631

27-
/* Define va_copy macro if not available (ANSI C99 only).
28-
* memcpy() is fallback suggested by the autoconf manual, but doesn't
29-
* work with g++ on AMD 64bit platform.
30-
* FIXME: Replace by autoconf test?
31-
*/
32-
#ifndef va_copy
33-
#ifdef __va_copy
34-
#define va_copy(dest, src) __va_copy(dest,src)
35-
#else
36-
#define va_copy(dest, src) memcpy(&dest, &src, sizeof(va_list))
37-
#endif
38-
#endif
39-
4032
/* Use program name in syslogging if defined */
4133
#ifndef PROGRAM
4234
#define PROGRAM NULL
@@ -52,22 +44,28 @@ int loglevel = LOG_DEBUG;
5244
FILE *stdlog = NULL;
5345
int syslog_open = 0;
5446

55-
char *printf_escape(const char *str)
47+
/* Escape '%' characters in a string for use in printf like functions */
48+
static std::string escape_percent(const char *str)
5649
{
57-
char *escaped;
58-
char c;
59-
size_t str_pos, esc_pos;
50+
if (str == NULL) return "";
51+
std::string escaped;
52+
size_t len = strlen(str);
53+
size_t percent_count = 0;
54+
55+
for (size_t i = 0; i < len; ++i) {
56+
if (str[i] == '%') {
57+
percent_count++;
58+
}
59+
}
6060

61-
escaped = (char *)malloc(2*strlen(str)+1);
62-
esc_pos = 0;
61+
escaped.reserve(len + percent_count);
6362

64-
for(str_pos=0; str_pos<strlen(str); str_pos++) {
65-
c = str[str_pos];
66-
escaped[esc_pos++] = c;
67-
if ( c=='%' ) escaped[esc_pos++] = c;
63+
for (size_t i = 0; i < len; ++i) {
64+
escaped += str[i];
65+
if (str[i] == '%') {
66+
escaped += '%';
67+
}
6868
}
69-
escaped[esc_pos] = 0;
70-
7169
return escaped;
7270
}
7371

@@ -77,9 +75,7 @@ void vlogmsg(int msglevel, const char *mesg, va_list ap)
7775
struct timeval currtime;
7876
struct tm tm_buf;
7977
char timestring[128];
80-
char *progname_escaped;
81-
char *buffer;
82-
int bufferlen;
78+
std::string buffer;
8379
va_list aq;
8480
char *str, *endptr;
8581
int syslog_fac;
@@ -106,37 +102,36 @@ void vlogmsg(int msglevel, const char *mesg, va_list ap)
106102
strftime(timestring, sizeof(timestring), "%b %d %H:%M:%S", &tm_buf);
107103
sprintf(timestring+strlen(timestring), ".%03d", (int)(currtime.tv_usec/1000));
108104

109-
progname_escaped = printf_escape(progname);
110-
if ( progname_escaped==NULL ) abort();
111-
112-
bufferlen = strlen(timestring)+strlen(progname_escaped)+strlen(mesg)+20;
113-
buffer = (char *)malloc(bufferlen);
114-
if ( buffer==NULL ) abort();
105+
std::string progname_escaped = escape_percent(progname);
115106

116-
snprintf(buffer, bufferlen, "[%s] %s[%d]: %s\n",
117-
timestring, progname_escaped, getpid(), mesg);
118-
119-
free(progname_escaped);
107+
// Construct the format string: "[time] progname[pid]: message\n"
108+
buffer = "[";
109+
buffer += timestring;
110+
buffer += "] ";
111+
buffer += progname_escaped;
112+
buffer += "[";
113+
buffer += std::to_string(getpid());
114+
buffer += "]: ";
115+
buffer += mesg;
116+
buffer += "\n";
120117

121118
if ( msglevel<=verbose ) {
122119
va_copy(aq, ap);
123-
vfprintf(stderr, buffer, aq);
120+
vfprintf(stderr, buffer.c_str(), aq);
124121
fflush(stderr);
125122
va_end(aq);
126123
}
127124
if ( msglevel<=loglevel && stdlog!=NULL ) {
128125
va_copy(aq, ap);
129-
vfprintf(stdlog, buffer, aq);
126+
vfprintf(stdlog, buffer.c_str(), aq);
130127
fflush(stdlog);
131128
va_end(aq);
132129
}
133130

134-
free(buffer);
135-
136131
if ( msglevel<=loglevel && syslog_open ) {
137-
buffer = vallocstr(mesg, ap);
138-
syslog(msglevel, "%s", buffer);
139-
free(buffer);
132+
char *syslog_buf = vallocstr(mesg, ap);
133+
syslog(msglevel, "%s", syslog_buf);
134+
free(syslog_buf);
140135
}
141136
}
142137

@@ -161,47 +156,36 @@ void logmsg(int msglevel, const char *mesg, ...)
161156
*/
162157
char *errorstring(const char *type, int errnum, const char *mesg)
163158
{
164-
size_t buffersize;
165-
char *errtype, *errdescr, *buffer;
159+
std::string errtype;
160+
std::string errdescr;
161+
std::string buffer;
166162

167163
/* Set errtype to given string or default to 'ERROR' */
168164
if ( type==NULL ) {
169-
errtype = strdup(ERRSTR);
170-
if ( errtype==NULL ) abort();
165+
errtype = ERRSTR;
171166
} else {
172-
errtype = (char *)type;
167+
errtype = type;
173168
}
174169

175-
errdescr = NULL;
176170
if ( errnum != 0 ) {
177171
errdescr = strerror(errno);
178172
} else if ( mesg == NULL ) {
179-
errdescr = strdup("unknown error");
173+
errdescr = "unknown error";
180174
}
181175

182-
buffersize = strlen(errtype)
183-
+ (errdescr == NULL ? 0 : strlen(errdescr))
184-
+ (mesg == NULL ? 0 : strlen(mesg))
185-
+ 5;
186-
187-
buffer = (char *)malloc(sizeof(char) * buffersize);
188-
if ( buffer==NULL ) abort();
189-
buffer[0] = '\0';
190-
191-
strcat(buffer, errtype);
192-
strcat(buffer, ": ");
176+
buffer = errtype + ": ";
193177

194-
if ( mesg != NULL ) strcat(buffer, mesg);
178+
if ( mesg != NULL ) buffer += mesg;
195179

196180
if ( mesg != NULL &&
197-
errdescr != NULL ) strcat(buffer, ": ");
181+
!errdescr.empty() ) buffer += ": ";
198182

199-
if ( errdescr != NULL ) strcat(buffer, errdescr);
183+
if ( !errdescr.empty() ) buffer += errdescr;
200184

201-
if ( type == NULL ) free(errtype);
202-
if ( mesg == NULL && errnum == 0 ) free(errdescr);
185+
char *res = strdup(buffer.c_str());
186+
if ( res==NULL ) abort();
203187

204-
return buffer;
188+
return res;
205189
}
206190

207191
/* Function to generate and write error logmessage (using vlogmsg) */

lib/lib.error.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
#ifndef LIB_ERROR_H
66
#define LIB_ERROR_H
77

8-
#include <stdarg.h>
9-
#include <stdio.h>
10-
#include <errno.h>
8+
#include <cstdarg>
9+
#include <cstdio>
10+
#include <cerrno>
1111
#ifdef HAVE_SYSLOG_H
1212
#include <syslog.h>
1313
#else
@@ -20,10 +20,6 @@
2020
#define WARNSTR "warning"
2121
#define WARNMATCH WARNSTR": "
2222

23-
#ifdef __cplusplus
24-
extern "C" {
25-
#endif
26-
2723
extern const int exit_failure;
2824

2925
/* Import from the main program for logging purposes */
@@ -89,8 +85,4 @@ char *vallocstr(const char *, va_list);
8985
* Returns a pointer to the allocated string
9086
*/
9187

92-
#ifdef __cplusplus
93-
}
94-
#endif
95-
9688
#endif /* LIB_ERROR_H */

0 commit comments

Comments
 (0)