You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
7.0 KiB

  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, 2011, 2013 Andrew Makhorin, Department for Applied
  7. * Informatics, Moscow Aviation Institute, Moscow, Russia. All rights
  8. * reserved. 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. #include "env.h"
  24. #include "glpsdf.h"
  25. #include "misc.h"
  26. struct glp_data
  27. { /* plain data file */
  28. char *fname;
  29. /* name of data file */
  30. glp_file *fp;
  31. /* stream assigned to data file */
  32. void *jump; /* jmp_buf jump; */
  33. /* label for go to in case of error */
  34. int count;
  35. /* line count */
  36. int c;
  37. /* current character of EOF */
  38. char item[255+1];
  39. /* current data item */
  40. };
  41. static void next_char(glp_data *data);
  42. glp_data *glp_sdf_open_file(const char *fname)
  43. { /* open plain data file */
  44. glp_data *data = NULL;
  45. glp_file *fp;
  46. jmp_buf jump;
  47. fp = glp_open(fname, "r");
  48. if (fp == NULL)
  49. { xprintf("Unable to open '%s' - %s\n", fname, get_err_msg());
  50. goto done;
  51. }
  52. data = xmalloc(sizeof(glp_data));
  53. data->fname = xmalloc(strlen(fname)+1);
  54. strcpy(data->fname, fname);
  55. data->fp = fp;
  56. data->jump = NULL;
  57. data->count = 0;
  58. data->c = '\n';
  59. data->item[0] = '\0';
  60. /* read the very first character */
  61. if (setjmp(jump))
  62. { glp_sdf_close_file(data);
  63. data = NULL;
  64. goto done;
  65. }
  66. data->jump = jump;
  67. next_char(data);
  68. data->jump = NULL;
  69. done: return data;
  70. }
  71. void glp_sdf_set_jump(glp_data *data, void *jump)
  72. { /* set up error handling */
  73. data->jump = jump;
  74. return;
  75. }
  76. void glp_sdf_error(glp_data *data, const char *fmt, ...)
  77. { /* print error message */
  78. va_list arg;
  79. xprintf("%s:%d: ", data->fname, data->count);
  80. va_start(arg, fmt);
  81. xvprintf(fmt, arg);
  82. va_end(arg);
  83. if (data->jump == NULL)
  84. xerror("");
  85. else
  86. longjmp(data->jump, 1);
  87. /* no return */
  88. }
  89. void glp_sdf_warning(glp_data *data, const char *fmt, ...)
  90. { /* print warning message */
  91. va_list arg;
  92. xprintf("%s:%d: warning: ", data->fname, data->count);
  93. va_start(arg, fmt);
  94. xvprintf(fmt, arg);
  95. va_end(arg);
  96. return;
  97. }
  98. static void next_char(glp_data *data)
  99. { /* read next character */
  100. int c;
  101. if (data->c == EOF)
  102. glp_sdf_error(data, "unexpected end of file\n");
  103. else if (data->c == '\n')
  104. data->count++;
  105. c = glp_getc(data->fp);
  106. if (c < 0)
  107. { if (glp_ioerr(data->fp))
  108. glp_sdf_error(data, "read error - %s\n", get_err_msg());
  109. else if (data->c == '\n')
  110. c = EOF;
  111. else
  112. { glp_sdf_warning(data, "missing final end of line\n");
  113. c = '\n';
  114. }
  115. }
  116. else if (c == '\n')
  117. ;
  118. else if (isspace(c))
  119. c = ' ';
  120. else if (iscntrl(c))
  121. glp_sdf_error(data, "invalid control character 0x%02X\n", c);
  122. data->c = c;
  123. return;
  124. }
  125. static void skip_pad(glp_data *data)
  126. { /* skip uninteresting characters and comments */
  127. loop: while (data->c == ' ' || data->c == '\n')
  128. next_char(data);
  129. if (data->c == '/')
  130. { next_char(data);
  131. if (data->c != '*')
  132. glp_sdf_error(data, "invalid use of slash\n");
  133. next_char(data);
  134. for (;;)
  135. { if (data->c == '*')
  136. { next_char(data);
  137. if (data->c == '/')
  138. { next_char(data);
  139. break;
  140. }
  141. }
  142. next_char(data);
  143. }
  144. goto loop;
  145. }
  146. return;
  147. }
  148. static void next_item(glp_data *data)
  149. { /* read next item */
  150. int len;
  151. skip_pad(data);
  152. len = 0;
  153. while (!(data->c == ' ' || data->c == '\n'))
  154. { data->item[len++] = (char)data->c;
  155. if (len == sizeof(data->item))
  156. glp_sdf_error(data, "data item '%.31s...' too long\n",
  157. data->item);
  158. next_char(data);
  159. }
  160. data->item[len] = '\0';
  161. return;
  162. }
  163. int glp_sdf_read_int(glp_data *data)
  164. { /* read integer number */
  165. int x;
  166. next_item(data);
  167. switch (str2int(data->item, &x))
  168. { case 0:
  169. break;
  170. case 1:
  171. glp_sdf_error(data, "integer '%s' out of range\n",
  172. data->item);
  173. case 2:
  174. glp_sdf_error(data, "cannot convert '%s' to integer\n",
  175. data->item);
  176. default:
  177. xassert(data != data);
  178. }
  179. return x;
  180. }
  181. double glp_sdf_read_num(glp_data *data)
  182. { /* read floating-point number */
  183. double x;
  184. next_item(data);
  185. switch (str2num(data->item, &x))
  186. { case 0:
  187. break;
  188. case 1:
  189. glp_sdf_error(data, "number '%s' out of range\n",
  190. data->item);
  191. case 2:
  192. glp_sdf_error(data, "cannot convert '%s' to number\n",
  193. data->item);
  194. default:
  195. xassert(data != data);
  196. }
  197. return x;
  198. }
  199. const char *glp_sdf_read_item(glp_data *data)
  200. { /* read data item */
  201. next_item(data);
  202. return data->item;
  203. }
  204. const char *glp_sdf_read_text(glp_data *data)
  205. { /* read text until end of line */
  206. int c, len = 0;
  207. for (;;)
  208. { c = data->c;
  209. next_char(data);
  210. if (c == ' ')
  211. { /* ignore initial spaces */
  212. if (len == 0) continue;
  213. /* and multiple ones */
  214. if (data->item[len-1] == ' ') continue;
  215. }
  216. else if (c == '\n')
  217. { /* remove trailing space */
  218. if (len > 0 && data->item[len-1] == ' ') len--;
  219. /* and stop reading */
  220. break;
  221. }
  222. /* add current character to the buffer */
  223. data->item[len++] = (char)c;
  224. if (len == sizeof(data->item))
  225. glp_sdf_error(data, "line too long\n", data->item);
  226. }
  227. data->item[len] = '\0';
  228. return data->item;
  229. }
  230. int glp_sdf_line(glp_data *data)
  231. { /* determine current line number */
  232. return data->count;
  233. }
  234. void glp_sdf_close_file(glp_data *data)
  235. { /* close plain data file */
  236. glp_close(data->fp);
  237. xfree(data->fname);
  238. xfree(data);
  239. return;
  240. }
  241. /* eof */