ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
user_string.m
Go to the documentation of this file.
1function [string, file_name] = user_string(string_name, string)
2%USER_STRING Get/set a user specific string
3%
4% Examples:
5% string = user_string(string_name)
6% isSaved = user_string(string_name, new_string)
7% [value, file_name] = user_string(...)
8%
9% Function to get and set a string in a system or user specific file. This
10% enables, for example, system specific paths to binaries to be saved.
11%
12% The specified string will be saved in a file named <string_name>.txt,
13% either in a subfolder named .ignore under this file's folder, or in the
14% user's prefdir folder (in case this file's folder is non-writable).
15%
16% IN:
17% string_name - String containing the name of the string required, which
18% sets the filename storing the string: <string_name>.txt
19% new_string - The new string to be saved in the <string_name>.txt file
20%
21% OUT:
22% string - The currently saved string. Default: ''
23% isSaved - Boolean indicating whether the save was succesful
24% file_name - path of the .txt file used to contain the requested string
25
26% Copyright (C) Oliver Woodford 2011-2014, Yair Altman 2015-
27
28% This method of saving paths avoids changing .m files which might be in a
29% version control system. Instead it saves the user dependent paths in
30% separate files with a .txt extension, which need not be checked in to
31% the version control system. Thank you to Jonas Dorn for suggesting this
32% approach.
33
34% 10/01/2013 - Access files in text, not binary mode, as latter can cause
35% errors. Thanks to Christian for pointing this out.
36% 29/05/2015 - Save file in prefdir if current folder is non-writable (issue #74)
37% 09/01/2018 - Fix issue #232: if the string looks like a file/folder path, ensure it actually exists
38% 15/01/2020 - Added file_name output argument
39
40 if ~ischar(string_name)
41 error('string_name must be a string.');
42 end
43 % Create the full filename
44 fname = [string_name '.txt'];
45 dname = fullfile(fileparts(mfilename('fullpath')), '.ignore');
46 file_name = fullfile(dname, fname);
47 default_file_name = file_name;
48 if nargin > 1
49 % Set string
50 if ~ischar(string)
51 error('new_string must be a string.');
52 end
53 % Make sure the save directory exists
54 %dname = fileparts(file_name);
55 if ~exist(dname, 'dir')
56 % Create the directory
57 try
58 if ~mkdir(dname)
59 string = false;
60 return
61 end
62 catch
63 string = false;
64 return
65 end
66 % Make it hidden
67 try
68 fileattrib(dname, '+h');
69 catch
70 end
71 end
72 % Write the file
73 fid = fopen(file_name, 'wt');
74 if fid == -1
75 % file cannot be created/updated - use prefdir if file does not already exist
76 % (if file exists but is simply not writable, don't create a duplicate in prefdir)
77 if ~exist(file_name,'file')
78 file_name = fullfile(prefdir, fname);
79 fid = fopen(file_name, 'wt');
80 end
81 if fid == -1
82 string = false;
83 file_name = default_file_name;
84 return
85 end
86 end
87 try
88 fprintf(fid, '%s', string);
89 catch
90 fclose(fid);
91 string = false;
92 return
93 end
94 fclose(fid);
95 string = true;
96 else
97 % Get string
98 fid = fopen(file_name, 'rt');
99 if fid == -1
100 % file cannot be read, try to read the file in prefdir
101 file_name = fullfile(prefdir, fname);
102 fid = fopen(file_name, 'rt');
103 if fid == -1
104 string = '';
105 file_name = default_file_name;
106 return
107 end
108 end
109 string = fgetl(fid);
110 fclose(fid);
111
112 % Fix issue #232: if the string looks like a file/folder path, ensure it actually exists
113 if ~isempty(string) && any(string=='\' | string=='/') && ~exist(string) %#ok<EXIST>
114 string = '';
115 end
116 end
117end
function user_string(in string_name, in string)