VCFBuffer.h 2.02 KB
Newer Older
zhanxw's avatar
zhanxw committed
1
2
3
#ifndef _VCFBUFFER_H_
#define _VCFBUFFER_H_

zhanxw's avatar
zhanxw committed
4
#include <stdio.h>
zhanxw's avatar
zhanxw committed
5
#include <stdlib.h>  // for size_t, fprintf
zhanxw's avatar
zhanxw committed
6
// #include <string.h>  // for strlen, memcpy
zhanxw's avatar
zhanxw committed
7
#include <cassert>
zhanxw's avatar
zhanxw committed
8
#include <string>
zhanxw's avatar
zhanxw committed
9

zhanxw's avatar
zhanxw committed
10
11
12
13
class FileWriter;

// holding a memory area for a string
// the content may be changed
zhanxw's avatar
zhanxw committed
14
class VCFBuffer {
zhanxw's avatar
zhanxw committed
15
16
 public:
  VCFBuffer() : buf(NULL), len(0), bufLen(0){};
zhanxw's avatar
zhanxw committed
17
18
19
20
21
  // VCFBuffer(const char* s) { this->copy(s); };
  void attach(char* s, int l) {
    this->buf = s;
    this->len = this->bufLen = l;
  }
zhanxw's avatar
zhanxw committed
22
  void attach(std::string& s) { attach(&s[0], (int)s.size()); }
zhanxw's avatar
zhanxw committed
23
#if 0
zhanxw's avatar
zhanxw committed
24
  void copy(const char* s) {
zhanxw's avatar
zhanxw committed
25
26
27
28
29
    this->len = strlen(s);
    if (this->bufLen < this->len + 1) {
      delete[] this->buf;
      this->buf = NULL;
    }
zhanxw's avatar
zhanxw committed
30
    if (NULL == this->buf) {
zhanxw's avatar
zhanxw committed
31
32
33
34
35
36
37
38
39
40
41
42
43
      this->buf = new char[this->len + 1];
      assert(this->buf);
      this->bufLen = this->len + 1;
    }
    memcpy(this->buf, s, this->len);
    this->buf[this->len] = '\0';
  };
  ~VCFBuffer() {
    if (this->buf) {
      delete[] buf;
      this->buf = NULL;
    }
  }
zhanxw's avatar
zhanxw committed
44
#endif
zhanxw's avatar
zhanxw committed
45
46
  char& operator[](const int idx) { return buf[idx]; }
  const char& operator[](const int idx) const { return buf[idx]; }
zhanxw's avatar
zhanxw committed
47
#if 0
zhanxw's avatar
zhanxw committed
48
49
  VCFBuffer& operator=(const char* s) {
    this->copy(s);
zhanxw's avatar
zhanxw committed
50
    return (*this);
zhanxw's avatar
zhanxw committed
51
  };
zhanxw's avatar
zhanxw committed
52
#endif
zhanxw's avatar
zhanxw committed
53
  void clear() { this->len = 0; };
zhanxw's avatar
zhanxw committed
54

zhanxw's avatar
zhanxw committed
55
  const char* c_str() const { return this->buf; };
zhanxw's avatar
zhanxw committed
56
  char* getBuffer() const { return this->buf; };
zhanxw's avatar
zhanxw committed
57
  size_t size() const { return this->len; };
58
59
60
61
62
  void dump(int firstNumChar) const {
    for (int i = 0; i < firstNumChar; i++) {
      fprintf(stderr, "%d: %c (%d)\n", i, buf[i], buf[i]);
    }
  }
zhanxw's avatar
zhanxw committed
63
  void output(FILE* fp) const {
zhanxw's avatar
zhanxw committed
64
65
66
67
    for (size_t i = 0; i != len; ++i) {
      fputc(buf[i], fp);
    }
  }
zhanxw's avatar
zhanxw committed
68
  void output(FILE* fp, char c) const {
zhanxw's avatar
zhanxw committed
69
70
71
72
    output(fp);
    fputc(c, fp);
  }
  void output(FileWriter* fp, char c) const;
zhanxw's avatar
zhanxw committed
73

zhanxw's avatar
zhanxw committed
74
 private:
zhanxw's avatar
zhanxw committed
75
76
  VCFBuffer(VCFBuffer& b);
  VCFBuffer& operator=(const VCFBuffer& b);
zhanxw's avatar
zhanxw committed
77
78
79

 private:
  char* buf;      // pointer to string
zhanxw's avatar
zhanxw committed
80
  size_t len;     // len (not including the trailing '\0')
zhanxw's avatar
zhanxw committed
81
  size_t bufLen;  // memory capacity
zhanxw's avatar
zhanxw committed
82
83
84
};

#endif /* _VCFBUFFER_H_ */