time | Calls | line |
---|
| | 1 | function pj = name( pj )
|
| | 2 | %NAME Method to check or create valid filename.
|
| | 3 | % Validate FileName in PrintJob object. If empty, no name passed to PRINT
|
| | 4 | % command, but one is required by the driver, image file formats, we invent
|
| | 5 | % a name and tell the user what it is. Also invent name, but do not tell
|
| | 6 | % user, for temporary PS file created on disk when printing directly
|
| | 7 | % to output device or for GhostScript conversion.
|
| | 8 |
|
| | 9 | % Copyright 1984-2016 The MathWorks, Inc.
|
| | 10 |
|
| | 11 | %Generate a name we would use if had to in various circumstances.
|
| | 12 |
|
< 0.001 | 12 | 13 | tellUserFilename = 0;
|
| | 14 |
|
< 0.001 | 12 | 15 | if isempty(pj.FileName)
|
| | 16 |
|
| | 17 | objName = ['figure' int2str( double(pj.Handles{1}(1) ))];
|
| | 18 |
|
| | 19 | if pj.DriverExport && ...
|
| | 20 | ~strcmp(pj.DriverClass,'MW') && ~pj.DriverClipboard && ~pj.RGBImage
|
| | 21 | %These kinds of files shouldn't go to printer, generate file on disk
|
| | 22 | pj.FileName = objName;
|
| | 23 | tellUserFilename = 1;
|
| | 24 |
|
| | 25 | else
|
| | 26 | %File is going to be sent to an output device by OS or us.
|
| | 27 | if ~strcmp(pj.DriverClass,'MW') && ~pj.DriverClipboard && ~pj.RGBImage
|
| | 28 | %Going to a file, invent a name
|
| | 29 | pj.FileName = tempname;
|
| | 30 | pj.PrintOutput = 1;
|
| | 31 | end
|
| | 32 | end
|
| | 33 |
|
| | 34 | % only support going to clipboard w/o specifying -clipboard option
|
| | 35 | % for the grandfathered cases on Windows
|
| | 36 | if ~pj.ClipboardOption && pj.DriverClipboard
|
| | 37 | if ~ispc || (ispc && ~any(strcmp(pj.Driver, {'meta', 'bitmap'})))
|
| | 38 | pj.FileName = objName;
|
| | 39 | tellUserFilename = 1;
|
| | 40 | end
|
| | 41 | end
|
< 0.001 | 12 | 42 | else
|
| | 43 | %Both / and \ are commonly used, but in MATLAB we recognize only filesep
|
0.001 | 12 | 44 | pj.FileName = strrep( pj.FileName, '/', filesep );
|
< 0.001 | 12 | 45 | pj.FileName = strrep( pj.FileName, '\', filesep );
|
< 0.001 | 12 | 46 | end
|
| | 47 |
|
| | 48 | %Append appropriate extension to filename if
|
| | 49 | % 1) it doesn't have one, and
|
| | 50 | % 2) we've determined a good one
|
< 0.001 | 12 | 51 | if ~isempty( pj.DriverExt ) && ~isempty( pj.FileName )
|
| | 52 | %Could assert that ~isempty( pj.FileName )
|
0.002 | 12 | 53 | [p,n,e] = fileparts( pj.FileName );
|
< 0.001 | 12 | 54 | if isempty( e )
|
| | 55 | pj.FileName = fullfile( p, [n '.' pj.DriverExt] );
|
< 0.001 | 12 | 56 | end
|
< 0.001 | 12 | 57 | end
|
| | 58 |
|
| | 59 | % on unix fix the file spec if it starts with '~/' so we look at the user's home dir
|
< 0.001 | 12 | 60 | if (isunix)
|
0.002 | 12 | 61 | pj.FileName = fixTilde(pj.FileName);
|
< 0.001 | 12 | 62 | end
|
| | 63 |
|
< 0.001 | 12 | 64 | if tellUserFilename
|
| | 65 | %Invented name above because of device or bad filename
|
| | 66 | if tellUserFilename == 1
|
| | 67 | errStr1 = sprintf( 'Files produced by the ''%s'' driver cannot be sent to printer.\n', pj.Driver);
|
| | 68 | else
|
| | 69 | errStr1 = '';
|
| | 70 | end
|
| | 71 |
|
| | 72 | warning(message('MATLAB:print:SavingToDifferentName', errStr1, pj.FileName))
|
< 0.001 | 12 | 73 | end
|
| | 74 |
|
| | 75 | % Check that we can write to the output file
|
< 0.001 | 12 | 76 | if ~isempty(pj.FileName)
|
< 0.001 | 12 | 77 | [fpath,fname,ext] = fileparts(pj.FileName);
|
< 0.001 | 12 | 78 | if isempty(fpath)
|
| | 79 | fpath = '.';
|
< 0.001 | 12 | 80 | end
|
0.002 | 12 | 81 | pj.FileName = fullfile(fpath,[fname ext]);
|
| | 82 | % first check if readable file already exists there
|
0.004 | 12 | 83 | fidread = fopen(pj.FileName,'r');
|
< 0.001 | 12 | 84 | didnotexist = (fidread == -1);
|
< 0.001 | 12 | 85 | if ~didnotexist
|
< 0.001 | 12 | 86 | fclose(fidread);
|
< 0.001 | 12 | 87 | end
|
| | 88 | % now check if file is appendable (will create file if not there)
|
< 0.001 | 12 | 89 | fidappend = fopen(pj.FileName,'a');
|
< 0.001 | 12 | 90 | if fidappend ~= -1
|
0.005 | 12 | 91 | fclose(fidappend);
|
| | 92 | % check if we have to delete the created file
|
< 0.001 | 12 | 93 | if didnotexist
|
| | 94 | % @todo Replace This once we have a flag on delete
|
| | 95 | % for disabling the recycle.
|
| | 96 | ov=recycle('off');
|
| | 97 | delete(pj.FileName);
|
| | 98 | recycle(ov);
|
< 0.001 | 12 | 99 | end
|
| | 100 | else
|
| | 101 | error(message('MATLAB:print:CannotCreateOutputFile', pj.FileName));
|
< 0.001 | 12 | 102 | end
|
< 0.001 | 12 | 103 | end
|
Other subfunctions in this file are not included in this listing.