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