UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
stringhunk.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "stringhunk.h"
27 #include "../common/common.h"
28 
32 bool STRHUNK_Add (stringHunk_t* hunk, const char* string)
33 {
34  const ptrdiff_t filled = hunk->pos - hunk->hunk;
35  const size_t remaining = hunk->size - filled;
36  const size_t strLength = strlen(string);
37  const bool overflow = remaining <= strLength;
38 
39  if (remaining < 1)
40  return false;
41 
42  /* add delimiter char. */
43  if (hunk->pos != hunk->hunk) {
44  if (remaining == 1)
45  return false;
46  *hunk->pos++ = '\xFF';
47  }
48 
49  Q_strncpyz(hunk->pos, string, remaining - 1);
50  hunk->pos += overflow ? remaining - 1: strLength + 1;
51  hunk->entries++;
52  return !overflow;
53 }
54 
56 {
57  hunk->pos = hunk->hunk;
58  hunk->entries = 0;
59  hunk->hunk[0] = '\0';
60 }
61 
63 {
64  const char* string = hunk->hunk;
65 
66  if (hunk->entries == 0)
67  return;
68 
69  for (;;) {
70  visitor(string);
71  string += strlen(string) + 1;
72  if (*string != '\xFF')
73  break;
74  string++;
75  }
76 }
77 
78 int STRHUNK_Size (const stringHunk_t* hunk)
79 {
80  return hunk->entries;
81 }
82 
83 size_t STRHUNK_GetFreeSpace (const stringHunk_t* hunk)
84 {
85  return hunk->size - (ptrdiff_t)(hunk->pos - hunk->hunk);
86 }
87 
89 {
91  strHunk->size = size;
92  strHunk->hunk = Mem_AllocTypeN(char, size);
93  strHunk->pos = strHunk->hunk;
94  return strHunk;
95 }
96 
98 {
99  Mem_Free(*hunk);
100  *hunk = nullptr;
101 }
#define Mem_AllocTypeN(type, n)
Definition: mem.h:38
Header for string hunk management.
bool STRHUNK_Add(stringHunk_t *hunk, const char *string)
Definition: stringhunk.cpp:32
size_t size
Definition: stringhunk.h:30
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
char * hunk
Definition: stringhunk.h:32
GLsizei size
Definition: r_gl.h:152
stringHunk_t * STRHUNK_Create(size_t size)
Definition: stringhunk.cpp:88
int STRHUNK_Size(const stringHunk_t *hunk)
Definition: stringhunk.cpp:78
void(* stringHunkVisitor_t)(const char *string)
Definition: stringhunk.h:36
void STRHUNK_Reset(stringHunk_t *hunk)
Definition: stringhunk.cpp:55
#define Mem_Free(ptr)
Definition: mem.h:35
#define Mem_AllocType(type)
Definition: mem.h:39
size_t STRHUNK_GetFreeSpace(const stringHunk_t *hunk)
Definition: stringhunk.cpp:83
char * pos
Definition: stringhunk.h:31
void STRHUNK_Delete(stringHunk_t **hunk)
Definition: stringhunk.cpp:97
void STRHUNK_Visit(stringHunk_t *hunk, stringHunkVisitor_t visitor)
Definition: stringhunk.cpp:62