1 /************************************************************************/
2 /*									*/
3 /*  Simplification of stdio: Output.					*/
4 /*									*/
5 /************************************************************************/
6 
7 #   include	"appUtilConfig.h"
8 
9 #   include	<stdlib.h>
10 #   include	<string.h>
11 
12 #   include	"sioGeneral.h"
13 #   include	<appDebugon.h>
14 
sioOutWriteBuffer(SimpleOutputStream * sos,const unsigned char * b,int n)15 static int sioOutWriteBuffer(	SimpleOutputStream *	sos,
16 				const unsigned char *	b,
17 				int			n )
18     {
19     while( n > 0 )
20 	{
21 	int		done;
22 
23 	done= (*sos->sosWriteBytes)( sos->sosPrivate, b, n );
24 	if  ( done <= 0 )
25 	    { LLDEB(n,done); return -1;	}
26 
27 	b += done; n -= done;
28 	sos->sosBytesWritten += done;
29 	}
30 
31     return 0;
32     }
33 
sioOutFlushBuffer(SimpleOutputStream * sos)34 int sioOutFlushBuffer(	SimpleOutputStream *	sos )
35     {
36     if  ( sioOutWriteBuffer( sos, sos->sosBuffer, sos->sosN ) )
37 	{ LDEB(sos->sosN); return -1;	}
38 
39     sos->sosN= 0;
40     sos->sosP= sos->sosBuffer;
41 
42     return 0;
43     }
44 
sioOutOpen(void * specific,SIOoutWRITEBYTES writeBytes,SIOoutCLOSE closeIt)45 SimpleOutputStream * sioOutOpen(	void *			specific,
46 					SIOoutWRITEBYTES	writeBytes,
47 					SIOoutCLOSE		closeIt )
48     {
49     SimpleOutputStream *	sos;
50 
51     sos= (SimpleOutputStream *)malloc( sizeof(SimpleOutputStream) );
52     if  ( ! sos )
53 	{ XDEB(sos); return sos;	}
54 
55     sos->sosP= sos->sosBuffer;
56     sos->sosN= 0;
57     sos->sosPrivate= specific;
58 
59     sos->sosBytesWritten= 0;
60 
61     sos->sosWriteBytes= writeBytes;
62     sos->sosClose= closeIt;
63 
64     return sos;
65     }
66 
sioOutClose(SimpleOutputStream * sos)67 int sioOutClose(	SimpleOutputStream *		sos )
68     {
69     int		rval= 0;
70 
71     if  ( sioOutFlushBuffer( sos ) )
72 	{ LDEB(sos->sosN); rval= -1;	}
73 
74     if  ( (*sos->sosClose)( sos->sosPrivate ) )
75 	{ XDEB(sos->sosPrivate); rval= -1;	}
76 
77     free( sos );
78 
79     return rval;
80     }
81 
sioOutGetBytesWritten(SimpleOutputStream * sos)82 long sioOutGetBytesWritten(			SimpleOutputStream *	sos )
83     { return sos->sosBytesWritten+ sos->sosN;	}
84 
sioOutPutString(const char * s,SimpleOutputStream * sos)85 int sioOutPutString(	const char *		s,
86 			SimpleOutputStream *	sos )
87     {
88     while( *s )
89 	{
90 	if  ( sioOutPutByte( *(s++), sos ) )
91 	    { return -1;	}
92 	}
93 
94     return 0;
95     }
96 
sioOutWriteBytes(SimpleOutputStream * sos,const unsigned char * buf,int count)97 int sioOutWriteBytes(	SimpleOutputStream *	sos,
98 			const unsigned char *	buf,
99 			int			count )
100     {
101     int		done;
102 
103     done= count;
104     if  ( done > SIOsizBUF- sos->sosN )
105 	{ done= SIOsizBUF- sos->sosN;	}
106 
107     memcpy( sos->sosP, buf, done );
108 
109     count -= done; buf += done;  sos->sosN += done; sos->sosP += done;
110 
111     if  ( count > 0 )
112 	{
113 	if  ( sioOutFlushBuffer( sos ) )
114 	    { LDEB(sos->sosN); return -1;	}
115 
116 	while( count >= SIOsizBUF )
117 	    {
118 	    if  ( sioOutWriteBuffer( sos, buf, SIOsizBUF ) )
119 		{ LDEB(sos->sosN); return -1;	}
120 
121 	    count -= SIOsizBUF; buf += SIOsizBUF; done += SIOsizBUF;
122 	    }
123 
124 	if  ( count > 0 )
125 	    {
126 	    memcpy( sos->sosBuffer, buf, count );
127 	    done += count; sos->sosN= count; sos->sosP= sos->sosBuffer+ count;
128 	    }
129 	}
130 
131     return done;
132     }
133 
134