1/*
2    Title:  polystring.h - String functions and types
3
4    Copyright (c) 2006, 2015 David C.J. Matthews
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License  version 2.1 as published by the Free Software Foundation.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19*/
20
21#ifndef POLYSTRING_H
22#define POLYSTRING_H
23
24#include "globals.h" // For PolyObject
25
26class SaveVecEntry;
27typedef SaveVecEntry *Handle;
28class TaskData;
29
30// A string object.
31class PolyStringObject: public PolyObject {
32
33public:
34    POLYUNSIGNED length;
35    char chars[1];
36};
37
38inline POLYUNSIGNED PolyStringLength(PolyWord ps) { return ((PolyStringObject*)ps.AsObjPtr())->length; }
39
40// We often want to be able to allocate a temporary C string from a Poly string
41// and have it automatically deallocated when the context has been exited.
42
43extern PolyWord EmptyString(TaskData *mdTaskData);
44
45/* PolyStringObject functions */
46extern PolyWord C_string_to_Poly(TaskData *mdTaskData, const char *buffer, size_t buffLen = -1);
47extern POLYUNSIGNED Poly_string_to_C(PolyWord ps, char *buff, POLYUNSIGNED bufflen);
48extern char *Poly_string_to_C_alloc(PolyWord ps, size_t extraChars = 0);
49
50extern Handle convert_string_list(TaskData *mdTaskData, int count, char **strings);
51
52// Dynamically allocated strings with automatic freeing.
53// These are mainly used for file-names.
54class TempCString
55{
56public:
57    TempCString(char *p = 0):  m_value(p) {}
58    TempCString(PolyWord ps): m_value(Poly_string_to_C_alloc(ps)) {}
59    ~TempCString();
60
61    operator char*() { return m_value; }
62    char* operator = (char* p)  { return (m_value = p); }
63
64private:
65    char *m_value;
66};
67
68#if (defined(_WIN32) && defined(UNICODE))
69
70extern unsigned int codePage;
71
72extern bool setWindowsCodePage(const TCHAR *codePageArg);
73
74#ifdef HAVE_TCHAR_H
75#include <tchar.h>
76#else
77#define WCHAR short
78#define TCHAR char
79#endif
80
81extern PolyWord C_string_to_Poly(TaskData *mdTaskData, const WCHAR *buffer, size_t buffLen = -1);
82extern POLYUNSIGNED Poly_string_to_C(PolyWord ps, WCHAR *buff, POLYUNSIGNED bufflen);
83extern WCHAR *Poly_string_to_U_alloc(PolyWord ps, size_t extraChars = 0);
84
85extern Handle convert_string_list(TaskData *mdTaskData, int count, WCHAR **strings);
86
87// Poly_string_to_T_alloc returns a Unicode string in Unicode and char string otherwise.
88#define Poly_string_to_T_alloc  Poly_string_to_U_alloc
89
90// Unicode on Windows, character strings elsewhere.
91class TempString
92{
93public:
94    TempString(TCHAR *p = 0): m_value(p) {}
95    TempString(PolyWord ps): m_value(Poly_string_to_T_alloc(ps)) {}
96    ~TempString();
97
98    operator TCHAR*() { return m_value; }
99    TCHAR* operator = (TCHAR* p)  { return (m_value = p); }
100
101private:
102    TCHAR *m_value;
103};
104
105#else
106#define Poly_string_to_T_alloc  Poly_string_to_C_alloc
107#define TempString TempCString
108#endif
109
110extern char **stringListToVector(Handle list);
111extern void freeStringVector(char **vec);
112extern void print_string(PolyWord s);
113
114// These should no longer be used in the RTS except internally.
115extern Handle strconcatc(TaskData *mdTaskData, Handle x, Handle y);
116
117
118#endif /* POLYSTRING_H */
119