]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/conv.c
e79aa70efca7b68f5842e86faa37c2216c03d1d1
[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  *  AUTHOR: Petr Smolik                 petr.smolik@wo.cz
6  *
7  *  ORTE - OCERA Real-Time Ethernet     http://www.ocera.org/
8  *  --------------------------------------------------------------------
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  This module maintains a hash table of key/value pairs.
21  *  Keys can be strings of any size, or numbers up to size
22  *  unsigned long (HASHKEYTYPE).
23  *  Values should be a pointer to some data you wish to store.
24  *
25  */
26
27 #include "orte_all.h"
28
29 /**********************************************************************************/
30 //get part of string, div. by semi.
31 int
32 getStringPart(char *string,char divChar,int *iterator,char *buff) {
33   char *cp;
34   int len;
35   
36   if (!string || !buff) return -1;
37   len=strlen(string);
38   if (len<(*iterator)) return -2;
39   cp=string+(*iterator);
40   if (cp[0]!=0) {  //till is length>0
41     char *dcp,tcp; 
42     dcp=strchr(cp,divChar);
43     if (!dcp) dcp=cp+strlen(cp);
44     tcp=*dcp;         //save ending value
45     *dcp=0;           //temporary end of string
46     strcpy(buff,cp);  
47     *dcp=tcp;         //restore value
48     if (dcp[0]!=0) (*iterator)+=dcp-cp+1;//next value
49     else (*iterator)=len;
50     return 1;
51   }
52   return 0;
53 }
54
55 /**********************************************************************************/
56 char* 
57 IPAddressToString(IPAddress ipAddress,char *buff) {
58   struct in_addr addr;
59   
60   addr.s_addr=htonl(ipAddress);
61   sprintf(buff,"%s",inet_ntoa(addr));
62   return buff;
63 }
64
65 /**********************************************************************************/
66 IPAddress 
67 StringToIPAddress(const char *string) {
68   IPAddress ipAddress=IPADDRESS_INVALID;
69   
70   ipAddress=ntohl(inet_addr(string));
71 #if defined HAVE_GETHOSTBYNAME
72   {
73     struct hostent *hostname; 
74     if (ipAddress==0) {
75       if ((hostname=gethostbyname(string))) {
76         ipAddress=ntohl(*((long*)(hostname->h_addr_list[0])));
77       }
78     }
79   }
80 #endif
81   return ipAddress;
82 }
83
84 /**********************************************************************************/
85 char *
86 NtpTimeToStringMs(NtpTime time,char *buff) {
87   int s,msec;
88   
89   NtpTimeDisAssembToMs(s, msec, time);
90   sprintf(buff,"%d.%03d",s,msec);
91   return buff;
92 }
93
94 /**********************************************************************************/
95 char *
96 NtpTimeToStringUs(NtpTime time,char *buff) {
97   int s,usec;
98   
99   NtpTimeDisAssembToUs(s, usec, time);
100   sprintf(buff,"%d.%06d",s,usec);
101   return buff;
102 }