Scippy

SCIP

Solving Constraint Integer Programs

readargs.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file readargs.c
17  * @brief read comand line arguments
18  * @author Marc Pfetsch
19  */
20 
21 #include "readargs.h"
22 
23 
24 /** get problem name
25  *
26  * Returns 0 if maxsize is not sufficient and 1 otherwise.
27  */
29  const char* filename, /**< file name */
30  char* probname, /**< name of problem (output) */
31  int maxsize /**< max length of problem name */
32  )
33 {
34  int result = 1;
35  int i = 0;
36  int l;
37  int j;
38 
39  /* first find end of string */
40  while ( filename[i] != 0)
41  ++i;
42  l = i; /* end of string */
43 
44  /* if we found ".gz" */
45  if ( l > 3 && filename[l-3] == '.' && filename[l-2] == 'g' && filename[l-1] == 'z' )
46  {
47  l -= 4;
48  i = l;
49  }
50 
51  /* go back until '.' or '/' appears */
52  while (i > 0 && filename[i] != '.' && filename[i] != '/')
53  --i;
54  assert( i > 0 );
55 
56  /* if we found '.', search for '/' */
57  if ( filename[i] == '.' )
58  {
59  l = i;
60  while ( i > 0 && filename[i] != '/' )
61  --i;
62  }
63 
64  /* correct counter */
65  if ( filename[i] == '/' )
66  ++i;
67 
68  /* copy name */
69  j = 0;
70  while ( i < l && filename[i] != 0 )
71  {
72  probname[j++] = filename[i++];
73  if ( j >= maxsize-1 )
74  {
75  result = 0;
76  break;
77  }
78  }
79  probname[j] = 0;
80 
81  return result;
82 }
83 
84 
85 /** read comand line arguments */
87  int argc, /**< number of shell parameters */
88  char** argv, /**< array with shell parameters */
89  const char** filename, /**< file name from arguments */
90  const char** settingsname, /**< name of settings file */
91  SCIP_Real* timelimit, /**< time limit read from arguments */
92  SCIP_Real* memlimit, /**< memory limit read from arguments */
93  SCIP_Longint* nodelimit, /**< node limit read from arguments */
94  int* dispfreq /**< display frequency */
95  )
96 {
97  int i;
98  char usage[SCIP_MAXSTRLEN];
99 #ifndef NDEBUG
100  int status;
101 #endif
102 
103  assert( argc > 0 );
104  assert( argv != NULL );
105  assert( filename != NULL );
106  assert( settingsname != NULL );
107  assert( timelimit != NULL );
108  assert( memlimit != NULL );
109  assert( nodelimit != NULL );
110  assert( dispfreq != NULL );
111 
112  /* init usage text */
113 #ifndef NDEBUG
114  status = SCIPsnprintf(usage, SCIP_MAXSTRLEN, "usage: %s <file> [-s <setting file>] [-t <time limit>] [-m <mem limit>] [-n <node limit>] [-d <display frequency>]", argv[0]);
115  assert( 0 <= status && status < SCIP_MAXSTRLEN );
116 #else
117  (void) SCIPsnprintf(usage, SCIP_MAXSTRLEN, "usage: %s <file> [-s <setting file>] [-t <time limit>] [-m <mem limit>] [-n <node limit>] [-d <display frequency>]", argv[0]);
118 #endif
119 
120  /* init arguments */
121  *timelimit = 1e20;
122  *memlimit = 1e20;
123  *nodelimit = SCIP_LONGINT_MAX;
124  *filename = NULL;
125  *settingsname = NULL;
126  *dispfreq = -1;
127 
128  /* check all arguments */
129  for (i = 1; i < argc; ++i)
130  {
131  /* check for settings */
132  if ( strcmp(argv[i], "-s") == 0 )
133  {
134  if ( *settingsname != NULL )
135  {
136  (void) fprintf(stderr, "Error: Setting name already supplied.\n");
137  (void) fprintf(stderr, "%s\n", usage);
138  return SCIP_INVALIDDATA;
139  }
140  if ( i == argc-1 )
141  {
142  fprintf(stderr, "Error: No setting file name supplied.\n");
143  fprintf(stderr, "%s\n", usage);
144  return SCIP_INVALIDDATA;
145  }
146  ++i;
147  *settingsname = argv[i];
148  assert( i < argc );
149  }
150  else
151  {
152  /* check for time limit */
153  if ( strcmp(argv[i], "-t") == 0 )
154  {
155  if ( i == argc-1 )
156  {
157  fprintf(stderr, "Erro: No time limit supplied.\n");
158  fprintf(stderr, "%s\n", usage);
159  return SCIP_INVALIDDATA;
160  }
161  ++i;
162  *timelimit = atof(argv[i]);
163  assert( i < argc );
164  }
165  else
166  {
167  /* check for memory limit */
168  if ( strcmp(argv[i], "-m") == 0 )
169  {
170  if ( i == argc-1 )
171  {
172  fprintf(stderr, "Error: No memory limit supplied.\n");
173  fprintf(stderr, "%s\n", usage);
174  return SCIP_INVALIDDATA;
175  }
176  ++i;
177  *memlimit = atof(argv[i]);
178  assert( i < argc );
179  }
180  else
181  {
182  /* check for memory limit */
183  if ( strcmp(argv[i], "-n") == 0 )
184  {
185  if ( i == argc-1 )
186  {
187  fprintf(stderr, "Error: No node limit supplied.\n");
188  fprintf(stderr, "%s\n", usage);
189  return SCIP_INVALIDDATA;
190  }
191  ++i;
192  *nodelimit = atol(argv[i]);
193  assert( i < argc );
194  }
195  else
196  {
197  /* check for display frequency */
198  if ( strcmp(argv[i], "-d") == 0 )
199  {
200  if ( i == argc-1 )
201  {
202  fprintf(stderr, "Error: No display frequency supplied.\n");
203  fprintf(stderr, "%s\n", usage);
204  return SCIP_INVALIDDATA;
205  }
206  ++i;
207  *dispfreq = atoi(argv[i]);
208  assert( i < argc );
209  }
210  else
211  {
212  /* if filename is already specified */
213  if ( *filename != NULL )
214  {
215  fprintf(stderr, "Error: file name already specified.\n");
216  fprintf(stderr, "%s\n", usage);
217  return SCIP_ERROR;
218  }
219  *filename = argv[i];
220  }
221  }
222  }
223  }
224  }
225  }
226 
227  if ( *filename == NULL )
228  {
229  fprintf(stderr, "Error: No filename supplied.\n");
230  fprintf(stderr, "%s\n", usage);
231  return SCIP_INVALIDDATA;
232  }
233 
234  return SCIP_OKAY;
235 }
#define NULL
Definition: def.h:253
#define SCIP_MAXSTRLEN
Definition: def.h:274
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define SCIP_LONGINT_MAX
Definition: def.h:150
SCIP_RETCODE readArguments(int argc, char **argv, const char **filename, const char **settingsname, SCIP_Real *timelimit, SCIP_Real *memlimit, SCIP_Longint *nodelimit, int *dispfreq)
Definition: readargs.c:86
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10263
#define SCIP_Real
Definition: def.h:164
#define SCIP_Longint
Definition: def.h:149
read comand line arguments
int getProblemName(const char *filename, char *probname, int maxsize)
Definition: readargs.c:28