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