File: dstr.c

package info (click to toggle)
msort 8.53-2.2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,360 kB
  • sloc: sh: 10,138; ansic: 10,031; makefile: 51
file content (110 lines) | stat: -rw-r--r-- 2,726 bytes parent folder | download | duplicates (3)
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
/* Time-stamp: <2008-09-26 15:07:55 poser> */
/*
 * Copyright (C) 1993-2008 William J. Poser.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include "compdefs.h"

#include <stdlib.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <wchar.h>
#ifdef HAVE_UNINUM_UNICODE_H
#include <uninum/unicode.h>
#else
#include "unicode.h"
#endif
#include "exitcode.h"
#include "retcodes.h"
#define  DSTR
#include "dstr.h"

/* Initialize a dynamic string */

void
InitializeDynamicString (struct dstr *s) {
  s->s = NULL;
  s->c = s->l = 0;
}

void
FreeDynamicString(struct dstr *d) {
  if (d) {
    if (d->s != NULL) free( (void *) (d->s));
    free ( (void *) d);
  }
}

/* 
 * Create a dynamic wide string and copy into it an existing regular wide string.
 */

struct dstr *MakeDynamicString(wchar_t *s) {
  struct dstr *new;
  int length;

  new = (struct dstr *) malloc(sizeof(struct dstr));
  if(new == NULL) exit(OUTOFMEMORY);
  length = wcslen(s);
  new->s = (wchar_t *) malloc((length + 1) * sizeof(wchar_t));
  if(new->s == NULL) exit(OUTOFMEMORY);
  wcscpy(new->s,s);
  new->c = new->l = length;
  return(new);
} 

/* 
 * Insert an existing wide string into a dynamic string.
 * The source is expected to be non-null.
 */

int
FillDynamicString(struct dstr *tgt, wchar_t *src) {
  int length;
  length = wcslen(src);
#ifdef SAFECALL
  if(length == 0) return(ERROR);
#endif
  if (length + 1 > tgt->c) {
    if(tgt->s != NULL) free( (void *) tgt->s);
    tgt->s = (wchar_t *) malloc((length + 1) * sizeof(wchar_t));
    if(tgt->s == NULL) return(ERROR);
    tgt->c = length;
  }
  wcscpy(tgt->s,src);
  tgt->l = length;
  return(SUCCESS);
} 

/* Append a wide string to a dynamic wide string */
int
AppendToDynamicString(struct dstr *tgt, wchar_t *src) {
  int SrcLength;
  int NewLength;

  SrcLength = wcslen(src);
  NewLength = SrcLength + tgt->l;

  if (NewLength > tgt->c) {
    tgt->s = (wchar_t *) realloc(tgt->s,(NewLength + 1) * sizeof(wchar_t));
    if(tgt->s == NULL) return(ERROR);
    tgt->c = NewLength;
  }
  wcscpy(tgt->s+tgt->l,src);
  tgt->l = NewLength;
  return(SUCCESS);
}