subrascasse

Sub Rascasse: an iPhone dive log for Subsurface cloud storage, with a pure-Swift git client (GPL-2.0)
git clone https://git.schurger.org/subrascasse.git
Log | Files | Refs | README | LICENSE

platform.c (2270B)


      1 /*
      2  * libdivecomputer
      3  *
      4  * Copyright (C) 2021 Jef Driesen
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Lesser General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2.1 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Lesser General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Lesser General Public
     17  * License along with this library; if not, write to the Free Software
     18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
     19  * MA 02110-1301 USA
     20  */
     21 
     22 #ifdef _WIN32
     23 #define WIN32_LEAN_AND_MEAN
     24 #define NOGDI
     25 #include <windows.h>
     26 #else
     27 #include <time.h>
     28 #include <errno.h>
     29 #endif
     30 
     31 #include <stdio.h>
     32 
     33 #include "platform.h"
     34 
     35 int
     36 dc_platform_sleep (unsigned int milliseconds)
     37 {
     38 #ifdef _WIN32
     39 	Sleep (milliseconds);
     40 #else
     41 	struct timespec ts;
     42 	ts.tv_sec  = (milliseconds / 1000);
     43 	ts.tv_nsec = (milliseconds % 1000) * 1000000;
     44 
     45 	while (nanosleep (&ts, &ts) != 0) {
     46 		if (errno != EINTR ) {
     47 			return -1;
     48 		}
     49 	}
     50 #endif
     51 
     52 	return 0;
     53 }
     54 
     55 int
     56 dc_platform_vsnprintf (char *str, size_t size, const char *format, va_list ap)
     57 {
     58 	int n = 0;
     59 
     60 	if (size == 0)
     61 		return -1;
     62 
     63 #ifdef _MSC_VER
     64 	/*
     65 	 * The non-standard vsnprintf implementation provided by MSVC doesn't null
     66 	 * terminate the string and returns a negative value if the destination
     67 	 * buffer is too small.
     68 	 */
     69 	n = _vsnprintf (str, size - 1, format, ap);
     70 	if (n == size - 1 || n < 0)
     71 		str[size - 1] = 0;
     72 #else
     73 	/*
     74 	 * The C99 vsnprintf function will always null terminate the string. If the
     75 	 * destination buffer is too small, the return value is the number of
     76 	 * characters that would have been written if the buffer had been large
     77 	 * enough.
     78 	 */
     79 	n = vsnprintf (str, size, format, ap);
     80 	if (n >= 0 && (size_t) n >= size)
     81 		n = -1;
     82 #endif
     83 
     84 	return n;
     85 }
     86 
     87 int
     88 dc_platform_snprintf (char *str, size_t size, const char *format, ...)
     89 {
     90 	va_list ap;
     91 	int n = 0;
     92 
     93 	va_start (ap, format);
     94 	n = dc_platform_vsnprintf (str, size, format, ap);
     95 	va_end (ap);
     96 
     97 	return n;
     98 }