Mercurial > fem-fenics-eugenio
annotate inst/ufl.m @ 223:8b159045242f
ufl errors out if filename exists when start command is issued
author | Eugenio Gianniti <eugenio.gianniti@mail.polimi.it> |
---|---|
date | Wed, 21 May 2014 18:16:05 +0200 |
parents | 6671cb83a2dd |
children | a108a0bfe201 |
rev | line source |
---|---|
220 | 1 ## Copyright (C) 2014 Eugenio Gianniti <eugenio.gianniti@mail.polimi.it> |
2 ## | |
3 ## This program is free software; you can redistribute it and/or modify it under | |
4 ## the terms of the GNU General Public License as published by the Free Software | |
5 ## Foundation; either version 3 of the License, or (at your option) any later | |
6 ## version. | |
7 ## | |
8 ## This program is distributed in the hope that it will be useful, but WITHOUT | |
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
11 ## details. | |
12 ## | |
13 ## You should have received a copy of the GNU General Public License along with | |
14 ## this program; if not, see <http://www.gnu.org/licenses/>. | |
15 | |
16 ## -*- texinfo -*- | |
17 ## @deftypefn {Function File} ufl [command] line | |
18 ## | |
19 ## Writes the given line to a ufl file. Optional commands are: | |
20 ## @itemize @bullet | |
21 ## @item start: opens an ufl file. It is possible to specify a name for it, | |
22 ## otherwise 'default.ufl' will be used. | |
23 ## @item end: closes an ufl file. | |
24 ## @end itemize | |
25 ## | |
26 ## @end deftypefn | |
27 | |
28 function ufl (varargin) | |
29 | |
30 persistent fid = -1; | |
31 filename = "default.ufl"; | |
32 | |
33 if (numel (varargin) < 1) | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
34 if (! is_valid_file_id (fid)) |
220 | 35 print_usage (); |
36 endif | |
37 elseif (! all (cellfun ("ischar", varargin))) | |
38 error ("ufl: all arguments should be strings"); | |
39 elseif (strcmpi (varargin{1}, "start")) | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
40 if (is_valid_file_id (fid)) |
220 | 41 error ("ufl: a file is already open"); |
42 else | |
43 if (numel (varargin) > 1) | |
44 filename = varargin{2}; | |
45 if (isempty (regexpi (filename, ".ufl$", "match"))) | |
46 filename = [filename, ".ufl"]; | |
47 endif | |
48 endif | |
223
8b159045242f
ufl errors out if filename exists when start command is issued
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
222
diff
changeset
|
49 [~, err, ~] = stat (filename); |
8b159045242f
ufl errors out if filename exists when start command is issued
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
222
diff
changeset
|
50 if (err == 0) |
8b159045242f
ufl errors out if filename exists when start command is issued
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
222
diff
changeset
|
51 error (["ufl: a file named ", filename, " already exists"]); |
8b159045242f
ufl errors out if filename exists when start command is issued
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
222
diff
changeset
|
52 else |
8b159045242f
ufl errors out if filename exists when start command is issued
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
222
diff
changeset
|
53 fid = fopen (filename, "w"); |
8b159045242f
ufl errors out if filename exists when start command is issued
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
222
diff
changeset
|
54 endif |
220 | 55 endif |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
56 if (! is_valid_file_id (fid)) |
220 | 57 error (["ufl: could not open file ", filename]); |
58 endif | |
59 elseif (strcmpi (varargin{1}, "end")) | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
60 if (! is_valid_file_id (fid)) |
220 | 61 error ("ufl: no open file"); |
62 else | |
63 fclose (fid); | |
64 fid = -1; | |
65 endif | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
66 elseif (! is_valid_file_id (fid)) |
220 | 67 error ("ufl: no open file"); |
68 else | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
69 if (is_valid_file_id (fid)) |
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
70 fprintf (fid, "%s ", varargin{:}); |
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
71 fprintf (fid, "\n"); |
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
72 else |
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
73 error ("ufl: no open file"); |
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
74 endif |
220 | 75 endif |
76 | |
77 endfunction |