InDesign SDK  20.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
FileUtils.h
1 
2 //========================================================================================
3 //
4 // $File$
5 //
6 // Owner: Robin_Briggs
7 //
8 // $Author$
9 //
10 // $DateTime$
11 //
12 // $Revision$
13 //
14 // $Change$
15 //
16 // Copyright 1997-2010 Adobe Systems Incorporated. All rights reserved.
17 //
18 // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance
19 // with the terms of the Adobe license agreement accompanying it. If you have received
20 // this file from a source other than Adobe, then your use, modification, or
21 // distribution of it requires the prior written permission of Adobe.
22 //
23 //
24 // ADOBE CONFIDENTIAL
25 //
26 //========================================================================================
27 
28 #ifndef __FileUtils__
29 #define __FileUtils__
30 
31 class IPMStream;
32 class PMString;
33 class SysFileList;
34 #include <string>
35 #include <vector>
36 #include "AppInfo.h"
37 #include "LocaleSetting.h"
38 
39 #if defined(WINDOWS) || defined(WASM)
40  enum CFURLPathStyle {
41  kCFURLPOSIXPathStyle = 0,
42  kCFURLHFSPathStyle = 1, // Deprecated: Use kCFURLPOSIXPathStyle instead.
43  // Note: Special handling exists in URLUtils.cpp for host names (like localhost)
44  // where kCFURLHFSPathStyle was previously used to correctly handle volume names.
45  kCFURLWindowsPathStyle = 2
46  };
47 #endif
48 
49 class FileUtils {
50 public:
51 #ifdef DEBUG
52  // in a debug build, get the path to a file in the QA:Logs: folder.
53  // will create the QA and Logs folders if none exist.
54  static PMString GetLogFilePath(const PMString& fileName);
55 #endif
56  // returns the directory seperator for the system
57  static char GetDirectorySeperator();
58  // Set the application directory.
59  static bool16 SetAppFolder(SysConnection appResourceHandle = nil);
60  // Return the file descriptor of the application directory
61  static bool16 GetAppFolder(IDFile *appFolder);
62  // Return the pathname for the application directory.
63  static bool16 GetAppFolder(PMString *appFolder);
64  // Return the full path for the application.
65  static bool16 GetAppPath(IDFile *appPath);
66  // Find a specified sub-folder of the application folder.
67  // Sub-folder is specified by name.
68  // NOTE! This function will return kTrue with a valid IDFile even
69  // if the folder does not exist. It does not create the sub-folder for you.
70  static bool16 GetAppSubFolder(IDFile *result, const PMString& subFolderName);
71  // Return the current user's roaming directory with appended folder paths. It wont create the folder if it doesnt exist
72  static bool16 GetUserRoamingFolder(IDFile *result, const PMString& appFolder, const PMString& subFolderName);
73 
88  static bool16 GetAppLocalCachedDataFolder(IDFile *result, const PMString& subFolderName);
89 
104  static bool16 GetAppRoamingDataFolder(IDFile *result, const PMString& subFolderName);
105 
106  // Find a specified sub-folder of the application document folder.
107  // Sub-folder is specified by name. User's unnamed files go here.
108  static bool16 GetAppDocumentFolder(IDFile *result, const PMString& subFolderName);
109  // Find a specified sub-folder of the application document folder.
110  // Sub-folder is specified by name. The Recovery directory goes here
111  static bool16 GetRecoveryFolder(IDFile *result, const PMString& subFolderName);
112  // Find a specified sub-folder of the user dictionaries folder.
113  // Creates the folder if it doesn't already exist.
114  // Sub-folder is specified by name. The user dictionaries files go here.
115  static bool16 GetUserDictionariesFolder(IDFile *result, const PMString& subFolderName);
116 
117  // Set the current working directory to the target file descriptor
118  static bool16 SetCurrentFolder(const IDFile& targetFolder);
119  // Return the file descriptor of the current working directory
120  static bool16 GetCurrentFolder(IDFile *currentFolder);
121 
122  // Append pathToAppend onto the end of originalPath.
123  static void AppendPath(IDFile *originalPath, const PMString& pathToAppend);
124  // Get the filename out of the IDFile.
125  static void GetFileName(const IDFile& fSpec, PMString& fileName);
126  static void GetFileName(const PMString& pathStr, PMString& fileName);
127 
128  // Compare the two file names for equality.
129  static bool16 IsEqual(const IDFile& file1, const IDFile& file2);
130 
131  // Does not actually swap the files, but simply moves file1 to file2. Returns false if operation fails.
132  static bool SwapFiles(const IDFile& file1, const IDFile& file2);
133 
142  static void IDFileToPMString(const IDFile& file, PMString& pathStr);
143 
152  static void PMStringToIDFile(const PMString& pathStr, IDFile& file);
153 
162  static PMString SysFileToPMString(const IDFile& file);
163 
172  static IDFile PMStringToSysFile(const PMString& pathStr);
173 
183  static PMString SysFileToFileURL(const IDFile& fSpec);
184 
196  static PMString PMStringToFileURL(const PMString& fileName);
197 
205  static PMString PMStringToURL(const PMString& str);
206 
215  static PMString URLToPMString (const std::string &URL);
216 
226  static std::string UTF8ToURL(const std::string& str, const char kSeparator);
227 
235  static std::string URLToUTF8 (const std::string& fileURL, bool & ContainsUTF16);
236 
255  static PMString FileURLToPMString (const std::string &fileURL);
256 
275  static PMString FileURLToPMString (const PMString &__fileURL);
276 
313  static IDFile FileURLToSysFile(const std::string& __fileURL, bool *isReturingAValidSysFile = NULL);
314 
350  static IDFile FileURLToSysFile(const PMString & __fileURL, bool *isReturingAValidSysFile = NULL);
351 
352  // If pathName is a relative path, it is resolved into a full path,
353  // based on the application directory. Indirection to parent folder
354  // (UNIX "..") is not supported. If the path is already fully resolved,
355  // returns nil.
356  static PMString *CreateResolvedPathName(const PMString& pathName);
357 
358  // Returns the amount of free space in K, on the volume or disk
359  // that holds the specified directory.
360  static uint32 SpaceAvail(const IDFile& diskToCheck);
361 
362  static bool16 IsDirectory(const IDFile& fileSpec);
363 
364  static bool16 CreateFolderIfNeeded(const IDFile& folderFileSpec, bool16 bCreateParent = kFalse);
365  static bool16 CopyFile(const IDFile& srcFile, const IDFile& dstFile);
366  static bool16 MoveFile(const IDFile& srcFile, const IDFile& dstFolder, IDFile* dstFile = NULL);
367  static bool16 GetParentDirectory(IDFile const &file, IDFile &parent);
368  static bool16 DoesFileExist(const IDFile& fileSpec);
369  // get the path, without the filename
370  static void GetPathOnly(const IDFile& fileSpec, PMString& pathName, bool16 includeSeparator = kFalse);
371  //get the filename, minus any extension
372  static void GetBaseFileName(const IDFile& inFile, PMString& outString);
373  static void GetBaseFileName(const PMString& inString, PMString& outString);
374  // Kinda the opposite of GetBaseFileName - returns only the extension
375  static void GetExtension(const IDFile& inFile, PMString& outString);
376 
377  //ResourceFork deprecated, function returns data fork size
378  static uint32 GetFileSize(const IDFile& fileSpec, bool16 includeResourceFork = kFalse);
379 
380 
381  // This function returns the file type(pub, template, book, library etc.) based on the passed in sysFile.
382  static bool16 GetOpenFileType(const IDFile& sysFile, SysFileType &type);
383 
384  // This function is like the one above, but takes an open stream as its input.
385  static bool16 GetStreamFileType(IPMStream *stream, SysFileType &type);
386 
387  // Get the name of the volume the file or folder is on.
388  static void GetVolumeName(const IDFile *fileOrFolderSpec, PMString * volumeName);
389 
390  // Compare the two file names for same volume.
391  static bool16 OnSameVolume(const IDFile& file1, const IDFile& file2);
392 
393  static int32 ShowFile( const IDFile& fileSpec);
394 
395  typedef enum { kNormal, kOpenNewWindow, kOpenMaximized } ViewMode;
396  static ErrorCode BrowseToFile(const IDFile *fileSpec, ViewMode mode = kNormal);
397  static ErrorCode OpenFileInEditor(const IDFile& fileToOpen, const AppInfo& appInfo = kNullAppInfo, PMString *errorString = nil);
398 
399  static bool16 IsNetworkDrive( const IDFile& sysFile );
400 
401  // Return the directory where app is installed by user. It should return the same folder as
402  // GetAppFolder on WINDOWS. On MACINTOSH, it should return the folder three level above the
403  // real application file due to mac package folder structure.
404  static bool16 GetAppInstallationFolder(IDFile *folder);
405 
413  static bool16 GetPresetsFolder(IDFile *folder, const PMString& subFolderName, const PMLocaleId& localeId);
414 
421  static void AppendLocalizedFolder(IDFile* folder, const PMLocaleId& localeId);
422 
423  // NOTE: Just moved this out of the #ifdef MAC section, and put a Windows impl in
424  static bool16 GetModificationDate(const IDFile & spec, uint32 *modDate);
425 
426  // Return kTrue if the file can be opened with the requested access rights.
427  typedef enum { kRead, kWrite, kReadWrite } Access;
428  static bool8 CanOpen(const IDFile& spec, Access access = kReadWrite);
429 
446  static void PrefixFileURL (const PMString &prefix, PMString &fileURL);
447 
454  static FILE* OpenFile(const IDFile& file, const char* mode);
455 
463  static FILE* OpenFile(const PMString& path, const char* mode);
464 
472  static FILE* OpenFile(const char* path, const char* mode);
473 
474 #ifdef MACINTOSH
475 
476  // Append pathToAppend onto the end of originalPath.
477  static void AppendPath(PMString *originalPath, const PMString& pathToAppend);
478 
479  // Get the dirID of the folder
480  static bool16 GetDirID(const IDFile& folder, int32* dirID);
481 
482  static void ReplaceResource(SysHandle rsrc, int32 type, int16 id, const PMString& name);
483 
484  static void GetFileCreatorType(const IDFile& fileSpec, SysOSType* fileType, SysOSType* creator);
485  static bool16 GetCreationDate(const IDFile & spec, uint32 *creDate);
486 
487  // Return the directory where application package is located. It should be two level above the
488  // real application file due to mac package folder structure.
489  static bool16 GetPackageFolder(IDFile *folder);
490 
491  // Create the file using the Unicode file name in the IDFile.
492  // Set bCreateParent to kTrue to create parent folders if necessary.
493  // If the file cannot be created or already exists, the method returns kFalse.
494  static bool16 CreateFileWithUnicodeName(IDFile& fileSpec, bool16 bCreateParent = kFalse);
495 
507  static std::string FileURLToPosixPath (const std::string& fileURL) ;
508 
516  static PMString DecomposeUnicode (const PMString &str);
517 
523  static void NormalizeUnComposedUnicodeChars (PMString &fileName);
524 
530  static IDFile PosixPathToSysFile(const PMString& posixPath);
531  static IDFile PosixPathToSysFile(const std::string& posixPath);
532 
542  static OSErr FSRefToIDFile(const FSRef& fsRef, IDFile& file);
543 
553  static OSErr IDFileToFSRef(const IDFile& file, FSRef& fsRef);
554 
560  static bool16 IsFileInTrash(const IDFile& file);
561 
569  static OSErr GetURLAppleEventDescFromCFURL(AEDesc* fURLDesc, CFURLRef url);
570 
571  /*
572  Decodes an Apple event descriptor record
573  @param aeDes IN AEDesc the input Apple Event descriptor to decode
574  @return CFURLRef on success, NULL if any error occurs.
575  Caller needs to release the CFURLRef
576  */
577  static CFURLRef CreateCFURLFromURLAppleEventDesc(const AEDesc* aeDesc);
578 
579 #endif
580 
581 #ifdef WINDOWS
582  // Windows edge code for implementing common dialog that checks
583  // for filenames with bad characters
584 
585  static IDThreading::ThreadLocal<DWORD> tl_GOFNCallerFlags;
586  static IDThreading::ThreadLocal<LPOFNHOOKPROC> tl_GOFNCallerHookProc;
587  static IDThreading::ThreadLocal<LPOPENFILENAME> tl_GOFNCallerlpofn;
588 
589  // Hook function used by FileUtils::GetOpenFileName()
590  static UINT_PTR APIENTRY GOFNHookFunction(HWND dlg, UINT msg, WPARAM wParam, LPARAM lParam);
591 
592  // Same as ::GetOpenFileName() but catches files with bad names and warns user
593  // Uses the AWS GetFileDialog() GUI if AWS is defined and will load the given
594  // SysFileList with the result files
595  // K2 should use this call in preference to Win32 API ::GetOpenFileName()
596  static bool16 GetOpenFileName(LPOPENFILENAME lpofn);
597  static bool16 GetOpenFileName(LPOPENFILENAME lpofn, SysFileList& resultFiles);
598  static bool16 GetOpenFileName(LPOPENFILENAME lpofn, SysFileList* pResultFiles);
599 
601  static int32 CoverSHGetFolderPath(int nFolder, IDFile *path);
602 #endif
603 
604 #ifdef WASM
605 
615  static bool16 GetAppTmpFolder(IDFile *result, const PMString& subFolderName);
616 #endif
617 
618  /*
619  Query if preferences folder was created in current app launch
620  @return kFalse, if the roaming data folder existed before app launch, kTrue otherwise
621  */
622  static bool16 IsAppRoamingDataFolderNew();
623 
632  static bool16 GetAppLocalCachedDataFolderSansLocale(IDFile *result);
633 
634  static textchar GetDirectorySeparatorTextChar();
635 
640  static bool16 IsTemplate(const IDFile & file);
641 
646  static bool16 IsTempFile(const IDFile & file);
647 
648 #ifdef MACINTOSH
649 
660  static void IDFileToPMStringBasedOnPathString(const IDFile& file, PMString& pathStr);
661 #endif
662 
670  static void CreateUniqueFileName(PMString & fileName);
671 
672 
673 #ifdef WINDOWS
674 
679  static void IDFileToPMStringWithUNCConversion(const IDFile& file, PMString& pathStr);
680 
681  typedef enum { kSystemPath, kWindowsPath } DirPath;
682  static HMODULE LoadLibraryUsingAbsolutePath(const std::wstring& componentName, DirPath dirPath = kSystemPath);
683 #endif
684 
691 
697 
703  static void AppendPaths(IDFile *originalPath, const std::vector<PMString> &pathsToAppend);
704 
710  static void AppendPaths(IDFile *originalPath, const std::vector<const wchar_t*> &pathsToAppend);
711 
712  // Posix directory seperator
713 
719  static char GetPosixDirectorySeparator();
720 
726  static textchar GetPosixDirectorySeparatorTextChar();
727 
734 
739  static CFURLPathStyle GetMacPathStyle();
740 
745  static char GetMacDirectorySeparator();
746 
751  static textchar GetMacDirectorySeparatorTextChar();
752 
758 
759 #ifdef MACINTOSH
760  // HFS <-> Posix conversion API's
761 
767  static void HFSPathToPOSIXPath(const PMString &hfsPath, PMString &posixPath);
768 
774  static void HFSPathToPOSIXPath(const std::string &hfsPath, std::string &posixPath);
775 
781  static void POSIXPathToHFSPath(const PMString &posixPath, PMString &hfsPath);
782 
788  static void POSIXPathToHFSPath(const std::string &posixPath, std::string &hfsPath);
789 
795  static void AppendPaths(PMString *originalPath, const std::vector<const PMString> &pathsToAppend);
796 
802  static void AppendPaths(PMString *originalPath, const std::vector<const wchar_t*> &pathsToAppend);
803 
804 #endif
805 private :
806 
807 
808 
821  static std::string FileURLToUTF8Path (const std::string& fileURL, const char kSeperator, bool & ContainsUTF16);
822 
836  static int16 GetHexChar (const std::string & string, int index);
837 
848  static bool16 IsFirstFilePrefixOfSecond(const IDFile & firstFile, const IDFile & secondFile);
849 };
850 
851 #endif