]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/conv.c
05b5d408bef953d73664e73d54c454ba77ae9c51
[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   char *cp;
44   int len;
45   
46   if (!string || !buff) return -1;
47   len=strlen(string);
48   if (len<(*iterator)) return -2;
49   cp=string+(*iterator);
50   if (cp[0]!=0) {  //till is length>0
51     char *dcp,tcp; 
52     dcp=strchr(cp,divChar);
53     if (!dcp) dcp=cp+strlen(cp);
54     tcp=*dcp;         //save ending value
55     *dcp=0;           //temporary end of string
56     strcpy((char *)buff,cp);  
57     *dcp=tcp;         //restore value
58     if (dcp[0]!=0) (*iterator)+=dcp-cp+1;//next value
59     else (*iterator)=len;
60     return 1;
61   }
62   return 0;
63 }
64
65 /**********************************************************************************/
66 char* 
67 IPAddressToString(IPAddress ipAddress,char *buff) {
68   struct in_addr addr;
69   
70   addr.s_addr=htonl(ipAddress);
71   sprintf(buff,"%s",inet_ntoa(addr));
72   return buff;
73 }
74
75 /**********************************************************************************/
76 IPAddress 
77 StringToIPAddress(const char *string) {
78   IPAddress ipAddress=IPADDRESS_INVALID;
79 #ifdef CONFIG_ORTE_MINGW   
80   unsigned long inetAddress = inet_addr(string);
81 #else
82   in_addr_t inetAddress = inet_addr(string);
83 #endif
84   
85   if(inetAddress!=INADDR_NONE) {
86     ipAddress=ntohl(inetAddress);
87   }
88 #if defined HAVE_GETHOSTBYNAME
89   {
90     struct hostent *hostname; 
91     if (ipAddress==IPADDRESS_INVALID) {
92       if ((hostname=gethostbyname(string))) {
93         ipAddress=ntohl(*((long*)(hostname->h_addr_list[0])));
94       }
95     }
96   }
97 #endif
98   return ipAddress;
99 }
100
101 /**********************************************************************************/
102 char *
103 NtpTimeToStringMs(NtpTime time,char *buff) {
104   int s,msec;
105   
106   NtpTimeDisAssembToMs(s, msec, time);
107   sprintf(buff,"%d.%03d",s,msec);
108   return buff;
109 }
110
111 /**********************************************************************************/
112 char *
113 NtpTimeToStringUs(NtpTime time,char *buff) {
114   int s,usec;
115   
116   NtpTimeDisAssembToUs(s, usec, time);
117   sprintf(buff,"%d.%06d",s,usec);
118   return buff;
119 }