VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
encodingDecodingUtils.h
Go to the documentation of this file.
1 /*********************************************************************
2 ** Copyright (c) 2025 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************/
5 
8 
9 #pragma once
10 
11 #include <string>
12 #include <vlutil/vlNetTypes.h>
13 
14 enum class DtPaddingTo
15 {
16  Dt8 = 1,
17  Dt16 = 2,
18  Dt32 = 4,
19  Dt64 = 8
20 };
21 
22 inline unsigned DtPaddingNeeded(unsigned size, DtPaddingTo padTo)
23 {
24  unsigned padding = 0;
25  while ((size + padding) % (unsigned)padTo != 0)
26  {
27  ++padding;
28  }
29 
30  return padding;
31 }
32 
33 inline unsigned DtNetSizeOfWString(const std::wstring& str)
34 {
35  unsigned netSize = sizeof(DtNetU32) // array size field
36  + str.size() * sizeof(DtNetInt16); // size for the string
37 
38  return netSize;
39 }
40 
41 inline char* DtEncodeWString(char* buffer, const std::wstring& str)
42 {
43  DtNetU32* sizePtr = reinterpret_cast<DtNetU32*>(buffer);
44  *sizePtr = str.size();
45 
46  buffer += sizeof(DtNetU32);
47 
48  DtNetInt16 temp;
49  for (int index = 0; index < str.size(); index++)
50  {
51  temp = (DtNetInt16)str[index];
52  memcpy(buffer, &temp, sizeof(DtNetInt16));
53  buffer += sizeof(DtNetInt16);
54  }
55 
56  return buffer;
57 }
58 
59 inline char* DtDecodeWString(char* buffer, std::wstring& str)
60 {
61  DtNetU32* sizePtr = reinterpret_cast<DtNetU32*>(buffer);
62  buffer += sizeof(DtNetU32);
63 
64  wchar_t* outBuffer = new wchar_t[*sizePtr];
65  DtNetInt16* in = reinterpret_cast<DtNetInt16*>(buffer);
66  wchar_t* out = outBuffer;
67 
68  for (unsigned index = 0; index < *sizePtr; ++index, out++, in++)
69  {
70  *out = *in;
71  }
72 
73  str = std::wstring(outBuffer, *sizePtr);
74  delete outBuffer;
75 
76  buffer += sizeof(DtNetInt16) * *sizePtr;
77 
78  return buffer;
79 }
DtPaddingTo
Definition: encodingDecodingUtils.h:14
char * DtEncodeWString(char *buffer, const std::wstring &str)
Definition: encodingDecodingUtils.h:41
unsigned DtNetSizeOfWString(const std::wstring &str)
Definition: encodingDecodingUtils.h:33
voidpf void uLong size
Definition: ioapi.h:39
unsigned DtPaddingNeeded(unsigned size, DtPaddingTo padTo)
Definition: encodingDecodingUtils.h:22
char * DtDecodeWString(char *buffer, std::wstring &str)
Definition: encodingDecodingUtils.h:59

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)