source: trunk/athena/bin/discuss/usp/put.c @ 1007

Revision 1007, 3.7 KB checked in by wesommer, 37 years ago (diff)
Performance improvement for strings: don't massage every character..
Line 
1/* UNIX Unified Stream Protocol
2
3   Copyright 1986 by the Massachusetts Institute of Technology
4   See permission and disclaimer notice in file "notice.h"
5*/
6
7#include <sys/types.h>
8#include <stdio.h>
9#include "gen.h"
10#include "usp.h"
11#include <netinet/in.h>
12
13/* output operations */
14
15USP_put_boolean(us, bo)
16
17USPStream    *us;
18USPBoolean   bo;
19{
20    errno = 0;
21    if(! us->us_out_sending_p) {
22        errno = UENOTSENDING;
23        return(ERROR);
24    }
25    switch (bo) {
26      case TRUE:
27      case FALSE:
28        break;
29      default:
30        errno = UEBADATA;
31        return(ERROR);
32    }
33    bo = (USPBoolean) htons(bo);
34    if(put_into_sub_block(us, (char *) &bo, sizeof(USPBoolean)) == ERROR) {
35        return(ERROR);
36    }
37    return(SUCCESS);
38}
39
40USP_put_integer(us, ui)
41
42USPStream    *us;
43USPInteger   ui;
44{
45    errno = 0;
46    if(! us->us_out_sending_p) {
47        errno = UENOTSENDING;
48        return(ERROR);
49    }
50    ui = (USPInteger) htons((u_short) ui);     
51    if(put_into_sub_block(us, (char *) &ui, sizeof(USPInteger)) == ERROR) {
52        return(ERROR);
53    }
54    return(SUCCESS);
55}
56
57USP_put_cardinal(us, ca)
58
59USPStream    *us;
60USPCardinal  ca;
61{
62    errno = 0;
63    if(! us->us_out_sending_p) {
64        errno = UENOTSENDING;
65        return(ERROR);
66    }
67    ca = (USPCardinal) htons((u_short) ca);
68    if(put_into_sub_block(us, (char *) &ca, sizeof(USPCardinal)) == ERROR) {
69        return(ERROR);
70    }
71    return(SUCCESS);
72}
73
74USP_put_long_integer(us, li)
75
76USPStream       *us;
77USPLong_integer li;
78{
79    errno = 0;
80    if(! us->us_out_sending_p) {
81        errno = UENOTSENDING;
82        return(ERROR);
83    }
84    li = (USPLong_integer) htonl((u_long) li);
85    if(put_into_sub_block(us, (char *) &li, sizeof(USPLong_integer)) == ERROR){
86        return(ERROR);
87    }
88    return(SUCCESS);
89}
90
91USP_put_long_cardinal(us, lc)
92
93USPStream        *us;
94USPLong_cardinal lc;
95{
96    errno = 0;
97    if(! us->us_out_sending_p) {
98        errno = UENOTSENDING;
99        return(ERROR);
100    }
101    lc = (USPLong_cardinal) htonl((u_long) lc);
102    if(put_into_sub_block(us, (char *)&lc, sizeof(USPLong_cardinal)) == ERROR){
103        return(ERROR);
104    }
105    return(SUCCESS);
106}
107
108USP_put_string(us, str)
109
110USPStream    *us;
111USPString    str;
112{
113    USPCardinal sl = 0, ssl;
114    register char *sptr = str, *stptr;
115    register int c;
116    char zero = '\0';
117    static char crlf[] = "\r\n";
118    static char crnul[] = "\r\0";
119
120    errno = 0;
121    if(! us->us_out_sending_p) {
122        errno = UENOTSENDING;
123        return(ERROR);
124    }
125
126    /* Process string one byte at a time in order to netasciify */
127
128    /* first find netasciified length by adding 1 extra for every CR or NL */
129
130    while(*sptr) {
131        ++sl;
132        if(*sptr == '\r' || *sptr == '\n') {
133            ++sl;
134        }
135        ++sptr;
136    }
137    ssl = (USPCardinal) htons((u_short) sl);
138    if(put_into_sub_block(us, (char *) &ssl, sizeof(USPCardinal)) == ERROR) {
139        return(ERROR);
140    }
141    sptr = str;
142    stptr = sptr;
143    while((c = *sptr) != '\0') {
144        if (c == '\n') {
145            if (put_into_sub_block (us, stptr, sptr-stptr-1) == ERROR) {
146                return (ERROR);
147            }
148            if(put_into_sub_block(us, crlf, sizeof (crnul)) == ERROR) {
149                return(ERROR);
150            }
151            stptr = sptr+1;
152        } else if (c == '\r') {
153            if (put_into_sub_block (us, stptr, sptr-stptr-1) == ERROR) {
154                return (ERROR);
155            }
156            if(put_into_sub_block(us, crnul, sizeof (crnul)) == ERROR) {
157                return(ERROR);
158            }
159            stptr = sptr+1;
160        }
161        ++sptr;
162    }
163    if (stptr - sptr) {
164        if (put_into_sub_block (us, stptr, sptr-stptr) == ERROR) {
165            return (ERROR);
166        }       
167    }
168    /* pad odd length strings */
169   
170    if(sl & 1) {
171        if(put_into_sub_block(us, &zero, sizeof(char)) == ERROR) {
172            return(ERROR);
173        }
174    }
175    return(SUCCESS);
176}
177
178/* this puts a raw block of bytes into a USP block.  For those of us who
179   have no need of USP's data types... */
180
181USP_put_byte_block(us, buf, len)
182
183USPStream       *us;
184Byte            *buf;
185unsigned        len;
186{
187    return(put_into_sub_block(us, buf, len));
188}
Note: See TracBrowser for help on using the repository browser.