utility.c
Go to the documentation of this file.
00001 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
00002 //
00003 // Permission is hereby granted, free of charge, to any person obtaining a copy
00004 // of this software and associated documentation files (the "Software"), to
00005 // deal in the Software without restriction, including without limitation the
00006 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
00007 // sell copies of the Software, and to permit persons to whom the Software is
00008 // furnished to do so, subject to the following conditions:
00009 //
00010 // The above copyright notice and this permission notice shall be included in
00011 // all copies or substantial portions of the Software.
00012 //
00013 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00016 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00019 // SOFTWARE.
00020 
00021 #include <math.h>
00022 #include <stdarg.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <strings.h>
00026 #include <unistd.h>
00027 
00028 #include <fcntl.h>
00029 #include <sys/stat.h>
00030 #include <sys/time.h>
00031 #include <sys/types.h>
00032 #include <time.h>
00033 
00034 #ifdef HAVE_CONFIG_H
00035 #include <config.h>
00036 #endif  // HAVE_CONFIG_H
00037 
00038 #include <utility.h>
00039 
00040 #ifdef __WIN32
00041 #include <windows.h>
00042 #endif  // __WIN32
00043 
00044 /* get time stamp */
00045 double get_time(void)
00046 {
00047   struct timeval current;
00048 
00049   gettimeofday(&current, NULL);
00050 
00051   return current.tv_sec + current.tv_usec / 1000000.0;
00052 }
00053 
00054 void yp_usleep(int usec)
00055 {
00056 #if defined(HAVE_NANOSLEEP)
00057   // nanosleepが利用可能
00058   struct timespec request;
00059   request.tv_sec = usec / 1000000;
00060   request.tv_nsec = (usec - request.tv_sec * 1000000) * 1000;
00061 
00062   nanosleep(&request, NULL);
00063 #elif defined(__MINGW32__)
00064   // MinGWのusleepには1ms以下切り捨ての問題があるためWindows環境ではWinAPIのSleepを使う
00065   // 1ms以下は切り上げ
00066   Sleep((usec + 999) / 1000);
00067 #else
00068   // 古いシステム用
00069   usleep(usec);
00070 #endif  // defined(HAVE_NANOSLEEP)
00071 }
00072 
00073 void hook_pre_global()
00074 {
00075   // Windows環境で標準出力がバッファリングされないように設定
00076   setvbuf(stdout, 0, _IONBF, 0);
00077   setvbuf(stderr, 0, _IONBF, 0);
00078 }
00079 
00080 #if !defined(HAVE_STRTOK_R)
00081 #ifndef strtok_r
00082 
00083 /* 
00084  * public domain strtok_r() by Charlie Gordon
00085  *   from comp.lang.c  9/14/2007
00086  *      http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
00087  *     (Declaration that it's public domain):
00088  *      http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
00089  */
00090 
00091 char *strtok_r(char *str, const char *delim, char **nextp)
00092 {
00093   char *ret;
00094 
00095   if (str == NULL)
00096     str = *nextp;
00097 
00098   str += strspn(str, delim);
00099 
00100   if (*str == '\0')
00101     return NULL;
00102 
00103   ret = str;
00104   str += strcspn(str, delim);
00105 
00106   if (*str)
00107     *str++ = '\0';
00108 
00109   *nextp = str;
00110 
00111   return ret;
00112 }
00113 
00114 #endif  // strtok_r
00115 #endif  // !defined(HAVE_STRTOK_R)


yp-spur
Author(s):
autogenerated on Fri May 10 2019 02:52:19