61#ifdef HAVE_SYS_IOCTL_H
64#ifdef HAVE_SYS_SELECT_H
65#include <sys/select.h>
70#ifdef FREECIV_HAVE_SYS_TYPES_H
79#ifdef HAVE_SYS_UTSNAME_H
80#include <sys/utsname.h>
82#ifdef FREECIV_HAVE_LIBZ
85#ifdef FREECIV_MSWINDOWS
97#ifdef FREECIV_HAVE_LIBZ
102#include "unicode/ustring.h"
121#ifndef HAVE_WORKING_VSNPRINTF
126#ifndef HAVE_LOCALTIME_R
191 UErrorCode err_code = U_ZERO_ERROR;
194 bool enough_mem =
FALSE;
210 while (!enough_mem) {
211 UErrorCode err_code0 = U_ZERO_ERROR;
212 UErrorCode err_code1 = U_ZERO_ERROR;
219 if (err_code0 == U_BUFFER_OVERFLOW_ERROR || err_code1 == U_BUFFER_OVERFLOW_ERROR) {
240 UErrorCode err_code = U_ZERO_ERROR;
243 bool enough_mem =
FALSE;
259 while (!enough_mem) {
260 UErrorCode err_code0 = U_ZERO_ERROR;
261 UErrorCode err_code1 = U_ZERO_ERROR;
268 if (err_code0 == U_BUFFER_OVERFLOW_ERROR || err_code1 == U_BUFFER_OVERFLOW_ERROR) {
302 const char *
const max = buf + buf_len - 2;
304 while (*
str !=
'\0' && dest < max) {
334 char *buf,
size_t buf_len)
337 const char *
const max = buf + buf_len - 1;
339 while (*
str !=
'\0' && dest < max) {
340 if (*
str ==
'\\' && *(
str + 1) ==
'\n') {
343 }
else if (full_escapes && *
str ==
'\\') {
368 if (
str[0] ==
'"' &&
str[
len-1] ==
'"') {
396 if (str0[0] ==
'"') {
397 if (str0[len0 - 1] ==
'"') {
404 if (str1[0] ==
'"') {
405 if (str1[len1 - 1] ==
'"') {
412 if (len0 <
n || len1 <
n) {
424 for (i = 0; i < cmplen ; i++, str0++, str1++) {
426 return ((
int) (
unsigned char)
fc_tolower(*str0))
441#ifdef HAVE_STRCASESTR
442 return strcasestr(haystack, needle);
448 if (NULL == needle ||
'\0' == *needle) {
449 return (
char *)haystack;
451 if (NULL == haystack ||
'\0' == *haystack) {
454 haystacks = strlen(haystack);
455 needles = strlen(needle);
456 if (haystacks < needles) {
460 for (p = haystack; p <= &haystack[haystacks - needles]; p++) {
474#if defined(ENABLE_NLS) && defined(HAVE_STRCOLL)
475 return strcoll(str0, str1);
476#elif defined(ENABLE_NLS) && defined(HAVE__STRCOLL)
477 return _strcoll(str0, str1);
479 return strcmp(str0, str1);
491#if defined(ENABLE_NLS) && defined(HAVE__STRICOLL)
492 return _stricoll(str0, str1);
493#elif defined(ENABLE_NLS) && defined(HAVE_STRICOLL)
494 return stricoll(str0, str1);
495#elif defined(ENABLE_NLS) && defined(HAVE_STRCASECOLL)
496 return strcasecoll(str0, str1);
506FILE *
fc_fopen(
const char *filename,
const char *opentype)
510#ifdef FREECIV_MSWINDOWS
513 const char *real_filename = filename;
517 if (fopen_s(&result, real_filename, opentype) != 0) {
521 result = fopen(real_filename, opentype);
524#ifdef FREECIV_MSWINDOWS
535#ifdef FREECIV_HAVE_LIBZ
536gzFile fc_gzopen(
const char *filename,
const char *opentype)
538#ifdef FREECIV_MSWINDOWS
540 char *filename_in_local_encoding =
543 result = gzopen(filename_in_local_encoding, opentype);
544 free(filename_in_local_encoding);
547 return gzopen(filename, opentype);
558#ifdef FREECIV_MSWINDOWS
560 char *filename_in_local_encoding =
563 result = remove(filename_in_local_encoding);
564 free(filename_in_local_encoding);
567 return remove(filename);
575int fc_stat(
const char *filename,
struct stat *buf)
577#ifdef FREECIV_MSWINDOWS
579 char *filename_in_local_encoding =
582 result = stat(filename_in_local_encoding, buf);
583 free(filename_in_local_encoding);
586 return stat(filename, buf);
595#ifdef FREECIV_MSWINDOWS
596 return GetLastError();
612#ifdef FREECIV_MSWINDOWS
613 static char buf[256];
615 if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
616 NULL, err, 0, buf,
sizeof(buf), NULL)) {
618 _(
"error %ld (failed FormatMessage)"), err);
623 static char buf[256];
631 _(
"error %d (compiled without strerror)"), err);
645 if (usec >= 1000000) {
646 ts.tv_sec = usec / 1000000;
647 ts.tv_nsec = (usec % 1000000) * 1000;
650 ts.tv_nsec = usec * 1000;
653 nanosleep(&ts, NULL);
662 EventRecord the_event;
669 WaitNextEvent(0, &the_event, usec, 0L);
671#ifdef FREECIV_MSWINDOWS
700 if (search == NULL || replace == NULL) {
704 len_max = ceil((
double)strlen(
str) * strlen(replace) / strlen(search)) + 1;
705 if ((*
len) < len_max) {
712#ifndef FREECIV_NDEBUG
719 "Can't replace '%s' by '%s' in '%s'. Too small "
720 "size after reallocation: " SIZE_T_PRINTF
".",
721 search, replace,
str, *
len);
734 size_t len_search, len_replace;
738 if (search == NULL || replace == NULL) {
742 len_search = strlen(search);
743 len_replace = strlen(replace);
747 p = strstr(s, search);
753 if (
len < (strlen(
str) + len_replace - len_search + 1)) {
758 memmove(p + len_replace, p + len_search, strlen(p + len_search) + 1);
759 memcpy(p, replace, len_replace);
789 bool enough_mem =
FALSE;
792 UErrorCode err_code = U_ZERO_ERROR;
804 while (!enough_mem) {
809 if (err_code == U_BUFFER_OVERFLOW_ERROR) {
811 err_code = U_ZERO_ERROR;
817 u_strToUTF8(dest,
n - 1, &dlen,
icu_buffer1, slen, &err_code);
836 start = strlen(dest);
840 return fc_strlcpy(dest + start, src,
n - start) + start;
895#define VSNP_BUF_SIZE (8096*1024)
898#ifdef HAVE_WORKING_VSNPRINTF
907#ifdef HAVE_WORKING_VSNPRINTF
908 r = vsnprintf(
str,
n, format, ap);
919 fprintf(stderr,
"fc_vsnprintf() call with length " SIZE_T_PRINTF
"."
930 fprintf(stderr,
"Could not allocate %i bytes for vsnprintf() "
948 fprintf(stderr,
"Overflow in vsnprintf replacement!"
974 va_start(ap, format);
1006 va_start(ap, format);
1010 return (-1 == ret ? -1 : ret +
len);
1018#ifdef HAVE_GETHOSTNAME
1019 return gethostname(buf,
len);
1025#ifdef FREECIV_SOCKET_ZERO_NOT_STDIN
1030#define CONSOLE_BUF_SIZE 100
1031static char console_buf[CONSOLE_BUF_SIZE + 1];
1035#ifdef FREECIV_MSWINDOWS
1036static HANDLE console_thread = INVALID_HANDLE_VALUE;
1041static DWORD WINAPI windows_console_thread(LPVOID arg)
1043 if (fgets(console_buf, CONSOLE_BUF_SIZE, stdin)) {
1046 if ((s = strchr(console_buf,
'\n'))) {
1058void fc_init_console(
void)
1060#ifdef FREECIV_MSWINDOWS
1061 if (console_thread != INVALID_HANDLE_VALUE) {
1065 console_buf[0] =
'\0';
1066 console_thread = (HANDLE) CreateThread(NULL, 0, windows_console_thread,
1087char *fc_read_console(
void)
1089#ifdef FREECIV_MSWINDOWS
1090 if (WaitForSingleObject(console_thread, 0) == WAIT_OBJECT_0) {
1091 CloseHandle(console_thread);
1092 console_thread = INVALID_HANDLE_VALUE;
1100 static char *bufptr = console_buf;
1104 while ((*bufptr = fgetc(stdin)) != EOF) {
1105 if (*bufptr ==
'\n') {
1108 if (*bufptr ==
'\0') {
1109 bufptr = console_buf;
1113 if ((bufptr - console_buf) <= CONSOLE_BUF_SIZE) {
1134 return S_ISREG(tmp.st_mode);
1136 return write_access && errno == ENOENT;
1146 size_t slen = (size_t)strlen(
str);
1152 if (
str && *
str !=
'\0' && slen > desired_len) {
1158 for (c =
str; c <
str + desired_len; c++) {
1160 slen -= c + 1 -
str;
1167 for (c =
str + desired_len; c >
str; c--) {
1170 slen -= c + 1 -
str;
1177 for (c =
str + desired_len + 1; *c !=
'\0'; c++) {
1180 slen -= c + 1 -
str;
1207 if (128 <= (
unsigned char) c) {
1210 return isalnum((
int) ((
unsigned char) c)) != 0;
1218 if (128 <= (
unsigned char) c) {
1221 return isalpha((
int) ((
unsigned char) c)) != 0;
1229 if (128 <= (
unsigned char) c) {
1232 return isdigit((
int) ((
unsigned char) c)) != 0;
1240 if (128 <= (
unsigned char) c) {
1243 return isprint((
int) ((
unsigned char) c)) != 0;
1251 if (128 <= (
unsigned char) c) {
1254 return isspace((
int) ((
unsigned char) c)) != 0;
1262 if (128 <= (
unsigned char) c) {
1265 return isupper((
int) ((
unsigned char) c)) != 0;
1273 if (128 <= (
unsigned char) c) {
1276 return (
char) toupper((
int) ((
unsigned char) c));
1284 if (128 <= (
unsigned char) c) {
1287 return (
char) tolower((
int) ((
unsigned char) c));
1298 static char buf[2048];
1304 return basename(buf);
1312#ifdef HAVE_LOCALTIME_R
1313 return localtime_r(timep, result);
1316 memcpy(result, localtime(timep),
sizeof(
struct tm));
1328#ifdef HAVE_AT_QUICK_EXIT
1329 return at_quick_exit(
func);
1342#ifndef HAVE_WORKING_VSNPRINTF
1346#ifndef HAVE_LOCALTIME_R
1360#ifndef HAVE_WORKING_VSNPRINTF
1368#ifndef HAVE_LOCALTIME_R
char * internal_to_local_string_malloc(const char *text)
char * local_to_internal_string_buffer(const char *text, char *buf, size_t bufsz)
void fc_allocate_mutex(fc_mutex *mutex)
void fc_release_mutex(fc_mutex *mutex)
void fc_destroy_mutex(fc_mutex *mutex)
void fc_init_mutex(fc_mutex *mutex)
#define fc_assert(condition)
#define fc_assert_ret_val(condition, val)
#define fc_assert_ret_val_msg(condition, val, message,...)
#define fc_realloc(ptr, sz)
void fc_nonblock(int sockfd)
int fc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, fc_timeval *timeout)
struct timeval fc_timeval
int fc_snprintf(char *str, size_t n, const char *format,...)
int fc_gethostname(char *buf, size_t len)
size_t fc_strlcpy(char *dest, const char *src, size_t n)
static bool support_initialized
void make_escapes(const char *str, char *buf, size_t buf_len)
int fc_strcasecmp(const char *str0, const char *str1)
void fc_usleep(unsigned long usec)
void fc_support_init(void)
const char * fc_strerror(fc_errno err)
size_t fc_strlcat(char *dest, const char *src, size_t n)
char * fc_strcasestr(const char *haystack, const char *needle)
bool fc_strrep(char *str, size_t len, const char *search, const char *replace)
struct tm * fc_localtime(const time_t *timep, struct tm *result)
int cat_snprintf(char *str, size_t n, const char *format,...)
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
FILE * fc_fopen(const char *filename, const char *opentype)
static void fc_strAPI_free(void)
int fc_break_lines(char *str, size_t desired_len)
static fc_mutex localtime_mutex
static UChar * icu_buffer1
fc_mutex icu_buffer_mutex
fc_errno fc_get_errno(void)
void fc_support_free(void)
void remove_escapes(const char *str, bool full_escapes, char *buf, size_t buf_len)
int fc_strcoll(const char *str0, const char *str1)
int fc_stat(const char *filename, struct stat *buf)
bool is_reg_file_for_access(const char *name, bool write_access)
const char * fc_basename(const char *path)
static void icu_buffers_initial(void)
static char * vsnprintf_buf
static int icu_buffer_uchars
int fc_strncasequotecmp(const char *str0, const char *str1, size_t n)
static fc_mutex vsnprintf_mutex
static void fc_strAPI_init(void)
size_t effectivestrlenquote(const char *str)
static void icu_buffers_increase(void)
int fc_at_quick_exit(void(*func)(void))
int fc_stricoll(const char *str0, const char *str1)
int fc_remove(const char *filename)
int fc_strncasecmp(const char *str0, const char *str1, size_t n)
char * fc_strrep_resize(char *str, size_t *len, const char *search, const char *replace)
static UChar * icu_buffer2
bool are_support_services_available(void)