]>
git.saurik.com Git - redis.git/blob - src/rio.c
7 /* Returns 1 or 0 for success/failure. */
8 static size_t rioBufferWrite(rio
*r
, const void *buf
, size_t len
) {
9 r
->io
.buffer
.ptr
= sdscatlen(r
->io
.buffer
.ptr
,(char*)buf
,len
);
10 r
->io
.buffer
.pos
+= len
;
14 /* Returns 1 or 0 for success/failure. */
15 static size_t rioBufferRead(rio
*r
, void *buf
, size_t len
) {
16 if (sdslen(r
->io
.buffer
.ptr
)-r
->io
.buffer
.pos
< len
)
17 return 0; /* not enough buffer to return len bytes. */
18 memcpy(buf
,r
->io
.buffer
.ptr
+r
->io
.buffer
.pos
,len
);
19 r
->io
.buffer
.pos
+= len
;
23 /* Returns read/write position in buffer. */
24 static off_t
rioBufferTell(rio
*r
) {
25 return r
->io
.buffer
.pos
;
28 /* Returns 1 or 0 for success/failure. */
29 static size_t rioFileWrite(rio
*r
, const void *buf
, size_t len
) {
30 return fwrite(buf
,len
,1,r
->io
.file
.fp
);
33 /* Returns 1 or 0 for success/failure. */
34 static size_t rioFileRead(rio
*r
, void *buf
, size_t len
) {
35 return fread(buf
,len
,1,r
->io
.file
.fp
);
38 /* Returns read/write position in file. */
39 static off_t
rioFileTell(rio
*r
) {
40 return ftello(r
->io
.file
.fp
);
43 static const rio rioBufferIO
= {
47 { { NULL
, 0 } } /* union for io-specific vars */
50 static const rio rioFileIO
= {
54 { { NULL
, 0 } } /* union for io-specific vars */
57 void rioInitWithFile(rio
*r
, FILE *fp
) {
62 void rioInitWithBuffer(rio
*r
, sds s
) {
68 /* Write multi bulk count in the format: "*<count>\r\n". */
69 size_t rioWriteBulkCount(rio
*r
, char prefix
, int count
) {
74 clen
= 1+ll2string(cbuf
+1,sizeof(cbuf
)-1,count
);
77 if (rioWrite(r
,cbuf
,clen
) == 0) return 0;
81 /* Write binary-safe string in the format: "$<count>\r\n<payload>\r\n". */
82 size_t rioWriteBulkString(rio
*r
, const char *buf
, size_t len
) {
85 if ((nwritten
= rioWriteBulkCount(r
,'$',len
)) == 0) return 0;
86 if (len
> 0 && rioWrite(r
,buf
,len
) == 0) return 0;
87 if (rioWrite(r
,"\r\n",2) == 0) return 0;
88 return nwritten
+len
+2;
91 /* Write a long long value in format: "$<count>\r\n<payload>\r\n". */
92 size_t rioWriteBulkLongLong(rio
*r
, long long l
) {
96 llen
= ll2string(lbuf
,sizeof(lbuf
),l
);
97 return rioWriteBulkString(r
,lbuf
,llen
);
100 /* Write a double value in the format: "$<count>\r\n<payload>\r\n" */
101 size_t rioWriteBulkDouble(rio
*r
, double d
) {
105 dlen
= snprintf(dbuf
,sizeof(dbuf
),"%.17g",d
);
106 return rioWriteBulkString(r
,dbuf
,dlen
);