diff scripts/path/savepath.m @ 7392:17f2cdb5232e

[project @ 2008-01-17 08:14:32 by jwe]
author jwe
date Thu, 17 Jan 2008 08:14:32 +0000
parents f071480b7eac
children a2e8cfe2fd17
line wrap: on
line diff
--- a/scripts/path/savepath.m
+++ b/scripts/path/savepath.m
@@ -50,27 +50,31 @@
     if (fid < 0)
       error ("savepath: could not open savefile, %s: %s", savefile, msg);
     endif
-    linenum = 0;
-    while (linenum >= 0)
-      result = fgetl (fid);
-      if (isnumeric (result))
-        ## end at the end of file
-        linenum = -1;
-      else
-        linenum = linenum + 1;
-        filelines{linenum} = result;
-        ## find the first and last lines if they exist in the file
-        if (strcmp (result, beginstring))
-          startline = linenum;
-        elseif (strcmp (result, endstring))
-          endline = linenum;
-        endif
+    unwind_protect
+      linenum = 0;
+      while (linenum >= 0)
+	result = fgetl (fid);
+	if (isnumeric (result))
+	  ## end at the end of file
+	  linenum = -1;
+	else
+	  linenum = linenum + 1;
+	  filelines{linenum} = result;
+	  ## find the first and last lines if they exist in the file
+	  if (strcmp (result, beginstring))
+	    startline = linenum;
+	  elseif (strcmp (result, endstring))
+	    endline = linenum;
+	  endif
+	endif
+      endwhile
+    unwind_protect_cleanup
+      closeread = fclose (fid);
+      if (closeread < 0)
+	error ("savepath: could not close savefile after reading, %s",
+	       savefile);
       endif
-    endwhile
-    closeread = fclose (fid);
-    if (closeread < 0)
-      error ("savepath: could not close savefile after reading, %s", savefile);
-    endif
+    end_unwind_protect
   endif
 
   if (startline > endline || (startline > 0 && endline == 0))
@@ -103,24 +107,27 @@
   if (fid < 0)
     error ("savepath: unable to open file for writing, %s, %s", savefile, msg);
   endif
-  for i = 1:length (pre)
-    fprintf (fid, "%s\n", pre{i})
-  endfor
+  unwind_protect
+    for i = 1:length (pre)
+      fprintf (fid, "%s\n", pre{i})
+    endfor
 
-  ## Use single quotes for PATH argument to avoid string escape
-  ## processing.
-  fprintf (fid, "%s\n  path ('%s');\n%s\n",
-	   beginstring, path (), endstring);
+    ## Use single quotes for PATH argument to avoid string escape
+    ## processing.
+    fprintf (fid, "%s\n  path ('%s');\n%s\n",
+	     beginstring, path (), endstring);
 
-  for i = 1:length (post)
-    fprintf (fid, "%s\n", post{i});
-  endfor
-  closeread = fclose (fid);
-  if (closeread < 0)
-    error ("savepath: could not close savefile after writing, %s", savefile);
-  elseif (nargin == 0)
-    warning ("savepath: current path saved to %s",savefile)
-  endif
+    for i = 1:length (post)
+      fprintf (fid, "%s\n", post{i});
+    endfor
+  unwind_protect_cleanup
+    closeread = fclose (fid);
+    if (closeread < 0)
+      error ("savepath: could not close savefile after writing, %s", savefile);
+    elseif (nargin == 0)
+      warning ("savepath: current path saved to %s", savefile);
+    endif
+  end_unwind_protect
 
   retval = 0;