CUBRID Engine  latest
cas_util.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Search Solution Corporation
3  * Copyright 2016 CUBRID Corporation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 
20 /*
21  * cas_util.c -
22  */
23 
24 #ident "$Id$"
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 
31 #if defined(WINDOWS)
32 #include <sys/timeb.h>
33 #else
34 #include <sys/time.h>
35 #endif
36 #include <assert.h>
37 
38 #include "cas_common.h"
39 #include "cas_util.h"
40 #include "cas_net_buf.h"
41 
42 char *
43 ut_uchar2ipstr (unsigned char *ip_addr)
44 {
45  static char ip_str[32];
46 
47  assert (ip_addr != NULL);
48 
49  sprintf (ip_str, "%d.%d.%d.%d", (unsigned char) ip_addr[0], (unsigned char) ip_addr[1], (unsigned char) ip_addr[2],
50  (unsigned char) ip_addr[3]);
51  return (ip_str);
52 }
53 
54 char *
55 ut_trim (char *str)
56 {
57  char *p;
58  char *s;
59 
60  if (str == NULL)
61  return (str);
62 
63  for (s = str; *s != '\0' && (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r'); s++)
64  ;
65  if (*s == '\0')
66  {
67  *str = '\0';
68  return (str);
69  }
70 
71  /* *s must be a non-white char */
72  for (p = s; *p != '\0'; p++)
73  ;
74  for (p--; *p == ' ' || *p == '\t' || *p == '\n' || *p == '\r'; p--)
75  ;
76  *++p = '\0';
77 
78  if (s != str)
79  memmove (str, s, strlen (s) + 1);
80 
81  return (str);
82 }
83 
84 
85 void
86 ut_tolower (char *str)
87 {
88  char *p;
89 
90  if (str == NULL)
91  return;
92 
93  for (p = str; *p; p++)
94  {
95  if (*p >= 'A' && *p <= 'Z')
96  *p = *p - 'A' + 'a';
97  }
98 }
99 
100 void
101 ut_timeval_diff (struct timeval *start, struct timeval *end, int *res_sec, int *res_msec)
102 {
103  int sec, msec;
104 
105  assert (start != NULL && end != NULL && res_sec != NULL && res_msec != NULL);
106 
107  sec = end->tv_sec - start->tv_sec;
108  msec = (end->tv_usec / 1000) - (start->tv_usec / 1000);
109  if (msec < 0)
110  {
111  msec += 1000;
112  sec--;
113  }
114  *res_sec = sec;
115  *res_msec = msec;
116 }
117 
118 int
119 ut_check_timeout (struct timeval *start_time, struct timeval *end_time, int timeout_msec, int *res_sec, int *res_msec)
120 {
121  struct timeval cur_time;
122  int diff_msec;
123 
124  assert (start_time != NULL && res_sec != NULL && res_msec != NULL);
125 
126  if (end_time == NULL)
127  {
128  end_time = &cur_time;
129  gettimeofday (end_time, NULL);
130  }
131  ut_timeval_diff (start_time, end_time, res_sec, res_msec);
132 
133  if (timeout_msec > 0)
134  {
135  diff_msec = *res_sec * 1000 + *res_msec;
136  }
137  else
138  {
139  diff_msec = -1;
140  }
141 
142  return (diff_msec >= timeout_msec) ? diff_msec : -1;
143 }
void ut_tolower(char *str)
Definition: cas_util.c:86
#define assert(x)
#define NULL
Definition: freelistheap.h:34
static struct timeval start_time
int ut_check_timeout(struct timeval *start_time, struct timeval *end_time, int timeout_msec, int *res_sec, int *res_msec)
Definition: cas_util.c:119
#define strlen(s1)
Definition: intl_support.c:43
char * ut_trim(char *str)
Definition: cas_util.c:55
char * ut_uchar2ipstr(unsigned char *ip_addr)
Definition: cas_util.c:43
void ut_timeval_diff(struct timeval *start, struct timeval *end, int *res_sec, int *res_msec)
Definition: cas_util.c:101
const char ** p
Definition: dynamic_load.c:945