# HG changeset patch # User Yu Hongbo # Date 1665016703 -28800 # Thu Oct 06 08:38:23 2022 +0800 # Node ID e58f700560f945d4381e943d6db5ae75b89d64bc # Parent b8f4ec18e728cd62199a175ff069757aefde79ce add websave() function diff -r b8f4ec18e728 -r e58f700560f9 scripts/web/module.mk --- a/scripts/web/module.mk Wed Oct 05 12:03:52 2022 -0700 +++ b/scripts/web/module.mk Thu Oct 06 08:38:23 2022 +0800 @@ -5,7 +5,8 @@ %reldir%/web.m \ %reldir%/weboptions.m \ %reldir%/webread.m \ - %reldir%/webwrite.m + %reldir%/webwrite.m \ + %reldir%/websave.m %canon_reldir%dir = $(fcnfiledir)/web diff -r b8f4ec18e728 -r e58f700560f9 scripts/web/websave.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/web/websave.m Thu Oct 06 08:38:23 2022 +0800 @@ -0,0 +1,126 @@ +######################################################################## +## +## Copyright (C) 2018-2022 The Octave Project Developers +## +## See the file COPYRIGHT.md in the top-level directory of this +## distribution or . +## +## This file is part of Octave. +## +## Octave is free software: you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## (at your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## . +## +######################################################################## + +## -*- texinfo -*- +## @deftypefn {} {@var{outfilename} =} websave (@var{filename}, @var{url}) +## @deftypefnx {} {@var{outfilename} =} websave (@var{filename}, @var{url}, @var{key_1}, @var{value_1}, @dots{}) +## @deftypefnx {} {@var{outfilename} =} websave (@dots{}, @var{options}) +## +## Save content from RESTful web service to a local file. +## +## Specify a string output filename, save content from the web service +## specified by @var{url} and the output filename is as @var{outfilename}. +## +## The @var{outfilename} contains @var{filename} and file extension, so +## @var{outfilename} and @var{filename} may be different. +## +## @seealso{weboptions, webwrite} +## @end deftypefn + +function filename = websave (filename, url, varargin) + + if (nargin == 0) + print_usage (); + endif + + if (nargin < 2) + error ("websave: must provide FILENAME and URL"); + endif + + if (! (ischar (filename) && isrow (filename))) + error ("websave: FILENAME must be a string"); + endif + + if (! (ischar (url) && isrow (url))) + error ("websave: URL must be a string"); + endif + + if (nargin == 2) + response = webread (url); + else + response = webread (url, varargin{:}); + endif + + if (nargin > 2 && isa (varargin{end}, "weboptions")) + has_weboptions = true; + options = varargin{end}; + varargin(end) = []; + else + has_weboptions = false; + options = weboptions (); + endif + + if (isempty (strfind (url, '?'))) + [~, ~, file_extension] = fileparts (filename); + [~, ~, url_extension] = fileparts (url); + if (strcmp (options.ContentType, 'html')) + pattern_cell = {'.htm', '.html'}; + file_extension_index = strncmpi ( + file_extension, pattern_cell, length (file_extension)); + if (any (file_extension_index) || isempty (file_extension)) + file_extension_index = strncmp ( + url_extension, pattern_cell, length (url_extension)); + if (any (file_extension_index) && isscalar (find (file_extension_index))) + url_extension = [pattern_cell{file_extension_index}]; + else + url_extension = '.html'; + endif + endif + elseif (! isempty (url_extension) && isempty (file_extension)) + filename = [filename url_extension]; + endif + endif + + if (exist(filename, 'file')) + is_new_file = false; + else + is_new_file = true; + endif + + try + fp = fopen(filename, 'w'); + if (fp == -1) + error (sprintf ("websave: cannot open file %s", filename)); + else + fprintf (fp, response); + endif + fclose(fp); + catch + if (is_new_file && exist(filename, 'file')) + delete(filename) + endif + end_try_catch + +endfunction + + +## Test input validation +%!error websave () +%!error websave (1) +%!error websave (["a";"b"]) +%!error websave (1, 'test.html') +%!error websave (["a";"b"], 'test.html') +%!error websave ('test.html', 1) +%!error websave ('test.html', ["a";"b"])