00001 /* EGlib "Efficient General Library" provides some basic structures and 00002 * algorithms commons in many optimization algorithms. 00003 * 00004 * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea. 00005 * 00006 * This library is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU Lesser General Public License as published by the 00008 * Free Software Foundation; either version 2.1 of the License, or (at your 00009 * option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00013 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00014 * License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this library; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00019 * */ 00020 00021 /* ========================================================================= */ 00022 /* input/output utilities 00023 * 00024 * Version 0.0.2 2003-05-09 (Marcos) 00025 * 00026 * Added the function EGioNParse to get a more argc, argv feel. 00027 * Also: Changed EGioParse so that it ignores multiple sequential delimiters. 00028 * 00029 * Version 0.0.1 2003-04-11 00030 * - 2004-08-17 00031 * - Add EGdisplayString function. 00032 * - 2006-08-16 00033 * - Add EGioReadLine function. 00034 * */ 00035 /* ========================================================================= */ 00036 00037 #ifndef __EG_IO_H__ 00038 #define __EG_IO_H__ 00039 #include <string.h> 00040 #include <strings.h> 00041 #include <stdio.h> 00042 #include <stdlib.h> 00043 #include <stdarg.h> 00044 #include <errno.h> 00045 #include "eg_macros.h" 00046 00047 /* ========================================================================= */ 00048 /** @brief safe open function, it test that we can open the given file with the 00049 * given mode, if an error occurs, display it on screen and exit, otherwise, 00050 * return the open file stream. 00051 * @param __file file name to open (is a const char*) 00052 * @param __mode mode to use to open the file (is a const char*) 00053 * @return FILE* stream */ 00054 #define EGsfopen(__file,__mode) ({\ 00055 const char*__EGsfile = (__file);\ 00056 const char*__EGsmode = (__mode);\ 00057 FILE*__EGsFILE = fopen(__EGsfile,__EGsmode);\ 00058 if(!__EGsFILE)\ 00059 {\ 00060 const int __EGserrno = errno;\ 00061 fprintf(stderr,"fopen() failed with error code %d, error\n%s",__EGserrno,strerror(__EGserrno));\ 00062 MESSAGE(0,"Could not open %s with mode %s", __EGsfile, __EGsmode);\ 00063 exit(__EGserrno);\ 00064 }\ 00065 __EGsFILE;}) 00066 00067 /* ========================================================================= */ 00068 /**@brief type of functions for display, receives a void* to the structure to print, 00069 * and a *FILE where to output */ 00070 typedef void (*EGdisplay_f) (void *, 00071 FILE *); 00072 #define EGnullDisplay ((EGdisplay_f)0) 00073 00074 /* ========================================================================= */ 00075 /** @brief type of functions for display, receives a void* to the structure to print, a 00076 * *FILE where to output, and a set of offsets for datas, the length of that 00077 * array must be know by the user. */ 00078 typedef void (*EGdisplayOS_f) (void *, 00079 FILE *, 00080 size_t *); 00081 00082 /* ========================================================================= */ 00083 /** @brief create names with indices */ 00084 void EGmvar (char *str, 00085 int nind, 00086 const char *header, 00087 ...); 00088 00089 /* ========================================================================= */ 00090 /** @brief Given a string 'input' this function uses EGioParse to separate 00091 * up to N words in it, we assume that argc is an array of pointers to strings 00092 * of size N, and note that the input array will be changed. */ 00093 void EGioNParse (char *input, 00094 int max_argc, 00095 const char *delim, 00096 const char *comment, 00097 int *argc, 00098 char **argv); 00099 00100 /* ========================================================================= */ 00101 /** @brief given two *pointers 'next' and 'current' and a constant set 00102 * of strings (parse delimiters), it store in 'next the next 00103 * meaningfull string, and in current the rest of the secuence, 00104 * the idea is to iterate over 'next' while it is not true; 00105 * you have to store the original pointer to the string stream 00106 * elsewere; also, we assume that the original stream is 00107 * terminated with '\0'; also, it will discaard any sub-string 00108 * that start with #, that is inteded for discard comments */ 00109 void EGioParse (char **next, 00110 char **current, 00111 const char *delim, 00112 const char *coment); 00113 00114 /* ========================================================================= */ 00115 /** @brief this discard all lines starting with comments and stores the next 00116 * leading line in current and the next token in next, we assume that next 00117 * does not contain data, and that current store the remainings of the 00118 * current line */ 00119 void EGioDisCom (char **next, 00120 char **current, 00121 const char *delim, 00122 const char *coment, 00123 char *store, 00124 unsigned int storeSize, 00125 FILE * in); 00126 00127 /* ========================================================================= */ 00128 /** @brief display function for strings 00129 * @param str pointer to a null terminated string of chars. 00130 * @param file pointer to a stream where we write the string. 00131 * @par Description: 00132 * This function just print the string on the file, it won't add a '\n' at the 00133 * end. */ 00134 void EGdisplayString (void *str, 00135 FILE * file); 00136 00137 /* ========================================================================= */ 00138 /** @brief read a line from an input stream. 00139 * @param str where to store the line. 00140 * @param max_len maximum allowed length. 00141 * @param file stream from where we read the input 00142 * @return zero on success, non-zero otherwise, errors are cast when we can not 00143 * read from the given file. 00144 * */ 00145 int EGioReadLine(char*const str,size_t const max_len, FILE*file); 00146 00147 #endif