breakhack/src/util.c

186 lines
3.7 KiB
C
Raw Normal View History

2018-02-16 18:11:26 +01:00
/*
* BreakHack - A dungeone crawler RPG
* Copyright (C) 2018 Linus Probert <linus.probert@gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
2017-11-30 21:00:47 +01:00
#include <stdio.h>
#include <stdlib.h>
2017-12-21 12:35:52 +01:00
#include <string.h>
#include <stdarg.h>
2018-01-24 09:35:21 +01:00
#include <time.h>
2018-01-25 10:45:05 +01:00
#include <ctype.h>
#include "defines.h"
2017-12-21 12:41:08 +01:00
#ifndef _WIN32
2017-11-30 21:00:47 +01:00
#include <unistd.h>
#endif // _WIN32
2017-11-30 21:00:47 +01:00
#include "util.h"
void
m_strcpy(char *dest, size_t destsz, const char *src)
{
2017-12-21 12:41:08 +01:00
#ifndef _MSC_VER
UNUSED(destsz);
2018-03-22 12:52:27 +01:00
strncpy(dest, src, destsz);
2017-12-21 12:41:08 +01:00
#else // _MSC_VER
strcpy_s(dest, destsz, src);
2017-12-21 12:41:08 +01:00
#endif // _MSC_VER
}
void
2017-12-19 22:51:00 +01:00
m_strncat(char *dest,
size_t destsz,
2017-12-19 22:51:00 +01:00
char *src,
size_t srcsz)
{
2017-12-21 12:41:08 +01:00
#ifndef _MSC_VER
UNUSED(destsz);
UNUSED(srcsz);
strncat(dest, src, srcsz);
2017-12-21 12:41:08 +01:00
#else // _MSC_VER
strncat_s(dest, destsz, src, srcsz);
2017-12-21 12:41:08 +01:00
#endif // _MSC_VER
}
void
m_sprintf(char * dest, size_t destsz, const char * format, ...)
{
va_list args;
va_start(args, format);
#ifndef _MSC_VER
UNUSED(destsz);
2018-03-22 12:52:27 +01:00
vsnprintf(dest, destsz, format, args);
#else // _MSC_VER
vsprintf_s(dest, destsz, format, args);
#endif // _MSC_VER
va_end(args);
}
void
m_vsprintf(char *dest, size_t sz, const char *fmt, va_list args)
{
#ifndef _MSC_VER
UNUSED (sz);
2018-03-22 12:52:27 +01:00
vsnprintf(dest, sz, fmt, args);
#else // _MSC_VER
vsprintf_s(dest, sz, fmt, args);
#endif // _MSC_VER
}
void
log_print(FILE *out,
const char *prefix,
const char *file,
int line,
const char *function,
const char * fmt,
...)
2017-11-30 21:00:47 +01:00
{
va_list args;
2018-01-24 09:35:21 +01:00
char tstamp[10];
timestamp(tstamp, 10);
#ifdef DEBUG
#ifndef _WIN32
if (out == stdout || out == stderr) {
fprintf(out, "\033[34m[%s]", tstamp);
if (strcmp(prefix, "DEBUG") == 0)
fprintf(out, "\033[33m");
else if (strcmp(prefix, "FATAL") == 0 || strcmp(prefix, "ERROR") == 0)
fprintf(out, "\033[31m");
else
fprintf(out, "\033[32m");
fprintf(out, "[%5s]", prefix);
fprintf(out, "\033[36m[%20s:%-3d]\033[37m[%20s()]\033[0m ",
file, line, function);
} else {
fprintf(out, "[%s][%5s][%20s:%-3d][%20s()] ", tstamp, prefix, file, line, function);
}
#else // _WIN32
fprintf(out, "[%s][%5s][%20s:%-3d][%20s()] ", tstamp, prefix, file, line, function);
#endif // _WIN32
#else // DEBUG
UNUSED(prefix);
UNUSED(file);
UNUSED(line);
UNUSED(function);
#endif // DEBUG
va_start(args, fmt);
vfprintf(out, fmt, args);
va_end(args);
printf("\n");
}
2017-11-30 21:00:47 +01:00
void *
ec_malloc(unsigned long size)
2017-11-30 21:00:47 +01:00
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
2018-01-24 09:35:21 +01:00
void *
ec_calloc(size_t count, unsigned long size)
{
void *ptr;
ptr = calloc(count, size);
if (ptr == NULL)
fatal("in ec_calloc() on memory allocation");
return ptr;
}
2018-01-24 09:35:21 +01:00
void
timestamp(char *tstamp, size_t sz)
{
time_t cTime;
struct tm *tm_info;
time(&cTime);
2018-01-25 08:45:57 +01:00
#ifdef _MSC_VER
2018-01-24 19:24:05 +01:00
tm_info = ec_malloc(sizeof(struct tm));
localtime_s(tm_info, &cTime);
2018-01-25 08:45:57 +01:00
#else // _MSC_VER
tm_info = localtime(&cTime);
2018-01-24 19:24:05 +01:00
#endif // _MSC_VER
2018-01-24 09:35:21 +01:00
strftime(tstamp, sz, "%H:%M:%S", tm_info);
2018-01-24 19:24:05 +01:00
2018-01-25 08:45:57 +01:00
#ifdef _MSC_VER
2018-01-24 19:24:05 +01:00
free(tm_info);
2018-01-25 08:45:57 +01:00
#endif // _MSC_VER
2018-01-24 09:35:21 +01:00
}
2018-01-25 10:45:05 +01:00
char *
to_lower(const char *str)
{
char *lcstr;
unsigned int i;
2018-02-22 09:44:27 +01:00
lcstr = ec_malloc(((unsigned int) strlen(str) + 1) * sizeof(char));
2018-01-25 10:45:05 +01:00
for (i = 0; i < strlen(str); ++i) {
2018-02-22 09:44:27 +01:00
lcstr[i] = (char) tolower(str[i]);
2018-01-25 10:45:05 +01:00
}
lcstr[i] = '\0';
return lcstr;
}