The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
StoreString.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file StoreString.c
5 
6  \version V1.22
7  \date 07.07.14
8  \brief
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1991-2014
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.1 08.02.91 Added KillLine()
50 - V1.2 10.02.91 Added setextn() and index()
51 - V1.3 20.03.91 Added Word()
52 - V1.4 28.05.92 ANSIed
53 - V1.5 22.06.92 Added tab check to Word(). Improved setextn().
54  Added WordN(). Documented other routines.
55 - V1.6 27.07.93 Corrected fsscanf() for double precision
56 - V1.7 07.10.93 Checks made on case before toupper()/tolower()
57  for SysV compatibility. Also index() becomes
58  chindex()
59 - V1.8 18.03.94 getc() -> fgetc()
60 - V1.9 11.05.94 Added GetFilestem(), upstrcmp(), upstrncmp() &
61  GetWord()
62 - V1.10 24.08.94 Added OpenStdFiles()
63 - V1.11 08.03.95 Corrected OpenFile() for non-UNIX
64 - V1.12 09.03.95 Added check on non-NULL filename in OpenFile()
65 - V1.13 17.07.95 Added countchar()
66 - V1.14 18.10.95 Moved YorN() to WindIO.c
67 - V1.15 06.11.95 Added StoreString(), InStringList() and FreeStringList()
68 - V1.16 22.11.95 Moved ftostr() to generam.c
69 - V1.17 15.12.95 Added QueryStrStr()
70 - V1.18 18.12.95 OpenStdFiles() treats filename of - as stdin/stdout
71 - V1.19 05.02.96 OpenStdFiles() allows NULL pointers instead if filenames
72 - V1.20 18.09.96 Added padchar()
73 - V1.21 18.06.02 Added string.h
74 - V1.22 07.07.14 Use bl prefix for functions By: CTP
75 
76 *************************************************************************/
77 /* Doxygen
78  -------
79  #GROUP General Programming
80  #SUBGROUP Lists of strings
81  #FUNCTION blStoreString()
82  Stores strings (of any length) in a linked list of type STRINGLIST.
83  Return a pointer to the start of the linked list which is used on
84  the first call to access the newly allocated memory.
85 */
86 /************************************************************************/
87 /* Includes
88 */
89 #include <stdlib.h>
90 #include <string.h>
91 #include "general.h"
92 #include "macros.h"
93 
94 /************************************************************************/
95 /* Defines and macros
96 */
97 
98 /************************************************************************/
99 /* Globals
100 */
101 
102 /************************************************************************/
103 /* Prototypes
104 */
105 
106 
107 /************************************************************************/
108 /*>STRINGLIST *blStoreString(STRINGLIST *StringList, char *string)
109  ---------------------------------------------------------------
110 *//**
111 
112  \param[in] *StringList The current linked list or NULL
113  if nothing yet allocated
114  \param[in] *string The string to store
115  \return Start of linked list. Used on
116  first call (when input StringList
117  is NULL) to return the pointer to
118  the start of the linked list.
119  NULL if unable to allocate.
120 
121  Stores strings (of any length) in a linked list of type STRINGLIST.
122  Return a pointer to the start of the linked list which is used on
123  the first call to access the newly allocated memory.
124 
125  If allocation fails, memory allocated so far is freed and the routine
126  returns NULL.
127 
128 - 06.11.95 Original By: ACRM
129 - 07.07.14 Use bl prefix for functions By: CTP
130 */
131 STRINGLIST *blStoreString(STRINGLIST *StringList, char *string)
132 {
133  STRINGLIST *p,
134  *start;
135 
136  if((StringList!=NULL) && ((string == NULL) || (string[0] == '\0')))
137  return(StringList);
138 
139  /* Set p to the String List and move to the end of the linked list */
140  start = StringList;
141 
142  /* If nothing in the list, initialise it */
143  if(start == NULL)
144  {
145  INIT(start,STRINGLIST);
146  p=start;
147  }
148  else /* Move to end of current list and add another item */
149  {
150  p=start;
151  LAST(p);
152 
153  /* Only allocate another slot if a string is inserted in this one */
154  if(p->string != NULL)
156  }
157 
158  /* Check allocation */
159  if(p==NULL)
160  {
161  /* If failed, free the list so far and return NULL */
162  FREELIST(start, STRINGLIST);
163  return(NULL);
164  }
165  p->string = NULL;
166 
167  /* Everything OK, allocate memory for the string */
168  if((string != NULL) && (string[0] != '\0'))
169  {
170  if((p->string = (char *)malloc((1+strlen(string))*sizeof(char)))
171  ==NULL)
172  {
173  /* No memory, free linked list and return */
174  FREELIST(start, STRINGLIST);
175  return(NULL);
176  }
177 
178  /* Still OK, copy in the string and return */
179  strcpy(p->string,string);
180  }
181 
182  return(start);
183 }
184 
185 
#define ALLOCNEXT(x, y)
Definition: macros.h:251
#define LAST(x)
Definition: macros.h:259
#define NULL
Definition: array2.c:99
STRINGLIST * blStoreString(STRINGLIST *StringList, char *string)
Definition: StoreString.c:131
Useful macros.
char * start
Definition: safemem.c:114
Header file for general purpose routines.
#define FREELIST(y, z)
Definition: macros.h:264
#define INIT(x, y)
Definition: macros.h:244
char * string
Definition: general.h:85