summaryrefslogtreecommitdiffstats
path: root/mpeglib/example/yaf/yafcore/buffer.cpp
blob: 3c58f0ee5fe63c975c278a1594195310f5ef7d86 (plain)
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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
  This class implements a dynamic string buffer
  Copyright (C) 1998  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "buffer.h"



Buffer::Buffer(int size) {
  nSize=size;
  msg=(char*) malloc(sizeof(char)*(nSize+1));
  msg[nSize]='\0';
  clear();

}


Buffer::~Buffer() {
  free (msg);
}


void Buffer::clear() {
  msg[0]='\0';
}

void Buffer::append(int value) {
  Buffer buf(30);
  sprintf(buf.getData(),"%d",value);
  append(buf.getData());
}




void Buffer::append(char* appendMsg) {
  char* appendPos=getAppendPos();
  int nlen=strlen(appendMsg);
  if (appendPos == NULL) return;
  
  append(appendMsg,nlen);
}
 

void Buffer::append(const char* appendMsg) {
  append((char*)appendMsg);
}

 
void Buffer::append(char* buffer,int buflen) {
  int nlen=len();
  int nBedarf;

  if (buflen+nlen <= nSize) {
    char* appendPos=getAppendPos();
    strncpy(appendPos,buffer,buflen);
    appendPos[buflen]='\0';
    return;
  }
  nBedarf=(nlen+buflen)-nSize;
  grow(nBedarf);
  append(buffer,buflen);
}
 
char* Buffer::getAppendPos() {
  int i;
  // this Array has nSize+1 entries!
  // and it *is* granted that msg[nSize]=0;  (think so)
  for (i=0;i<=nSize;i++) {
    if (msg[i] == '\0') return &(msg[i]);
  }
  // should never reach this point
  return NULL;
}


void Buffer::setData(char* msg) {
  if (strlen(msg) == 0) {
    clear();
    return;
  }
  clear();
  append(msg);
}

char* Buffer::getData() {
  return msg;
}


int  Buffer::len() {
  return strlen(msg);
}  

int Buffer::getSize() {
  return nSize;
}

void Buffer::grow(int size) {
  int i;
  int newSize=nSize+size;
  char* tmp=(char*) malloc(sizeof(char)*(newSize+1));
  tmp[newSize]='\0';
  for(i=0;i<=nSize;i++) {
    tmp[i]=msg[i];
  }
 
  nSize=newSize;
  free(msg);
  msg=tmp;

}


int Buffer::find(char zeichen) {
  int i;
  int nlen=len();
  for(i=0;i<nlen;i++) {
    if (msg[i] == zeichen) return i;
  }
  return -1;
}



void Buffer::forward(int bytes) {
  int i;
  int aktPos;
  int nlen=len();
  if (bytes > nlen) {
    bytes=nlen;
  }
  i=0;
  aktPos=bytes;
  while(aktPos <= nlen) {
    msg[i]=msg[aktPos];
    i++;
    aktPos++;
  }
}



void Buffer::print() {
  printf("Buffer:%s\n",msg);
}