glpsdf.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* glpsdf.c (plain data file reading routines) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #define GLPSDF_H
  24. #define GLP_DATA_DEFINED
  25. typedef struct glp_data glp_data;
  26. #include "glpapi.h"
  27. struct glp_data
  28. { /* plain data file */
  29. char *fname;
  30. /* name of data file */
  31. XFILE *fp;
  32. /* stream assigned to data file */
  33. void *jump; /* jmp_buf jump; */
  34. /* label for go to in case of error */
  35. int count;
  36. /* line count */
  37. int c;
  38. /* current character of XEOF */
  39. char item[255+1];
  40. /* current data item */
  41. };
  42. static void next_char(glp_data *data);
  43. glp_data *glp_sdf_open_file(const char *fname)
  44. { /* open plain data file */
  45. glp_data *data = NULL;
  46. XFILE *fp;
  47. jmp_buf jump;
  48. fp = xfopen(fname, "r");
  49. if (fp == NULL)
  50. { xprintf("Unable to open `%s' - %s\n", fname, xerrmsg());
  51. goto done;
  52. }
  53. data = xmalloc(sizeof(glp_data));
  54. data->fname = xmalloc(strlen(fname)+1);
  55. strcpy(data->fname, fname);
  56. data->fp = fp;
  57. data->jump = NULL;
  58. data->count = 0;
  59. data->c = '\n';
  60. data->item[0] = '\0';
  61. /* read the very first character */
  62. if (setjmp(jump))
  63. { glp_sdf_close_file(data);
  64. data = NULL;
  65. goto done;
  66. }
  67. data->jump = jump;
  68. next_char(data);
  69. data->jump = NULL;
  70. done: return data;
  71. }
  72. void glp_sdf_set_jump(glp_data *data, void *jump)
  73. { /* set up error handling */
  74. data->jump = jump;
  75. return;
  76. }
  77. void glp_sdf_error(glp_data *data, const char *fmt, ...)
  78. { /* print error message */
  79. va_list arg;
  80. xprintf("%s:%d: ", data->fname, data->count);
  81. va_start(arg, fmt);
  82. xvprintf(fmt, arg);
  83. va_end(arg);
  84. if (data->jump == NULL)
  85. xerror("");
  86. else
  87. longjmp(data->jump, 1);
  88. /* no return */
  89. }
  90. void glp_sdf_warning(glp_data *data, const char *fmt, ...)
  91. { /* print warning message */
  92. va_list arg;
  93. xprintf("%s:%d: warning: ", data->fname, data->count);
  94. va_start(arg, fmt);
  95. xvprintf(fmt, arg);
  96. va_end(arg);
  97. return;
  98. }
  99. static void next_char(glp_data *data)
  100. { /* read next character */
  101. int c;
  102. if (data->c == XEOF)
  103. glp_sdf_error(data, "unexpected end of file\n");
  104. else if (data->c == '\n')
  105. data->count++;
  106. c = xfgetc(data->fp);
  107. if (c < 0)
  108. { if (xferror(data->fp))
  109. glp_sdf_error(data, "read error - %s\n", xerrmsg());
  110. else if (data->c == '\n')
  111. c = XEOF;
  112. else
  113. { glp_sdf_warning(data, "missing final end of line\n");
  114. c = '\n';
  115. }
  116. }
  117. else if (c == '\n')
  118. ;
  119. else if (isspace(c))
  120. c = ' ';
  121. else if (iscntrl(c))
  122. glp_sdf_error(data, "invalid control character 0x%02X\n", c);
  123. data->c = c;
  124. return;
  125. }
  126. static void skip_pad(glp_data *data)
  127. { /* skip uninteresting characters and comments */
  128. loop: while (data->c == ' ' || data->c == '\n')
  129. next_char(data);
  130. if (data->c == '/')
  131. { next_char(data);
  132. if (data->c != '*')
  133. glp_sdf_error(data, "invalid use of slash\n");
  134. next_char(data);
  135. for (;;)
  136. { if (data->c == '*')
  137. { next_char(data);
  138. if (data->c == '/')
  139. { next_char(data);
  140. break;
  141. }
  142. }
  143. next_char(data);
  144. }
  145. goto loop;
  146. }
  147. return;
  148. }
  149. static void next_item(glp_data *data)
  150. { /* read next item */
  151. int len;
  152. skip_pad(data);
  153. len = 0;
  154. while (!(data->c == ' ' || data->c == '\n'))
  155. { data->item[len++] = (char)data->c;
  156. if (len == sizeof(data->item))
  157. glp_sdf_error(data, "data item `%.31s...' too long\n",
  158. data->item);
  159. next_char(data);
  160. }
  161. data->item[len] = '\0';
  162. return;
  163. }
  164. int glp_sdf_read_int(glp_data *data)
  165. { /* read integer number */
  166. int x;
  167. next_item(data);
  168. switch (str2int(data->item, &x))
  169. { case 0:
  170. break;
  171. case 1:
  172. glp_sdf_error(data, "integer `%s' out of range\n",
  173. data->item);
  174. case 2:
  175. glp_sdf_error(data, "cannot convert `%s' to integer\n",
  176. data->item);
  177. default:
  178. xassert(data != data);
  179. }
  180. return x;
  181. }
  182. double glp_sdf_read_num(glp_data *data)
  183. { /* read floating-point number */
  184. double x;
  185. next_item(data);
  186. switch (str2num(data->item, &x))
  187. { case 0:
  188. break;
  189. case 1:
  190. glp_sdf_error(data, "number `%s' out of range\n",
  191. data->item);
  192. case 2:
  193. glp_sdf_error(data, "cannot convert `%s' to number\n",
  194. data->item);
  195. default:
  196. xassert(data != data);
  197. }
  198. return x;
  199. }
  200. const char *glp_sdf_read_item(glp_data *data)
  201. { /* read data item */
  202. next_item(data);
  203. return data->item;
  204. }
  205. const char *glp_sdf_read_text(glp_data *data)
  206. { /* read text until end of line */
  207. int c, len = 0;
  208. for (;;)
  209. { c = data->c;
  210. next_char(data);
  211. if (c == ' ')
  212. { /* ignore initial spaces */
  213. if (len == 0) continue;
  214. /* and multiple ones */
  215. if (data->item[len-1] == ' ') continue;
  216. }
  217. else if (c == '\n')
  218. { /* remove trailing space */
  219. if (len > 0 && data->item[len-1] == ' ') len--;
  220. /* and stop reading */
  221. break;
  222. }
  223. /* add current character to the buffer */
  224. data->item[len++] = (char)c;
  225. if (len == sizeof(data->item))
  226. glp_sdf_error(data, "line too long\n", data->item);
  227. }
  228. data->item[len] = '\0';
  229. return data->item;
  230. }
  231. int glp_sdf_line(glp_data *data)
  232. { /* determine current line number */
  233. return data->count;
  234. }
  235. void glp_sdf_close_file(glp_data *data)
  236. { /* close plain data file */
  237. xfclose(data->fp);
  238. xfree(data->fname);
  239. xfree(data);
  240. return;
  241. }
  242. /* eof */