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-2010 The MathWorks, Inc.
|
| | 10 |
|
| | 11 | %Generate a name we would use if had to in various circumstances.
|
| 6 | 12 | if isfigure( pj.Handles{1}(1) )
|
| 6 | 13 | objName = ['figure' int2str( double(pj.Handles{1}(1) ))];
|
| | 14 | else
|
| | 15 | objName = get_param(pj.Handles{1}(1),'name');
|
| | 16 | end
|
| | 17 |
|
| 6 | 18 | tellUserFilename = 0;
|
| 6 | 19 | if isempty(pj.FileName)
|
| | 20 | if pj.DriverExport && ...
|
| | 21 | ~strcmp(pj.DriverClass,'MW') && ~pj.DriverClipboard && ~pj.RGBImage
|
| | 22 | %These kinds of files shouldn't go to printer, generate file on disk
|
| | 23 | pj.FileName = objName;
|
| | 24 | tellUserFilename = 1;
|
| | 25 |
|
| | 26 | else
|
| | 27 | %File is going to be sent to an output device by OS or us.
|
| | 28 | if ~strcmp(pj.DriverClass,'MW') && ~pj.DriverClipboard && ~pj.RGBImage
|
| | 29 | %Going to a file, invent a name
|
| | 30 | pj.FileName = tempname;
|
| | 31 | pj.PrintOutput = 1;
|
| | 32 | end
|
| | 33 | end
|
| | 34 |
|
| | 35 | % only support going to clipboard w/o specifying -clipboard option
|
| | 36 | % for the grandfathered cases on Windows
|
| | 37 | if ~pj.ClipboardOption && pj.DriverClipboard
|
| | 38 | if ~ispc || (ispc && ~any(strcmp(pj.Driver, {'meta', 'bitmap'})))
|
| | 39 | pj.FileName = objName;
|
| | 40 | tellUserFilename = 1;
|
| | 41 | end
|
| | 42 | end
|
| 6 | 43 | else
|
| | 44 | %Both / and \ are commonly used, but in MATLAB we recognize only filesep
|
| 6 | 45 | pj.FileName = strrep( pj.FileName, '/', filesep );
|
| 6 | 46 | pj.FileName = strrep( pj.FileName, '\', filesep );
|
| 6 | 47 | end
|
| | 48 |
|
| | 49 | %Append appropriate extension to filename if
|
| | 50 | % 1) it doesn't have one, and
|
| | 51 | % 2) we've determined a good one
|
| 6 | 52 | if ~isempty( pj.DriverExt ) && ~isempty( pj.FileName )
|
| | 53 | %Could assert that ~isempty( pj.FileName )
|
0.01 | 6 | 54 | [p,n,e] = fileparts( pj.FileName );
|
| 6 | 55 | if isempty( e )
|
| | 56 | pj.FileName = fullfile( p, [n '.' pj.DriverExt] );
|
| | 57 | end
|
| 6 | 58 | end
|
| | 59 |
|
| | 60 | % on unix fix the file spec if it starts with '~/' so we look at the user's home dir
|
| 6 | 61 | if (isunix)
|
0.01 | 6 | 62 | pj.FileName = fixTilde(pj.FileName);
|
| 6 | 63 | end
|
| | 64 |
|
| 6 | 65 | if tellUserFilename
|
| | 66 | %Invented name above because of device or bad filename
|
| | 67 | if tellUserFilename == 1
|
| | 68 | errStr1 = sprintf( 'Files produced by the ''%s'' driver cannot be sent to printer.\n', pj.Driver);
|
| | 69 | else
|
| | 70 | errStr1 = '';
|
| | 71 | end
|
| | 72 |
|
| | 73 | warning(message('MATLAB:print:SavingToDifferentName', errStr1, pj.FileName))
|
| | 74 | end
|
| | 75 |
|
| | 76 | % Check that we can write to the output file
|
| 6 | 77 | if ~isempty(pj.FileName)
|
| 6 | 78 | [fpath,fname,ext] = fileparts(pj.FileName);
|
| 6 | 79 | if isempty(fpath)
|
| | 80 | fpath = '.';
|
| | 81 | end
|
| 6 | 82 | pj.FileName = fullfile(fpath,[fname ext]);
|
| | 83 | % first check if readable file already exists there
|
0.01 | 6 | 84 | fidread = fopen(pj.FileName,'r');
|
| 6 | 85 | didnotexist = (fidread == -1);
|
| 6 | 86 | if ~didnotexist
|
| 6 | 87 | fclose(fidread);
|
| 6 | 88 | end
|
| | 89 | % now check if file is appendable (will create file if not there)
|
| 6 | 90 | fidappend = fopen(pj.FileName,'a');
|
| 6 | 91 | if fidappend ~= -1
|
| 6 | 92 | fclose(fidappend);
|
| | 93 | % check if we have to delete the created file
|
| 6 | 94 | if didnotexist
|
| | 95 | % @todo Replace This once we have a flag on delete
|
| | 96 | % for disabling the recycle.
|
| | 97 | ov=recycle('off');
|
| | 98 | delete(pj.FileName);
|
| | 99 | recycle(ov);
|
| | 100 | end
|
| | 101 | else
|
| | 102 | error(message('MATLAB:print:CannotCreateOutputFile', pj.FileName));
|
| | 103 | end
|
| 6 | 104 | end
|
| | 105 |
|
| | 106 | %A little business to clear up.
|
| | 107 | %Now that we found good extension and all that for GS driver,
|
| | 108 | %we swap in a PS driver and temporary name for later conversion
|
| | 109 | %to a file whose name we just prettied up.
|
| 6 | 110 | if strcmp( pj.DriverClass, 'GS' )
|
| | 111 | %Remember actual device, get MATLAB to produce PostScript
|
| | 112 | %for later conversion by GhostScript
|
| | 113 | pj.GhostDriver = pj.Driver;
|
| | 114 | if pj.DriverColor
|
| | 115 | pj.Driver = 'psc';
|
| | 116 | else
|
| | 117 | pj.Driver = 'ps';
|
| | 118 | end
|
| | 119 | pj.GhostName = pj.FileName;
|
| | 120 | pj.FileName = [tempname '.ps'];
|
| | 121 | end
|
Other subfunctions in this file are not included in this listing.