]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/conv.c
Reformat the sources with orte/uncrustify script
[orte.git] / orte / liborte / conv.c
1 /*
2  *  $Id: conv.c,v 0.0.0.1               2003/09/10
3  *
4  *  DEBUG:  section 7                 ordering conversion
5  *
6  *  -------------------------------------------------------------------
7  *                                ORTE
8  *                      Open Real-Time Ethernet
9  *
10  *                      Copyright (C) 2001-2006
11  *  Department of Control Engineering FEE CTU Prague, Czech Republic
12  *                      http://dce.felk.cvut.cz
13  *                      http://www.ocera.org
14  *
15  *  Author:              Petr Smolik    petr@smoliku.cz
16  *  Advisor:             Pavel Pisa
17  *  Project Responsible: Zdenek Hanzalek
18  *  --------------------------------------------------------------------
19  *
20  *  This program is free software; you can redistribute it and/or modify
21  *  it under the terms of the GNU General Public License as published by
22  *  the Free Software Foundation; either version 2 of the License, or
23  *  (at your option) any later version.
24  *
25  *  This program is distributed in the hope that it will be useful,
26  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
27  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  *  GNU General Public License for more details.
29  *
30  *  This module maintains a hash table of key/value pairs.
31  *  Keys can be strings of any size, or numbers up to size
32  *  unsigned long (HASHKEYTYPE).
33  *  Values should be a pointer to some data you wish to store.
34  *
35  */
36
37 #include "orte_all.h"
38
39 /**********************************************************************************/
40 //get part of string, div. by semi.
41 int
42 getStringPart(char *string, char divChar, int *iterator, char *buff)
43 {
44   char *cp;
45   int len;
46
47   if (!string || !buff)
48     return -1;
49   len = strlen(string);
50   if (len < (*iterator))
51     return -2;
52   cp = string+(*iterator);
53   if (cp[0] != 0) {  //till is length>0
54     char *dcp, tcp;
55     dcp = strchr(cp, divChar);
56     if (!dcp)
57       dcp = cp+strlen(cp);
58     tcp = *dcp;         //save ending value
59     *dcp = 0;           //temporary end of string
60     strcpy((char *)buff, cp);
61     *dcp = tcp;         //restore value
62     if (dcp[0] != 0)
63       (*iterator) += dcp-cp+1;           //next value
64     else
65       (*iterator) = len;
66     return 1;
67   }
68   return 0;
69 }
70
71 /**********************************************************************************/
72 char *
73 IPAddressToString(IPAddress ipAddress, char *buff)
74 {
75   struct in_addr addr;
76
77   addr.s_addr = htonl(ipAddress);
78   sprintf(buff, "%s", inet_ntoa(addr));
79   return buff;
80 }
81
82 /**********************************************************************************/
83 IPAddress
84 StringToIPAddress(const char *string)
85 {
86   IPAddress ipAddress = IPADDRESS_INVALID;
87
88 #ifdef CONFIG_ORTE_MINGW
89   unsigned long inetAddress = inet_addr(string);
90 #else
91   in_addr_t inetAddress = inet_addr(string);
92 #endif
93
94   if (inetAddress != INADDR_NONE) {
95     ipAddress = ntohl(inetAddress);
96   }
97 #if defined HAVE_GETHOSTBYNAME
98   {
99     struct hostent *hostname;
100     if (ipAddress == IPADDRESS_INVALID) {
101       if ((hostname = gethostbyname(string))) {
102         ipAddress = ntohl(*((long *)(hostname->h_addr_list[0])));
103       }
104     }
105   }
106 #endif
107   return ipAddress;
108 }
109
110 /**********************************************************************************/
111 char *
112 NtpTimeToStringMs(NtpTime time, char *buff)
113 {
114   int s, msec;
115
116   NtpTimeDisAssembToMs(s, msec, time);
117   sprintf(buff, "%d.%03d", s, msec);
118   return buff;
119 }
120
121 /**********************************************************************************/
122 char *
123 NtpTimeToStringUs(NtpTime time, char *buff)
124 {
125   int s, usec;
126
127   NtpTimeDisAssembToUs(s, usec, time);
128   sprintf(buff, "%d.%06d", s, usec);
129   return buff;
130 }