File: polystring.h

package info (click to toggle)
polyml 5.7.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 40,616 kB
  • sloc: cpp: 44,142; ansic: 26,963; sh: 22,002; asm: 13,486; makefile: 602; exp: 525; python: 253; awk: 91
file content (118 lines) | stat: -rw-r--r-- 3,602 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
    Title:  polystring.h - String functions and types

    Copyright (c) 2006, 2015 David C.J. Matthews

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License  version 2.1 as published by the Free Software Foundation.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

#ifndef POLYSTRING_H
#define POLYSTRING_H

#include "globals.h" // For PolyObject

class SaveVecEntry;
typedef SaveVecEntry *Handle;
class TaskData;

// A string object.
class PolyStringObject: public PolyObject {

public:
    POLYUNSIGNED length;
    char chars[1];
};

inline POLYUNSIGNED PolyStringLength(PolyWord ps) { return ((PolyStringObject*)ps.AsObjPtr())->length; }

// We often want to be able to allocate a temporary C string from a Poly string
// and have it automatically deallocated when the context has been exited.

extern PolyWord EmptyString(TaskData *mdTaskData);

/* PolyStringObject functions */
extern PolyWord C_string_to_Poly(TaskData *mdTaskData, const char *buffer, size_t buffLen = -1);
extern POLYUNSIGNED Poly_string_to_C(PolyWord ps, char *buff, POLYUNSIGNED bufflen);
extern char *Poly_string_to_C_alloc(PolyWord ps, size_t extraChars = 0);

extern Handle convert_string_list(TaskData *mdTaskData, int count, char **strings);

// Dynamically allocated strings with automatic freeing.
// These are mainly used for file-names.
class TempCString
{
public:
    TempCString(char *p = 0):  m_value(p) {}
    TempCString(PolyWord ps): m_value(Poly_string_to_C_alloc(ps)) {}
    ~TempCString();

    operator char*() { return m_value; }
    char* operator = (char* p)  { return (m_value = p); }

private:
    char *m_value;
};

#if (defined(_WIN32) && defined(UNICODE))

extern unsigned int codePage;

extern bool setWindowsCodePage(const TCHAR *codePageArg);

#ifdef HAVE_TCHAR_H
#include <tchar.h>
#else
#define WCHAR short
#define TCHAR char
#endif

extern PolyWord C_string_to_Poly(TaskData *mdTaskData, const WCHAR *buffer, size_t buffLen = -1);
extern POLYUNSIGNED Poly_string_to_C(PolyWord ps, WCHAR *buff, POLYUNSIGNED bufflen);
extern WCHAR *Poly_string_to_U_alloc(PolyWord ps, size_t extraChars = 0);

extern Handle convert_string_list(TaskData *mdTaskData, int count, WCHAR **strings);

// Poly_string_to_T_alloc returns a Unicode string in Unicode and char string otherwise.
#define Poly_string_to_T_alloc  Poly_string_to_U_alloc

// Unicode on Windows, character strings elsewhere.
class TempString
{
public:
    TempString(TCHAR *p = 0): m_value(p) {}
    TempString(PolyWord ps): m_value(Poly_string_to_T_alloc(ps)) {}
    ~TempString();

    operator TCHAR*() { return m_value; }
    TCHAR* operator = (TCHAR* p)  { return (m_value = p); }

private:
    TCHAR *m_value;
};

#else
#define Poly_string_to_T_alloc  Poly_string_to_C_alloc
#define TempString TempCString
#endif

extern char **stringListToVector(Handle list);
extern void freeStringVector(char **vec);
extern void print_string(PolyWord s);

// These should no longer be used in the RTS except internally.
extern Handle strconcatc(TaskData *mdTaskData, Handle x, Handle y);


#endif /* POLYSTRING_H */