1 /* $Id: Str.h,v 1.6 2006/04/07 13:35:35 inu Exp $ */
2 /*
3  * String manipulation library for Boehm GC
4  *
5  * (C) Copyright 1998-1999 by Akinori Ito
6  *
7  * This software may be redistributed freely for this purpose, in full
8  * or in part, provided that this entire copyright notice is included
9  * on any copies of this software and applications and derivations thereof.
10  *
11  * This software is provided on an "as is" basis, without warranty of any
12  * kind, either expressed or implied, as to any matter including, but not
13  * limited to warranty of fitness of purpose, or merchantability, or
14  * results obtained from use of this software.
15  */
16 #ifndef GC_STR_H
17 #define GC_STR_H
18 #include <stdio.h>
19 #include <string.h>
20 #include <limits.h>
21 #ifdef __EMX__
22 #define strcasecmp	stricmp
23 #define strncasecmp	strnicmp
24 #endif
25 
26 typedef struct _Str {
27     char *ptr;
28     int length;
29     int area_size;
30 } *Str;
31 
32 Str Strnew(void);
33 Str Strnew_size(int);
34 Str Strnew_charp(const char *);
35 Str Strnew_charp_n(const char *, int);
36 Str Strnew_m_charp(const char *, ...);
37 Str Strdup(Str);
38 void Strclear(Str);
39 void Strfree(Str);
40 void Strcopy(Str, Str);
41 void Strcopy_charp(Str, const char *);
42 void Strcopy_charp_n(Str, const char *, int);
43 void Strcat_charp_n(Str, const char *, int);
44 void Strcat(Str, Str);
45 void Strcat_charp(Str, const char *);
46 void Strcat_m_charp(Str, ...);
47 Str Strsubstr(Str, int, int);
48 void Strinsert_char(Str, int, char);
49 void Strinsert_charp(Str, int, const char *);
50 void Strdelete(Str, int, int);
51 void Strtruncate(Str, int);
52 void Strlower(Str);
53 void Strupper(Str);
54 void Strchop(Str);
55 void Strshrink(Str, int);
56 void Strshrinkfirst(Str, int);
57 void Strremovefirstspaces(Str);
58 void Strremovetrailingspaces(Str);
59 Str Stralign_left(Str, int);
60 Str Stralign_right(Str, int);
61 Str Stralign_center(Str, int);
62 
63 Str Sprintf(char *fmt, ...);
64 
65 Str Strfgets(FILE *);
66 Str Strfgetall(FILE *);
67 
68 void Strgrow(Str s);
69 
70 #define STR_SIZE_MAX (INT_MAX / 32)
71 #define Strcat_char(x,y) (((x)->length+1>=STR_SIZE_MAX)?0:(((x)->length+1>=(x)->area_size)?Strgrow(x),0:0,(x)->ptr[(x)->length++]=(y),(x)->ptr[(x)->length]=0))
72 #define Strcatc(x,y) ((x)->ptr[(x)->length++]=(y))
73 #define Strnulterm(x) ((x)->ptr[(x)->length]=0)
74 #define Strcmp(x,y)                  strcmp((x)->ptr,(y)->ptr)
75 #define Strcmp_charp(x,y)            strcmp((x)->ptr,(y))
76 #define Strncmp(x,y,n)               strncmp((x)->ptr,(y)->ptr,(n))
77 #define Strncmp_charp(x,y,n)         strncmp((x)->ptr,(y),(n))
78 #define Strcasecmp(x,y)              strcasecmp((x)->ptr,(y)->ptr)
79 #define Strcasecmp_charp(x,y)        strcasecmp((x)->ptr,(y))
80 #define Strncasecmp(x,y,n)           strncasecmp((x)->ptr,(y)->ptr,(n))
81 #define Strncasecmp_charp(x,y,n)     strncasecmp((x)->ptr,(y),(n))
82 
83 #define Strlastchar(s)               ((s)->length>0?(s)->ptr[(s)->length-1]:'\0')
84 #define Strinsert(s,n,p)             Strinsert_charp((s),(n),(p)->ptr)
85 #define Strshrinkfirst(s,n)          Strdelete((s),0,(n))
86 #define Strfputs(s,f)                fwrite((s)->ptr,1,(s)->length,(f))
87 #endif				/* not GC_STR_H */
88