CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Buffer.h
Views: 1401
1
#pragma once
2
3
#include <string>
4
#include <vector>
5
6
#include "Common/Common.h"
7
8
class Path;
9
10
// Acts as a queue. Intended to be as fast as possible for most uses.
11
// Does not do synchronization, must use external mutexes.
12
class Buffer {
13
public:
14
Buffer();
15
Buffer(Buffer &&) = default;
16
~Buffer();
17
18
static Buffer Void() {
19
Buffer buf;
20
buf.void_ = true;
21
return buf;
22
}
23
24
// Write max [length] bytes to the returned pointer.
25
// Any other operation on this Buffer invalidates the pointer.
26
char *Append(size_t length);
27
28
// These work pretty much like you'd expect.
29
void Append(const char *str); // str null-terminated. The null is not copied.
30
void Append(const std::string &str);
31
void Append(const Buffer &other);
32
33
// Various types. Useful for varz etc. Appends a string representation of the
34
// value, rather than a binary representation.
35
void AppendValue(int value);
36
37
// Parsing Helpers
38
39
// Use for easy line skipping. If no CRLF within the buffer, returns -1.
40
// If parsing HTML headers, this indicates that you should probably buffer up
41
// more data.
42
int OffsetToAfterNextCRLF();
43
44
// Takers
45
46
void Take(size_t length, std::string *dest);
47
void Take(size_t length, char *dest);
48
void TakeAll(std::string *dest) { Take(size(), dest); }
49
// On failure, return value < 0 and *dest is unchanged.
50
// Strips off the actual CRLF from the result.
51
int TakeLineCRLF(std::string *dest);
52
53
// Skippers
54
void Skip(size_t length);
55
// Returns -1 on failure (no CRLF within sight).
56
// Otherwise returns the length of the line skipped, not including CRLF. Can be 0.
57
int SkipLineCRLF();
58
59
// Utility functions.
60
void Printf(const char *fmt, ...);
61
62
// Dumps the entire buffer to the string, but keeps it around.
63
// Only to be used for debugging, since it might not be fast at all.
64
void PeekAll(std::string *dest);
65
66
// Simple I/O.
67
68
// Writes the entire buffer to the file descriptor. Also resets the
69
// size to zero. On failure, data remains in buffer and nothing is
70
// written.
71
bool FlushToFile(const Path &filename);
72
73
// Utilities. Try to avoid checking for size.
74
size_t size() const { return data_.size(); }
75
bool empty() const { return size() == 0; }
76
void clear() { data_.resize(0); }
77
bool IsVoid() const { return void_; }
78
79
protected:
80
// TODO: Find a better internal representation, like a cord.
81
std::vector<char> data_;
82
bool void_ = false;
83
84
private:
85
DISALLOW_COPY_AND_ASSIGN(Buffer);
86
};
87
88