tbuffer.h 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * 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/>.
 */

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <setjmp.h>

#ifndef TDENGINE_TBUFFER_H
#define TDENGINE_TBUFFER_H


/*
SBuffer can be used to read or write a buffer, but cannot be used for both
weixin_48148422's avatar
weixin_48148422 已提交
27 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
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;
}
80 81 82
*/
typedef struct {
  jmp_buf jb;
weixin_48148422's avatar
weixin_48148422 已提交
83
  char* data;
84 85 86 87 88 89
  size_t pos;
  size_t size;
} 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);
weixin_48148422's avatar
weixin_48148422 已提交
94
void tbufClose(SBuffer* buf, bool keepData);
95 96 97


// basic read functions
weixin_48148422's avatar
weixin_48148422 已提交
98
#define tbufBeginRead(buf, data, len) (((buf)->data = (char*)data), ((buf)->pos = 0), ((buf)->size = ((data) == NULL) ? 0 : (len)), setjmp((buf)->jb))
99 100 101 102 103 104 105
char* tbufRead(SBuffer* buf, size_t size);
void tbufReadToBuffer(SBuffer* buf, void* dst, size_t size);
const char* tbufReadString(SBuffer* buf, size_t* len);
size_t tbufReadToString(SBuffer* buf, char* dst, size_t size);


// basic write functions
weixin_48148422's avatar
weixin_48148422 已提交
106
#define tbufBeginWrite(buf) ((buf)->data = NULL, ((buf)->pos = 0), ((buf)->size = 0), setjmp((buf)->jb))
107
void tbufEnsureCapacity(SBuffer* buf, size_t size);
weixin_48148422's avatar
weixin_48148422 已提交
108
char* tbufGetData(SBuffer* buf, bool takeOver);
109 110 111 112 113 114 115
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);


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

weixin_48148422's avatar
weixin_48148422 已提交
123 124 125 126 127 128 129 130 131 132 133 134
TBUFFER_DEFINE_FUNCTION( bool, Bool )
TBUFFER_DEFINE_FUNCTION( char, Char )
TBUFFER_DEFINE_FUNCTION( int8_t, Int8 )
TBUFFER_DEFINE_FUNCTION( uint8_t, Unt8 )
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 )
135 136

#endif