]> git.saurik.com Git - redis.git/blame - src/rio.c
crc64.c modified for incremental computation.
[redis.git] / src / rio.c
CommitLineData
9ba4d5a3 1/* rio.c is a simple stream-oriented I/O abstraction that provides an interface
2 * to write code that can consume/produce data using different concrete input
3 * and output devices. For instance the same rdb.c code using the rio abstraction
4 * can be used to read and write the RDB format using in-memory buffers or files.
5 *
6 * A rio object provides the following methods:
7 * read: read from stream.
8 * write: write to stream.
9 * tell: get the current offset.
10 *
11 * It is also possible to set a 'checksum' method that is used by rio.c in order
12 * to compute a checksum of the data written or read, or to query the rio object
13 * for the current checksum. */
14
f013f400 15#include "fmacros.h"
2e4b0e77 16#include <string.h>
f013f400 17#include <stdio.h>
2e4b0e77
PN
18#include "rio.h"
19#include "util.h"
20
21/* Returns 1 or 0 for success/failure. */
22static size_t rioBufferWrite(rio *r, const void *buf, size_t len) {
23 r->io.buffer.ptr = sdscatlen(r->io.buffer.ptr,(char*)buf,len);
24 r->io.buffer.pos += len;
69cecb51 25 return 1;
2e4b0e77
PN
26}
27
28/* Returns 1 or 0 for success/failure. */
29static size_t rioBufferRead(rio *r, void *buf, size_t len) {
30 if (sdslen(r->io.buffer.ptr)-r->io.buffer.pos < len)
69cecb51 31 return 0; /* not enough buffer to return len bytes. */
2e4b0e77
PN
32 memcpy(buf,r->io.buffer.ptr+r->io.buffer.pos,len);
33 r->io.buffer.pos += len;
34 return 1;
35}
36
37/* Returns read/write position in buffer. */
38static off_t rioBufferTell(rio *r) {
39 return r->io.buffer.pos;
40}
41
42/* Returns 1 or 0 for success/failure. */
43static size_t rioFileWrite(rio *r, const void *buf, size_t len) {
44 return fwrite(buf,len,1,r->io.file.fp);
45}
46
47/* Returns 1 or 0 for success/failure. */
48static size_t rioFileRead(rio *r, void *buf, size_t len) {
49 return fread(buf,len,1,r->io.file.fp);
50}
51
52/* Returns read/write position in file. */
53static off_t rioFileTell(rio *r) {
54 return ftello(r->io.file.fp);
55}
56
57static const rio rioBufferIO = {
58 rioBufferRead,
59 rioBufferWrite,
60 rioBufferTell,
61 { { NULL, 0 } } /* union for io-specific vars */
62};
63
64static const rio rioFileIO = {
65 rioFileRead,
66 rioFileWrite,
67 rioFileTell,
68 { { NULL, 0 } } /* union for io-specific vars */
69};
70
f96a8a80 71void rioInitWithFile(rio *r, FILE *fp) {
72 *r = rioFileIO;
73 r->io.file.fp = fp;
2e4b0e77 74}
f96a8a80 75
76void rioInitWithBuffer(rio *r, sds s) {
77 *r = rioBufferIO;
78 r->io.buffer.ptr = s;
79 r->io.buffer.pos = 0;
2e4b0e77
PN
80}
81
9ba4d5a3 82/* ------------------------------ Higher level interface ---------------------------
83 * The following higher level functions use lower level rio.c functions to help
84 * generating the Redis protocol for the Append Only File. */
85
2e4b0e77
PN
86/* Write multi bulk count in the format: "*<count>\r\n". */
87size_t rioWriteBulkCount(rio *r, char prefix, int count) {
88 char cbuf[128];
89 int clen;
90
91 cbuf[0] = prefix;
92 clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,count);
93 cbuf[clen++] = '\r';
94 cbuf[clen++] = '\n';
95 if (rioWrite(r,cbuf,clen) == 0) return 0;
96 return clen;
97}
98
99/* Write binary-safe string in the format: "$<count>\r\n<payload>\r\n". */
100size_t rioWriteBulkString(rio *r, const char *buf, size_t len) {
101 size_t nwritten;
102
103 if ((nwritten = rioWriteBulkCount(r,'$',len)) == 0) return 0;
104 if (len > 0 && rioWrite(r,buf,len) == 0) return 0;
105 if (rioWrite(r,"\r\n",2) == 0) return 0;
106 return nwritten+len+2;
107}
108
109/* Write a long long value in format: "$<count>\r\n<payload>\r\n". */
110size_t rioWriteBulkLongLong(rio *r, long long l) {
111 char lbuf[32];
112 unsigned int llen;
113
114 llen = ll2string(lbuf,sizeof(lbuf),l);
115 return rioWriteBulkString(r,lbuf,llen);
116}
117
118/* Write a double value in the format: "$<count>\r\n<payload>\r\n" */
119size_t rioWriteBulkDouble(rio *r, double d) {
120 char dbuf[128];
121 unsigned int dlen;
122
123 dlen = snprintf(dbuf,sizeof(dbuf),"%.17g",d);
124 return rioWriteBulkString(r,dbuf,dlen);
125}