utility.c
Go to the documentation of this file.
1 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include <math.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <strings.h>
26 #include <unistd.h>
27 
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <time.h>
33 
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif // HAVE_CONFIG_H
37 
38 #include <utility.h>
39 
40 #ifdef __WIN32
41 #include <windows.h>
42 #endif // __WIN32
43 
44 /* get time stamp */
45 double get_time(void)
46 {
47  struct timeval current;
48 
49  gettimeofday(&current, NULL);
50 
51  return current.tv_sec + current.tv_usec / 1000000.0;
52 }
53 
54 void yp_usleep(int usec)
55 {
56 #if defined(HAVE_NANOSLEEP)
57  // nanosleepが利用可能
58  struct timespec request;
59  request.tv_sec = usec / 1000000;
60  request.tv_nsec = (usec - request.tv_sec * 1000000) * 1000;
61 
62  nanosleep(&request, NULL);
63 #elif defined(__MINGW32__)
64  // MinGWのusleepには1ms以下切り捨ての問題があるためWindows環境ではWinAPIのSleepを使う
65  // 1ms以下は切り上げ
66  Sleep((usec + 999) / 1000);
67 #else
68  // 古いシステム用
69  usleep(usec);
70 #endif // defined(HAVE_NANOSLEEP)
71 }
72 
74 {
75  // Windows環境で標準出力がバッファリングされないように設定
76  setvbuf(stdout, 0, _IONBF, 0);
77  setvbuf(stderr, 0, _IONBF, 0);
78 }
79 
80 #if !defined(HAVE_STRTOK_R)
81 #ifndef strtok_r
82 
83 /*
84  * public domain strtok_r() by Charlie Gordon
85  * from comp.lang.c 9/14/2007
86  * http://groups.google.com/group/comp.lang.c/msg/2ab1ecbb86646684
87  * (Declaration that it's public domain):
88  * http://groups.google.com/group/comp.lang.c/msg/7c7b39328fefab9c
89  */
90 
91 char *strtok_r(char *str, const char *delim, char **nextp)
92 {
93  char *ret;
94 
95  if (str == NULL)
96  str = *nextp;
97 
98  str += strspn(str, delim);
99 
100  if (*str == '\0')
101  return NULL;
102 
103  ret = str;
104  str += strcspn(str, delim);
105 
106  if (*str)
107  *str++ = '\0';
108 
109  *nextp = str;
110 
111  return ret;
112 }
113 
114 #endif // strtok_r
115 #endif // !defined(HAVE_STRTOK_R)
void hook_pre_global()
Definition: utility.c:73
void yp_usleep(int usec)
Definition: utility.c:54
char * strtok_r(char *str, const char *delim, char **nextp)
Definition: utility.c:91
double get_time(void)
Definition: utility.c:45


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24