~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to src/relooper/ministring.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// Tiny implementation of strings. Avoids linking in all of std::string
 
3
 
 
4
#include <stdlib.h>
 
5
#include <string.h>
 
6
 
 
7
class ministring {
 
8
  int used;
 
9
  char *buffer;
 
10
  int bufferSize;
 
11
public:
 
12
  ministring() : used(0), buffer(NULL), bufferSize(0) {}
 
13
  ~ministring() { if (buffer) free(buffer); }
 
14
 
 
15
  char *c_str() { return buffer; }
 
16
  int size() { return used; }
 
17
 
 
18
  void clear() {
 
19
    used = 0; // keep the buffer alive as an optimization, just resize
 
20
  }
 
21
 
 
22
  ministring& operator+=(const char *s) {
 
23
    int len = strlen(s);
 
24
    if (used + len + 2 > bufferSize) {
 
25
      // try to avoid frequent reallocations
 
26
      bufferSize = 2*(bufferSize + len);
 
27
      bufferSize += 1024 - bufferSize % 1024;
 
28
      buffer = (char*)(buffer ? realloc(buffer, bufferSize) : malloc(bufferSize));
 
29
    }
 
30
    strcpy(buffer + used, s);
 
31
    used += len;
 
32
    return *this;
 
33
  }
 
34
};
 
35