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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
Title: polystring.cpp - 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#elif defined(_WIN32)
#include "winconfig.h"
#else
#error "No configuration file"
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "globals.h"
#include "polystring.h"
#include "run_time.h"
#include "mpoly.h"
#include "sys.h"
#include "arb.h"
#include "save_vec.h"
#include "processes.h"
#define SAVE(x) mdTaskData->saveVec.push(x)
#define SIZEOF(x) (sizeof(x)/sizeof(PolyWord))
#define DEREFSTRINGHANDLE(_x) ((PolyStringObject *)(_x)->WordP())
// Return empty string.
PolyWord EmptyString(TaskData *mdTaskData)
{
// It might be preferable to have a single empty string.
PolyStringObject *result = (PolyStringObject *)(alloc(mdTaskData, 1, F_BYTE_OBJ));
result->length = 0;
return result;
}
PolyWord C_string_to_Poly(TaskData *mdTaskData, const char *buffer, size_t buffLen)
/* Returns a C string as a Poly string. */
{
if (buffer == NULL) return EmptyString(mdTaskData);
if (buffLen == (size_t)-1) buffLen = strlen(buffer);
/* Get the number of words required, plus 1 for length word,
plus flag bit. */
PolyStringObject *result = (PolyStringObject *)(alloc(mdTaskData, WORDS(buffLen) + 1, F_BYTE_OBJ));
/* Set length of string, then copy the characters. */
result->length = buffLen;
memcpy(result->chars,buffer,buffLen);
return result;
} /* C_string_to_Poly */
POLYUNSIGNED Poly_string_to_C(PolyWord ps, char *buff, POLYUNSIGNED bufflen)
/* Copies the characters from the string into the destination buffer.
Returns original length of string. */
{
PolyStringObject *str = (PolyStringObject *)ps.AsObjPtr();
POLYUNSIGNED chars = str->length >= bufflen ? bufflen-1 : str->length;
if (chars != 0) strncpy(buff, str->chars, chars);
buff[chars] = '\0';
return chars;
} /* Poly_string_to_C */
char *Poly_string_to_C_alloc(PolyWord ps, size_t extraChars)
/* Similar to Poly_string_to_C except that the string is allocated using
malloc and must be freed by the caller. */
{
PolyStringObject * str = (PolyStringObject *)ps.AsObjPtr();
POLYUNSIGNED chars = str->length;
char *res = (char*)malloc(chars + extraChars + 1);
if (res == 0) return 0;
if (chars != 0) strncpy(res, str->chars, chars);
res[chars] = '\0';
return res;
} /* Poly_string_to_C_alloc */
TempCString::~TempCString()
{
free(m_value);
}
#if (defined(_WIN32) && defined(UNICODE))
unsigned int codePage = CP_ACP;
bool setWindowsCodePage(const TCHAR *codePageArg)
{
if (_tcscmp(codePageArg, _T("CP_ACP")) == 0) { codePage = CP_ACP; return true; }
if (_tcscmp(codePageArg, _T("CP_UTF7")) == 0 || lstrcmpi(codePageArg, _T("utf7")) == 0)
{ codePage = CP_UTF7; return true; }
if (_tcscmp(codePageArg, _T("CP_UTF8")) == 0 || lstrcmpi(codePageArg, _T("utf8")) == 0)
{ codePage = CP_UTF8; return true; }
if (*codePageArg >= '0' && *codePageArg <= '9')
{
TCHAR *endp;
codePage = _tcstol(codePageArg, &endp, 10);
return true;
}
return false;
}
/* We need Unicode versions of these. */
PolyWord C_string_to_Poly(TaskData *mdTaskData, const WCHAR *buffer, size_t buffLen)
/* Returns a Unicode string as a Poly string. */
{
if (buffer == NULL) return EmptyString(mdTaskData);
// Get the length of the string, without the terminating null.
if (buffLen == -1) buffLen = wcslen(buffer);
if (buffLen == 0) return EmptyString(mdTaskData); // If it's zero return empty string.
// Find the length when converted.
int outputLen = WideCharToMultiByte(codePage, 0, buffer, (int)buffLen, NULL, 0, NULL, NULL);
// Return the null string if there's an error
if (outputLen <= 0) return EmptyString(mdTaskData);
// Get the number of words required, plus 1 for length word, plus flag bit.
PolyStringObject *result = (PolyStringObject *)(alloc(mdTaskData, WORDS(outputLen) + 1, F_BYTE_OBJ));
// Set length of string, then copy the characters.
result->length = outputLen;
int check = WideCharToMultiByte(codePage, 0, buffer, (int)buffLen, result->chars, outputLen, NULL, NULL);
if (check <= 0) return EmptyString(mdTaskData);
return result;
}
POLYUNSIGNED Poly_string_to_C(PolyWord ps, WCHAR *buff, POLYUNSIGNED bufflen)
{
PolyStringObject *str = (PolyStringObject *)ps.AsObjPtr();
int iLength = (int)str->length;
if (iLength == 0)
{
// Null string.
if (bufflen != 0) buff[0] = 0;
return 0;
}
const char *iPtr = str->chars;
// We can convert it directly using the maximum string length.
int space = MultiByteToWideChar(codePage, 0, iPtr, iLength, buff, (int)bufflen-1);
if (space <= 0)
{
if (bufflen != 0) buff[0] = 0;
return 0; // Error
}
buff[space] = 0; // Null terminate
return space;
}
WCHAR *Poly_string_to_U_alloc(PolyWord ps, size_t extraChars)
{
PolyStringObject *str = (PolyStringObject *)ps.AsObjPtr();
int iLength = (int)str->length;
if (iLength == 0 && extraChars == 0) return _wcsdup(L"");
const char *iPtr = str->chars;
// Find the space required.
int chars = MultiByteToWideChar(codePage, 0, iPtr, iLength, NULL, 0);
if (chars <= 0) return _wcsdup(L"");
WCHAR *res = (WCHAR*)malloc((chars+1+extraChars) * sizeof(WCHAR));
if (res == 0) return 0;
chars = MultiByteToWideChar(codePage, 0, iPtr, iLength, res, chars);
res[chars] = 0;
return res;
}
// convert_string_list return a list of strings.
// This converts Unicode strings.
Handle convert_string_list(TaskData *mdTaskData, int count, WCHAR **strings)
{
Handle saved = mdTaskData->saveVec.mark();
Handle list = SAVE(ListNull);
// It's simplest to process the strings in reverse order */
for (int i = count - 1; 0 <= i; i--)
{
Handle value = SAVE(C_string_to_Poly(mdTaskData, strings[i]));
Handle next = alloc_and_save(mdTaskData, SIZEOF(ML_Cons_Cell));
DEREFLISTHANDLE(next)->h = DEREFWORDHANDLE(value);
DEREFLISTHANDLE(next)->t = DEREFLISTHANDLE(list);
// reset save vector to stop it overflowing
mdTaskData->saveVec.reset(saved);
list = SAVE(DEREFHANDLE(next));
}
return list;
}
TempString::~TempString()
{
free(m_value);
}
#endif
/* convert_string_list return a list of strings. */
Handle convert_string_list(TaskData *mdTaskData, int count, char **strings)
{
Handle saved = mdTaskData->saveVec.mark();
Handle list = SAVE(ListNull);
// It's simplest to process the strings in reverse order */
for (int i = count - 1; 0 <= i; i--)
{
/*
The order of these declarations is important, becaue we don't
want to have make to make the cons cell mutable. This is only
safe if we promise to initialise it fully before the next
ML heap allocation. SPF 29/11/96
*/
Handle value = SAVE(C_string_to_Poly(mdTaskData, strings[i]));
Handle next = alloc_and_save(mdTaskData, SIZEOF(ML_Cons_Cell));
DEREFLISTHANDLE(next)->h = DEREFWORDHANDLE(value);
DEREFLISTHANDLE(next)->t = DEREFLISTHANDLE(list);
// reset save vector to stop it overflowing
mdTaskData->saveVec.reset(saved);
list = SAVE(DEREFHANDLE(next));
}
return list;
}
/* Convert a string list to a vector of C strings. */
char **stringListToVector(Handle list)
{
int len = 0;
/* Find the length of the list */
for (PolyWord p = DEREFHANDLE(list); p != ListNull; p = ((ML_Cons_Cell*)p.AsObjPtr())->t) len++;
/* Allocate vector. */
char **vec = (char**)calloc(len+1, sizeof(char*));
/* Copy the strings and put them into the vector. */
len = 0;
PolyWord q = DEREFHANDLE(list);
while (q != ListNull) {
ML_Cons_Cell *cell = (ML_Cons_Cell*)q.AsObjPtr();
vec[len++] = Poly_string_to_C_alloc(cell->h);
q = cell->t;
}
return vec;
}
/* Free the memory used by the vector. */
void freeStringVector(char **vec)
{
char **p;
if (vec == 0) return;
for (p = vec; *p != 0; p++) free(*p);
free(vec);
}
// Concatenate two strings. Now used only internally in the RTS in process_env.cpp
Handle strconcatc(TaskData *mdTaskData, Handle y, Handle x)
/* Note: arguments are in the reverse order from Poly */
{
POLYUNSIGNED xlen = DEREFSTRINGHANDLE(x)->length;
/* Don't concatenate with null strings */
if (xlen == 0) return y;
POLYUNSIGNED ylen = DEREFSTRINGHANDLE(y)->length;
if (ylen == 0) return x;
POLYUNSIGNED len = xlen + ylen;
/* Get store for combined string. Include rounding up to next word and
room for the length word and add in the flag. */
Handle result = alloc_and_save(mdTaskData, (len + sizeof(PolyWord)-1)/sizeof(PolyWord) + 1, F_BYTE_OBJ);
DEREFSTRINGHANDLE(result)->length = len;
/* Copy first string */
char *to_ptr = DEREFSTRINGHANDLE(result)->chars;
char *from_ptr = DEREFSTRINGHANDLE(x)->chars;
while (xlen-- > 0) (*to_ptr++ = *from_ptr++);
/* Add on second */
from_ptr = DEREFSTRINGHANDLE(y)->chars;
while (ylen-- > 0) (*to_ptr++ = *from_ptr++);
return(result);
} /* strconcat */
// Only used in Xwindows and then only for debugging.
void print_string(PolyWord s)
{
extern FILE *polyStdout;
PolyStringObject * str = (PolyStringObject *)s.AsObjPtr();
fwrite(str->chars, 1, str->length, polyStdout);
}
|