tbuffer.h 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2020 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TDENGINE_TBUFFER_H
#define TDENGINE_TBUFFER_H

19 20 21 22 23 24
#include "setjmp.h"
#include "os.h"

#ifdef __cplusplus
extern "C" {
#endif
25 26 27

/*
SBuffer can be used to read or write a buffer, but cannot be used for both
weixin_48148422's avatar
weixin_48148422 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
read & write at a same time. Below is an example:

int main(int argc, char** argv) {
  //--------------------- write ------------------------
  SBuffer wbuf;
  int32_t code = tbufBeginWrite(&wbuf);
  if (code != 0) {
    // handle errors
    return 0;
  }

  // reserve 1024 bytes for the buffer to improve performance
  tbufEnsureCapacity(&wbuf, 1024);

  // write 5 integers to the buffer
  for (int i = 0; i < 5; i++) {
    tbufWriteInt32(&wbuf, i);
  }

  // write a string to the buffer
  tbufWriteString(&wbuf, "this is a string.\n");

  // acquire the result and close the write buffer
  size_t size = tbufTell(&wbuf);
  char*  data = tbufGetData(&wbuf, true);
  tbufClose(&wbuf, true);


  //------------------------ read -----------------------
  SBuffer rbuf;
  code = tbufBeginRead(&rbuf, data, size);
  if (code != 0) {
    printf("you will see this message after print out 5 integers and a string.\n");
    tbufClose(&rbuf, false);
    return 0;
  }

  // read & print out 5 integers
  for (int i = 0; i < 5; i++) {
    printf("%d\n", tbufReadInt32(&rbuf));
  }

  // read & print out a string
  printf(tbufReadString(&rbuf, NULL));

  // try read another integer, this result in an error as there no this integer
  tbufReadInt32(&rbuf);

  printf("you should not see this message.\n");
  tbufClose(&rbuf, false);

  return 0;
}
81 82 83
*/
typedef struct {
  jmp_buf jb;
84 85 86
  char*   data;
  size_t  pos;
  size_t  size;
87 88 89
} SBuffer;

// common functions can be used in both read & write
weixin_48148422's avatar
weixin_48148422 已提交
90
#define tbufThrowError(buf, code) longjmp((buf)->jb, (code))
91 92 93
size_t tbufTell(SBuffer* buf);
size_t tbufSeekTo(SBuffer* buf, size_t pos);
size_t tbufSkip(SBuffer* buf, size_t size);
94
void   tbufClose(SBuffer* buf, bool keepData);
95 96

// basic read functions
97 98 99
#define tbufBeginRead(buf, _data, len) ((buf)->data = (char*)(_data), ((buf)->pos = 0), ((buf)->size = ((_data) == NULL) ? 0 : (len)), setjmp((buf)->jb))
char*       tbufRead(SBuffer* buf, size_t size);
void        tbufReadToBuffer(SBuffer* buf, void* dst, size_t size);
100
const char* tbufReadString(SBuffer* buf, size_t* len);
101
size_t      tbufReadToString(SBuffer* buf, char* dst, size_t size);
102 103

// basic write functions
weixin_48148422's avatar
weixin_48148422 已提交
104
#define tbufBeginWrite(buf) ((buf)->data = NULL, ((buf)->pos = 0), ((buf)->size = 0), setjmp((buf)->jb))
105
void  tbufEnsureCapacity(SBuffer* buf, size_t size);
weixin_48148422's avatar
weixin_48148422 已提交
106
char* tbufGetData(SBuffer* buf, bool takeOver);
107 108 109 110
void  tbufWrite(SBuffer* buf, const void* data, size_t size);
void  tbufWriteAt(SBuffer* buf, size_t pos, const void* data, size_t size);
void  tbufWriteStringLen(SBuffer* buf, const char* str, size_t len);
void  tbufWriteString(SBuffer* buf, const char* str);
111 112

// read & write function for primitive types
weixin_48148422's avatar
weixin_48148422 已提交
113 114
#ifndef TBUFFER_DEFINE_FUNCTION
#define TBUFFER_DEFINE_FUNCTION(type, name) \
115 116 117 118 119
  type tbufRead##name(SBuffer* buf); \
  void tbufWrite##name(SBuffer* buf, type data); \
  void tbufWrite##name##At(SBuffer* buf, size_t pos, type data);
#endif

120 121 122
TBUFFER_DEFINE_FUNCTION(bool, Bool)
TBUFFER_DEFINE_FUNCTION(char, Char)
TBUFFER_DEFINE_FUNCTION(int8_t, Int8)
weixin_48148422's avatar
weixin_48148422 已提交
123
TBUFFER_DEFINE_FUNCTION(uint8_t, Uint8)
124 125 126 127 128 129 130 131 132 133 134 135
TBUFFER_DEFINE_FUNCTION(int16_t, Int16)
TBUFFER_DEFINE_FUNCTION(uint16_t, Uint16)
TBUFFER_DEFINE_FUNCTION(int32_t, Int32)
TBUFFER_DEFINE_FUNCTION(uint32_t, Uint32)
TBUFFER_DEFINE_FUNCTION(int64_t, Int64)
TBUFFER_DEFINE_FUNCTION(uint64_t, Uint64)
TBUFFER_DEFINE_FUNCTION(float, Float)
TBUFFER_DEFINE_FUNCTION(double, Double)

#ifdef __cplusplus
}
#endif
136 137

#endif