]> git.saurik.com Git - redis.git/blame - src/rio.h
BSD license added to every C source and header file.
[redis.git] / src / rio.h
CommitLineData
4365e5b2 1/*
2 * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
2e4b0e77
PN
32#ifndef __REDIS_RIO_H
33#define __REDIS_RIO_H
34
35#include <stdio.h>
736b7c3f 36#include <stdint.h>
2e4b0e77
PN
37#include "sds.h"
38
39struct _rio {
4c046297 40 /* Backend functions.
41 * Since this functions do not tolerate short writes or reads the return
42 * value is simplified to: zero on error, non zero on complete success. */
2e4b0e77
PN
43 size_t (*read)(struct _rio *, void *buf, size_t len);
44 size_t (*write)(struct _rio *, const void *buf, size_t len);
45 off_t (*tell)(struct _rio *);
736b7c3f 46 /* The update_cksum method if not NULL is used to compute the checksum of all the
47 * data that was read or written so far. The method should be designed so that
48 * can be called with the current checksum, and the buf and len fields pointing
49 * to the new block of data to add to the checksum computation. */
8491f1d9 50 void (*update_cksum)(struct _rio *, const void *buf, size_t len);
736b7c3f 51
52 /* The current checksum */
53 uint64_t cksum;
2e4b0e77
PN
54
55 /* Backend-specific vars. */
56 union {
57 struct {
58 sds ptr;
59 off_t pos;
60 } buffer;
61 struct {
62 FILE *fp;
63 } file;
64 } io;
65};
66
67typedef struct _rio rio;
68
736b7c3f 69/* The following functions are our interface with the stream. They'll call the
70 * actual implementation of read / write / tell, and will update the checksum
71 * if needed. */
72
c44ab51d 73static inline size_t rioWrite(rio *r, const void *buf, size_t len) {
8491f1d9 74 if (r->update_cksum) r->update_cksum(r,buf,len);
736b7c3f 75 return r->write(r,buf,len);
76}
77
c44ab51d 78static inline size_t rioRead(rio *r, void *buf, size_t len) {
736b7c3f 79 if (r->read(r,buf,len) == 1) {
8491f1d9 80 if (r->update_cksum) r->update_cksum(r,buf,len);
736b7c3f 81 return 1;
82 }
83 return 0;
84}
85
c44ab51d 86static inline off_t rioTell(rio *r) {
736b7c3f 87 return r->tell(r);
88}
2e4b0e77 89
f96a8a80 90void rioInitWithFile(rio *r, FILE *fp);
91void rioInitWithBuffer(rio *r, sds s);
2e4b0e77
PN
92
93size_t rioWriteBulkCount(rio *r, char prefix, int count);
94size_t rioWriteBulkString(rio *r, const char *buf, size_t len);
95size_t rioWriteBulkLongLong(rio *r, long long l);
96size_t rioWriteBulkDouble(rio *r, double d);
97
736b7c3f 98void rioGenericUpdateChecksum(rio *r, const void *buf, size_t len);
99
2e4b0e77 100#endif