Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Buffer.h
3185 views
1
#pragma once
2
3
#include <string>
4
#include <string_view>
5
#include <vector>
6
7
#include "Common/Common.h"
8
#include "Common/Data/Collections/CharQueue.h"
9
10
class Path;
11
12
// Acts as a queue. Intended to be as fast as possible for most uses.
13
// Does not do synchronization, must use external mutexes.
14
class Buffer {
15
public:
16
Buffer() = default;
17
Buffer(Buffer &&) = default;
18
19
static Buffer Void() {
20
Buffer buf;
21
buf.void_ = true;
22
return buf;
23
}
24
25
// Write max [length] bytes to the returned pointer.
26
// Any other operation on this Buffer invalidates the pointer.
27
char *Append(size_t length);
28
29
// These work pretty much like you'd expect.
30
void Append(std::string_view 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, if the clear flag is true. On failure, data remains in buffer and nothing is
70
// written.
71
bool FlushToFile(const Path &filename, bool clear);
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_.clear(); }
77
bool IsVoid() const { return void_; }
78
79
protected:
80
// Custom queue implementation.
81
CharQueue data_;
82
bool void_ = false;
83
84
private:
85
DISALLOW_COPY_AND_ASSIGN(Buffer);
86
};
87
88