]> rtime.felk.cvut.cz Git - orte.git/blob - orte/liborte/conv.c
5f53fce3bcbbd30a6c35e464a8f45d632e0cbdc6
[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.h"
28
29 /**********************************************************************************/
30 void conv_u16(u_int16_t *x,char ef) {
31   #if WORDS_BIGENDIAN
32      if(ef) *x=bswap_16(*x);
33   #else
34      if(!ef) *x=bswap_16(*x);
35   #endif
36 }
37
38 /**********************************************************************************/
39 void conv_u32(u_int32_t *x,char ef) {
40   #if WORDS_BIGENDIAN
41      if(ef) *x=bswap_32(*x);
42   #else
43      if(!ef) *x=bswap_32(*x);
44   #endif
45 }
46
47 /**********************************************************************************/
48 void conv_sn(SequenceNumber *sn,char ef) {
49   #if WORDS_BIGENDIAN
50      if(ef) {
51        sn->high=bswap_32(sn->high);
52        sn->low=bswap_32(sn->low);
53      }  
54   #else
55      if(!ef) {
56        sn->high=bswap_32(sn->high);
57        sn->low=bswap_32(sn->low);
58      }  
59   #endif
60 }
61
62 /**********************************************************************************/
63 void conv_ntp(NtpTime *ntp,char ef) {
64   #if WORDS_BIGENDIAN
65      if(ef) {
66        ntp->seconds=bswap_32(ntp->seconds);
67        ntp->fraction=bswap_32(ntp->fraction);
68      }  
69   #else
70      if(!ef) {
71        ntp->seconds=bswap_32(ntp->seconds);
72        ntp->fraction=bswap_32(ntp->fraction);
73      }  
74   #endif
75 }
76
77 /**********************************************************************************/
78 //get part of string, div. by semi.
79 int
80 getStringPart(char *string,char divChar,int *iterator,char *buff) {
81   char *cp;
82   int len;
83   
84   if (!string || !buff) return -1;
85   len=strlen(string);
86   if (len<(*iterator)) return -2;
87   cp=string+(*iterator);
88   if (cp[0]!=0) {  //till is length>0
89     char *dcp,tcp; 
90     dcp=strchr(cp,divChar);
91     if (!dcp) dcp=cp+strlen(cp);
92     tcp=*dcp;         //save ending value
93     *dcp=0;           //temporary end of string
94     strcpy(buff,cp);  
95     *dcp=tcp;         //restore value
96     if (dcp[0]!=0) (*iterator)=dcp-cp+1;//next value
97     else (*iterator)=dcp-cp;
98     return 1;
99   }
100   return 0;
101 }
102
103 /**********************************************************************************/
104 char* 
105 IPAddressToString(IPAddress ipAddress,char *buff) {
106   struct in_addr addr;
107   
108   addr.s_addr=htonl(ipAddress);
109   sprintf(buff,"%s",inet_ntoa(addr));
110   return buff;
111 }
112
113 /**********************************************************************************/
114 IPAddress 
115 StringToIPAddress(const char *string) {
116   IPAddress ipAddress=IPADDRESS_INVALID;
117   
118   ipAddress=ntohl(inet_addr(string));
119 #if defined HAVE_GETHOSTBYNAME
120   {
121     struct hostent *hostname; 
122     if (ipAddress==0) {
123       if ((hostname=gethostbyname(string))) {
124         ipAddress=ntohl(*((long*)(hostname->h_addr_list[0])));
125       }
126     }
127   }
128 #endif
129   return ipAddress;
130 }
131
132 /**********************************************************************************/
133 char *
134 NtpTimeToStringMs(NtpTime time,char *buff) {
135   int s,msec;
136   
137   NtpTimeDisAssembToMs(s, msec, time);
138   sprintf(buff,"%d.%03d",s,msec);
139   return buff;
140 }
141
142 /**********************************************************************************/
143 char *
144 NtpTimeToStringUs(NtpTime time,char *buff) {
145   int s,usec;
146   
147   NtpTimeDisAssembToUs(s, usec, time);
148   sprintf(buff,"%d.%06d",s,usec);
149   return buff;
150 }