]>
git.saurik.com Git - redis.git/blob - src/rio.c
5 /* Returns 1 or 0 for success/failure. */
6 static size_t rioBufferWrite(rio
*r
, const void *buf
, size_t len
) {
7 r
->io
.buffer
.ptr
= sdscatlen(r
->io
.buffer
.ptr
,(char*)buf
,len
);
8 r
->io
.buffer
.pos
+= len
;
12 /* Returns 1 or 0 for success/failure. */
13 static size_t rioBufferRead(rio
*r
, void *buf
, size_t len
) {
14 if (sdslen(r
->io
.buffer
.ptr
)-r
->io
.buffer
.pos
< len
)
15 return 0; /* not enough buffer to return len bytes. */
16 memcpy(buf
,r
->io
.buffer
.ptr
+r
->io
.buffer
.pos
,len
);
17 r
->io
.buffer
.pos
+= len
;
21 /* Returns read/write position in buffer. */
22 static off_t
rioBufferTell(rio
*r
) {
23 return r
->io
.buffer
.pos
;
26 /* Returns 1 or 0 for success/failure. */
27 static size_t rioFileWrite(rio
*r
, const void *buf
, size_t len
) {
28 return fwrite(buf
,len
,1,r
->io
.file
.fp
);
31 /* Returns 1 or 0 for success/failure. */
32 static size_t rioFileRead(rio
*r
, void *buf
, size_t len
) {
33 return fread(buf
,len
,1,r
->io
.file
.fp
);
36 /* Returns read/write position in file. */
37 static off_t
rioFileTell(rio
*r
) {
38 return ftello(r
->io
.file
.fp
);
41 static const rio rioBufferIO
= {
45 { { NULL
, 0 } } /* union for io-specific vars */
48 static const rio rioFileIO
= {
52 { { NULL
, 0 } } /* union for io-specific vars */
55 void rioInitWithFile(rio
*r
, FILE *fp
) {
60 void rioInitWithBuffer(rio
*r
, sds s
) {
66 /* Write multi bulk count in the format: "*<count>\r\n". */
67 size_t rioWriteBulkCount(rio
*r
, char prefix
, int count
) {
72 clen
= 1+ll2string(cbuf
+1,sizeof(cbuf
)-1,count
);
75 if (rioWrite(r
,cbuf
,clen
) == 0) return 0;
79 /* Write binary-safe string in the format: "$<count>\r\n<payload>\r\n". */
80 size_t rioWriteBulkString(rio
*r
, const char *buf
, size_t len
) {
83 if ((nwritten
= rioWriteBulkCount(r
,'$',len
)) == 0) return 0;
84 if (len
> 0 && rioWrite(r
,buf
,len
) == 0) return 0;
85 if (rioWrite(r
,"\r\n",2) == 0) return 0;
86 return nwritten
+len
+2;
89 /* Write a long long value in format: "$<count>\r\n<payload>\r\n". */
90 size_t rioWriteBulkLongLong(rio
*r
, long long l
) {
94 llen
= ll2string(lbuf
,sizeof(lbuf
),l
);
95 return rioWriteBulkString(r
,lbuf
,llen
);
98 /* Write a double value in the format: "$<count>\r\n<payload>\r\n" */
99 size_t rioWriteBulkDouble(rio
*r
, double d
) {
103 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
104 return rioWriteBulkString(r
,dbuf
,dlen
);