Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
wrapprint.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file wrapprint.c
5 
6  \version V1.2
7  \date 07.07.14
8  \brief
9 
10  \copyright (c) Dr. Andrew C. R. Martin, University of Reading, 2002-14
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39 
40 **************************************************************************
41 
42  Usage:
43  ======
44 
45 **************************************************************************
46 
47  Revision History:
48  =================
49 - V1.2 07.07.14 Use bl prefix for functions By: CTP
50 
51 *************************************************************************/
52 /* Doxygen
53  -------
54  #GROUP General Programming
55  #SUBGROUP String handling
56  #FUNCTION blWrapString()
57  Wraps a string with double inverted commas if it contains spaces
58  and escapes any contained double inverted commas with a backslash.
59 
60  #FUNCTION blWrapPrint()
61  Wraps a string with double inverted commas if it contains spaces
62  and escapes any contained double inverted commas with a backslash.
63  Then prints the string
64 */
65 /************************************************************************/
66 /* Includes
67 */
68 #include <stdio.h>
69 #include <string.h>
70 #include <stdlib.h>
71 #include "SysDefs.h"
72 #include "general.h"
73 
74 /************************************************************************/
75 /* Defines and macros
76 */
77 
78 /************************************************************************/
79 /* Globals
80 */
81 
82 /************************************************************************/
83 /* Prototypes
84 */
85 
86 /************************************************************************/
87 /*>BOOL blWrapString(char *in, char *out, int maxlen)
88  --------------------------------------------------
89 *//**
90 
91  \param[in] *in Input string
92  \param[in] maxlen Max length of output string
93  \param[out] *out Output wrapped string
94  \return Output string was long enough
95 
96  Wraps a string with double inverted commas if it contains spaces
97  and escapes any contained double inverted commas with a backslash.
98  If the output string wasn't big enough, then the routine just
99  returns FALSE without copying anything into the output string.
100 
101 - 30.05.02 Original By: ACRM
102 - 07.07.14 Use bl prefix for functions By: CTP
103 */
104 BOOL blWrapString(char *in, char *out, int maxlen)
105 {
106  int len,
107  ndic,
108  i;
109  char *chp;
110  BOOL hasWhitespace = FALSE;
111 
112  out[0] = '\0';
113 
114  /* Get the length of the input string */
115  len = strlen(in);
116 
117  /* If the string is blank, just return two double inverted commas */
118  if(!len)
119  {
120  if(maxlen<3)
121  return(FALSE);
122  strcpy(out, "\"\"");
123  return(TRUE);
124  }
125 
126  /* Increment len to account for the terminating NUL */
127  len++;
128 
129  /* See if the string contains spaces - if so we need to wrap the
130  string with double inverted commas. Set the flag to say there
131  is whitespace and increment the string length by two to
132  account for the two double inverted commas.
133  */
134  if(strchr(in,' ') || strchr(in,'\t'))
135  {
136  hasWhitespace = TRUE;
137  len += 2;
138  }
139 
140  /* See if the string has double inverted commas - if so we need
141  to escape them, so increment the string length
142  */
143  ndic = blCountchar(in, '"');
144  len += ndic;
145 
146  /* Check that there is space for our padded string */
147  if(len > maxlen)
148  return(FALSE);
149 
150  /* Now create the output string */
151  i=0;
152 
153  if(hasWhitespace)
154  {
155  /* Put in the leading double inverted commas */
156  out[i++] = '"';
157  }
158 
159  /* Copy the string one character at a time, escaping the double
160  inverted commas
161  */
162  for(chp=in; *chp; chp++)
163  {
164  if(*chp == '"')
165  {
166  out[i++] = '\\';
167  out[i++] = '"';
168  }
169  else
170  {
171  out[i++] = *chp;
172  }
173  }
174 
175  if(hasWhitespace)
176  {
177  /* Put in the traling double inverted commas */
178  out[i++] = '"';
179  }
180 
181  /* Terminate the string */
182  out[i] = '\0';
183 
184  return(TRUE);
185 }
186 
187 /************************************************************************/
188 /*>BOOL blWrapPrint(FILE *out, char *string)
189  -----------------------------------------
190 *//**
191 
192  \param[in] *out Output file pointer
193  \param[in] *string String to be printed
194  \return OK?
195 
196  Wraps a string with double inverted commas if it contains spaces
197  and escapes any contained double inverted commas with a backslash.
198  Allocates memory for temporary storage of the wrapped string.
199  Returns FALSE if this memory allocation failed.
200 
201 - 30.05.02 Original By: ACRM
202 - 07.07.14 Use bl prefix for functions By: CTP
203 */
204 BOOL blWrapPrint(FILE *out, char *string)
205 {
206  int len;
207  char *buffer;
208 
209  len = 2 * strlen(string);
210  if(len < 8)
211  len=8;
212 
213  if((buffer=(char *)malloc(len*sizeof(char)))==NULL)
214  return(FALSE);
215 
216  if(!blWrapString(string, buffer, len))
217  {
218  free(buffer);
219  return(FALSE);
220  }
221 
222  fprintf(out, "%s ", buffer);
223  free(buffer);
224 
225  return(TRUE);
226 }
227 
short BOOL
Definition: SysDefs.h:64
#define NULL
Definition: array2.c:99
int blCountchar(char *string, char ch)
Definition: countchar.c:115
#define FALSE
Definition: macros.h:223
BOOL blWrapString(char *in, char *out, int maxlen)
Definition: wrapprint.c:104
BOOL blWrapPrint(FILE *out, char *string)
Definition: wrapprint.c:204
#define TRUE
Definition: macros.h:219
Header file for general purpose routines.
System-type variable type definitions.