00001 /********************************************************************** 00002 * cbf_context -- handle cbf contexts * 00003 * * 00004 * Version 0.6 13 January 1999 * 00005 * * 00006 * Paul Ellis (ellis@ssrl.slac.stanford.edu) and * 00007 * Herbert J. Bernstein (yaya@bernstein-plus-sons.com) * 00008 **********************************************************************/ 00009 00010 /********************************************************************** 00011 * NOTICE * 00012 * Creative endeavors depend on the lively exchange of ideas. There * 00013 * are laws and customs which establish rights and responsibilities * 00014 * for authors and the users of what authors create. This notice * 00015 * is not intended to prevent you from using the software and * 00016 * documents in this package, but to ensure that there are no * 00017 * misunderstandings about terms and conditions of such use. * 00018 * * 00019 * Please read the following notice carefully. If you do not * 00020 * understand any portion of this notice, please seek appropriate * 00021 * professional legal advice before making use of the software and * 00022 * documents included in this software package. In addition to * 00023 * whatever other steps you may be obliged to take to respect the * 00024 * intellectual property rights of the various parties involved, if * 00025 * you do make use of the software and documents in this package, * 00026 * please give credit where credit is due by citing this package, * 00027 * its authors and the URL or other source from which you obtained * 00028 * it, or equivalent primary references in the literature with the * 00029 * same authors. * 00030 * * 00031 * Some of the software and documents included within this software * 00032 * package are the intellectual property of various parties, and * 00033 * placement in this package does not in any way imply that any * 00034 * such rights have in any way been waived or diminished. * 00035 * * 00036 * With respect to any software or documents for which a copyright * 00037 * exists, ALL RIGHTS ARE RESERVED TO THE OWNERS OF SUCH COPYRIGHT. * 00038 * * 00039 * Even though the authors of the various documents and software * 00040 * found here have made a good faith effort to ensure that the * 00041 * documents are correct and that the software performs according * 00042 * to its documentation, and we would greatly appreciate hearing of * 00043 * any problems you may encounter, the programs and documents any * 00044 * files created by the programs are provided **AS IS** without any * 00045 * warranty as to correctness, merchantability or fitness for any * 00046 * particular or general use. * 00047 * * 00048 * THE RESPONSIBILITY FOR ANY ADVERSE CONSEQUENCES FROM THE USE OF * 00049 * PROGRAMS OR DOCUMENTS OR ANY FILE OR FILES CREATED BY USE OF THE * 00050 * PROGRAMS OR DOCUMENTS LIES SOLELY WITH THE USERS OF THE PROGRAMS * 00051 * OR DOCUMENTS OR FILE OR FILES AND NOT WITH AUTHORS OF THE * 00052 * PROGRAMS OR DOCUMENTS. * 00053 **********************************************************************/ 00054 00055 /********************************************************************** 00056 * The IUCr Policy * 00057 * on * 00058 * the Use of the Crystallographic Information File (CIF) * 00059 * * 00060 * The Crystallographic Information File (Hall, Allen & Brown, * 00061 * 1991) is, as of January 1992, the recommended method for * 00062 * submitting publications to Acta Crystallographica Section C. The * 00063 * International Union of Crystallography holds the Copyright on * 00064 * the CIF, and has applied for Patents on the STAR File syntax * 00065 * which is the basis for the CIF format. * 00066 * * 00067 * It is a principal objective of the IUCr to promote the use of * 00068 * CIF for the exchange and storage of scientific data. The IUCr's * 00069 * sponsorship of the CIF development was motivated by its * 00070 * responsibility to its scientific journals, which set the * 00071 * standards in crystallographic publishing. The IUCr intends that * 00072 * CIFs will be used increasingly for electronic submission of * 00073 * manuscripts to these journals in future. The IUCr recognises * 00074 * that, if the CIF and the STAR File are to be adopted as a means * 00075 * for universal data exchange, the syntax of these files must be * 00076 * strictly and uniformly adhered to. Even small deviations from * 00077 * the syntax would ultimately cause the demise of the universal * 00078 * file concept. Through its Copyrights and Patents the IUCr has * 00079 * taken the steps needed to ensure strict conformance with this * 00080 * syntax. * 00081 * * 00082 * The IUCr policy on the use of the CIF and STAR File processes is * 00083 * as follows: * 00084 * _________________________________________________________________ * 00085 * * 00086 * * 1 CIFs and STAR Files may be generated, stored or transmitted, * 00087 * without permission or charge, provided their purpose is not * 00088 * specifically for profit or commercial gain, and provided that * 00089 * the published syntax is strictly adhered to. * 00090 * * 2 Computer software may be developed for use with CIFs or STAR * 00091 * files, without permission or charge, provided it is distributed * 00092 * in the public domain. This condition also applies to software * 00093 * for which a charge is made, provided that its primary function * 00094 * is for use with files that satisfy condition 1 and that it is * 00095 * distributed as a minor component of a larger package of * 00096 * software. * 00097 * * 3 Permission will be granted for the use of CIFs and STAR Files * 00098 * for specific commercial purposes (such as databases or network * 00099 * exchange processes), and for the distribution of commercial * 00100 * CIF/STAR software, on written application to the IUCr Executive * 00101 * Secretary, 2 Abbey Square, Chester CH1 2HU, England. The * 00102 * nature, terms and duration of the licences granted will be * 00103 * determined by the IUCr Executive and Finance Committees. * 00104 * * 00105 * _________________________________________________________________ * 00106 * * 00107 * In summary, the IUCr wishes to promote the use of the STAR File * 00108 * concepts as a standard universal data file. It will insist on * 00109 * strict compliance with the published syntax for all * 00110 * applications. To assist with this compliance, the IUCr provides * 00111 * public domain software for checking the logical integrity of a * 00112 * CIF, and for validating the data name definitions contained * 00113 * within a CIF. Detailed information on this software, and the * 00114 * associated dictionaries, may be obtained from the IUCr Office at * 00115 * 5 Abbey Square, Chester CH1 2HU, England. * 00116 **********************************************************************/ 00117 00118 #ifdef __cplusplus 00119 00120 extern "C" { 00121 00122 #endif 00123 00124 #include "cbf.h" 00125 #include "cbf_alloc.h" 00126 #include "cbf_context.h" 00127 00128 #include <stdlib.h> 00129 #include <string.h> 00130 #include <limits.h> 00131 00132 00133 /* Create and initialise a context */ 00134 00135 int cbf_make_context (cbf_context **context) 00136 { 00137 /* Allocate the memory */ 00138 00139 cbf_failnez (cbf_alloc ((void **) context, NULL, sizeof (cbf_context), 1)) 00140 00141 00142 /* Initialise */ 00143 00144 (*context)->temporary = NULL; 00145 00146 (*context)->connections = 1; 00147 00148 00149 /* Success */ 00150 00151 return 0; 00152 } 00153 00154 00155 /* Free a context */ 00156 00157 int cbf_free_context (cbf_context **context) 00158 { 00159 int errorcode; 00160 00161 errorcode = 0; 00162 00163 if (context) 00164 00165 if (*context) 00166 { 00167 if ((*context)->temporary) 00168 00169 errorcode = cbf_free_file (&(*context)->temporary); 00170 00171 errorcode |= cbf_free ((void **) context, NULL); 00172 } 00173 00174 00175 /* Success? */ 00176 00177 return errorcode; 00178 } 00179 00180 00181 /* Add a context connection */ 00182 00183 int cbf_add_contextconnection (cbf_context **context) 00184 { 00185 /* Does the context pointer exist? */ 00186 00187 if (!context) 00188 00189 return CBF_ARGUMENT; 00190 00191 00192 /* Does the context exist? */ 00193 00194 if (*context) 00195 { 00196 (*context)->connections++; 00197 00198 return 0; 00199 } 00200 00201 00202 /* Create a new context */ 00203 00204 return cbf_make_context (context); 00205 } 00206 00207 00208 /* Remove a context connection */ 00209 00210 int cbf_delete_contextconnection (cbf_context **context) 00211 { 00212 /* Does the context pointer exist? */ 00213 00214 if (!context) 00215 00216 return CBF_ARGUMENT; 00217 00218 00219 /* Does the context exist? */ 00220 00221 if (!*context) 00222 00223 return CBF_ARGUMENT; 00224 00225 00226 /* Remove a connection */ 00227 00228 (*context)->connections--; 00229 00230 00231 /* Delete the context? */ 00232 00233 if ((*context)->connections == 0) 00234 00235 return cbf_free_context (context); 00236 00237 00238 /* Success */ 00239 00240 return 0; 00241 } 00242 00243 00244 /* Open a temporary file connection */ 00245 00246 int cbf_open_temporary (cbf_context *context, cbf_file **temporary) 00247 { 00248 FILE *stream; 00249 00250 int errorcode; 00251 00252 00253 /* Check the arguments */ 00254 00255 if (!context || !temporary) 00256 00257 return CBF_ARGUMENT; 00258 00259 00260 /* Does a temporary file already exist? */ 00261 00262 if (context->temporary) 00263 { 00264 cbf_failnez (cbf_add_fileconnection (&context->temporary, NULL)) 00265 00266 *temporary = context->temporary; 00267 00268 return 0; 00269 } 00270 00271 00272 /* Create the temporary file */ 00273 00274 stream = tmpfile (); 00275 00276 if (!stream) 00277 00278 return CBF_FILEOPEN; 00279 00280 errorcode = cbf_make_file (&context->temporary, stream); 00281 00282 if (errorcode) 00283 { 00284 if (fclose (stream)) 00285 00286 errorcode |= CBF_FILECLOSE; 00287 00288 return errorcode; 00289 } 00290 00291 00292 /* Open a connection */ 00293 00294 return cbf_open_temporary (context, temporary); 00295 } 00296 00297 00298 /* Close a temporary file connection */ 00299 00300 int cbf_close_temporary (cbf_context *context, cbf_file **temporary) 00301 { 00302 /* Check the arguments */ 00303 00304 if (!context || !temporary) 00305 00306 return CBF_ARGUMENT; 00307 00308 if (!*temporary) 00309 00310 return CBF_ARGUMENT; 00311 00312 00313 /* Check that the temporary file matches */ 00314 00315 if (context->temporary != *temporary) 00316 00317 return CBF_NOTFOUND; 00318 00319 00320 /* Delete the connection */ 00321 00322 cbf_failnez (cbf_delete_fileconnection (&context->temporary)) 00323 00324 *temporary = NULL; 00325 00326 00327 /* Is there only one connection left? */ 00328 00329 if (context->temporary) 00330 00331 if (cbf_file_connections (context->temporary) == 1) 00332 00333 cbf_failnez (cbf_free_file (&context->temporary)) 00334 00335 00336 /* Success */ 00337 00338 return 0; 00339 } 00340 00341 00342 /* Copy a string */ 00343 00344 const char *cbf_copy_string (cbf_context *context, const char *string, 00345 char type) 00346 { 00347 char *new_string; 00348 00349 if (string) 00350 00351 if (type) 00352 { 00353 if (cbf_alloc ((void **) &new_string, NULL, 00354 sizeof (char), strlen (string) + 2) == 0) 00355 { 00356 *new_string = type; 00357 00358 strcpy (new_string + 1, string); 00359 00360 return new_string; 00361 } 00362 } 00363 else 00364 00365 if (cbf_alloc ((void **) &new_string, NULL, \ 00366 sizeof (char), strlen (string) + 1) == 0) 00367 { 00368 strcpy (new_string, string); 00369 00370 return new_string; 00371 } 00372 00373 00374 /* Fail */ 00375 00376 return NULL; 00377 } 00378 00379 00380 /* Free a string */ 00381 00382 void cbf_free_string (cbf_context *context, const char *string) 00383 { 00384 cbf_free ((void **) &string, NULL); 00385 } 00386 00387 00388 #ifdef __cplusplus 00389 00390 } 00391 00392 #endif