1 /* open_datafile.c -- Set up datafile context. 2 3 Usage: Before including this template file in your source file, 4 #define the prototype of the function under test in the CALL 5 symbol, see tadd_tmpl.c for an example. 6 7 Copyright (C) 2012, 2013 INRIA 8 9 This file is part of GNU MPC. 10 11 GNU MPC is free software; you can redistribute it and/or modify it under 12 the terms of the GNU Lesser General Public License as published by the 13 Free Software Foundation; either version 3 of the License, or (at your 14 option) any later version. 15 16 GNU MPC is distributed in the hope that it will be useful, but WITHOUT ANY 17 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 18 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for 19 more details. 20 21 You should have received a copy of the GNU Lesser General Public License 22 along with this program. If not, see http://www.gnu.org/licenses/ . 23 */ 24 25 #include <string.h> 26 #include "mpc-tests.h" 27 28 void 29 open_datafile (mpc_datafile_context_t* datafile_context, 30 const char * data_filename) 31 { 32 char *src_dir; 33 char default_srcdir[] = "."; 34 35 src_dir = getenv ("srcdir"); 36 if (src_dir == NULL) 37 src_dir = default_srcdir; 38 39 datafile_context->pathname = 40 (char *) malloc ((strlen (src_dir)) + strlen (data_filename) + 2); 41 if (datafile_context->pathname == NULL) 42 { 43 fprintf (stderr, "Cannot allocate memory\n"); 44 exit (1); 45 } 46 sprintf (datafile_context->pathname, "%s/%s", src_dir, data_filename); 47 datafile_context->fd = fopen (datafile_context->pathname, "r"); 48 if (datafile_context->fd == NULL) 49 { 50 fprintf (stderr, "Unable to open %s\n", datafile_context->pathname); 51 exit (1); 52 } 53 54 datafile_context->line_number = 1; 55 datafile_context->nextchar = getc (datafile_context->fd); 56 tpl_skip_whitespace_comments (datafile_context); 57 } 58