Mercurial > fem-fenics-eugenio
annotate inst/ufl.m @ 222:6671cb83a2dd
Make use of is_valid_file_id for fid checks
author | Eugenio Gianniti <eugenio.gianniti@mail.polimi.it> |
---|---|
date | Wed, 21 May 2014 17:56:22 +0200 |
parents | 9adf76893ce4 |
children | 8b159045242f |
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 | |
49 fid = fopen (filename, "w"); | |
50 endif | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
51 if (! is_valid_file_id (fid)) |
220 | 52 error (["ufl: could not open file ", filename]); |
53 endif | |
54 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
|
55 if (! is_valid_file_id (fid)) |
220 | 56 error ("ufl: no open file"); |
57 else | |
58 fclose (fid); | |
59 fid = -1; | |
60 endif | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
61 elseif (! is_valid_file_id (fid)) |
220 | 62 error ("ufl: no open file"); |
63 else | |
222
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
64 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
|
65 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
|
66 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
|
67 else |
6671cb83a2dd
Make use of is_valid_file_id for fid checks
Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
parents:
220
diff
changeset
|
68 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
|
69 endif |
220 | 70 endif |
71 | |
72 endfunction |