home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / ufilemgr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.0 KB  |  190 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #pragma once
  20. // ===========================================================================
  21. // ufilemgr.h
  22. // File manager utility routines.
  23. // File manager class that takes care of temporary file management
  24. // Created by atotic, June 15th, 1994
  25. // ===========================================================================
  26.  
  27. #include <LArray.h>
  28. #include <LFileStream.h>
  29. #include "PascalString.h"
  30.  
  31. class LFile;
  32. struct CStr255;
  33. class cstring;
  34. typedef struct URL_Struct_ URL_Struct;
  35. class LFileBufferStream;
  36. typedef struct MWContext_ MWContext;
  37.  
  38. struct AEDesc;
  39. typedef AEDesc AEDescList;
  40. typedef AEDescList AERecord;
  41. typedef AERecord AppleEvent;
  42.  
  43.  
  44. /*****************************************************************************
  45.  * class CFileMgr
  46.  * does general file routines. It provides many file utility functions, (as static)
  47.  * and keeps track of temporary files (throught sFileManager). 
  48.  * It deletes temporary files as necessary. (disk overflow, on quit).
  49.  * To register a temporary file, call RegisterFile
  50.  * If registered file is deleted, call CancelRegister
  51.  * All registered files are deleted when application quits.
  52.  * Only one instance of the manager is created. (sFileManager global)
  53.  * THIS CLASS IS GETTING UNMANAGEABLE. SPLIT IT
  54.  *****************************************************************************/
  55. class CFileMgr;
  56.  
  57. class CFileMgr    {
  58.     LArray fFiles;                        // Registered files
  59. public:
  60.     static CFileMgr sFileManager;        // The manager
  61.     static unsigned int sMungeNum;        // Number used to automatically generate a unique file name
  62. // ÑÑ constructors/destructors
  63.     virtual ~CFileMgr();
  64.  
  65. // ÑÑ file management interface
  66.     
  67.     void RegisterFile(LFileBufferStream * inFile);        // Register a file
  68.     void CancelRegister(LFileBufferStream * inFile );    // Cancel file registration.
  69.     void CancelAndDelete(LFileBufferStream * inFile );    // Cancels registration, and deletes the file.
  70.  
  71.     void HandleFindURLEvent(const AppleEvent    &inAppleEvent,
  72.                             AppleEvent            &outAEReply,
  73.                             AEDesc                &outResult,
  74.                             long                inAENumber);
  75. // ÑÑ utility functions, all static
  76. // ÑÑ MacOS
  77.     // Generates file spec for a file that does not exist (not created yet)
  78.     // Used to create a new unique temporary files
  79.     static OSErr UniqueFileSpec(const FSSpec& inSpec, const CStr31& genericName,
  80.                                 FSSpec & outSpec);
  81.     // File spec for non-existent file. Name is based on URL
  82.     static OSErr NewFileSpecFromURLStruct (const char * location, 
  83.                                     const FSSpec& inSpec,
  84.                                     FSSpec &outSpec);
  85.     // Does a volume have a desktop database
  86.     static OSErr    VolHasDesktopDB (short vRefNum, Boolean& hasDesktop);
  87.     // Set file comment
  88.     static void     FileSetComment (const FSSpec& file, const CStr255& comment);
  89.     // Get system volume
  90.     static OSErr     GetSysVolume(short& vRefNum);
  91.     // Get volume by number (used for iteration, returns nsvErr when there are no more volumes)
  92.     static OSErr    GetIndVolume(short index,  short& vRefNum);
  93.     // Finds a writable folder inside a folder
  94.     static OSErr FindWFolderInFolder(short refNum,         // Directory/folder to be searched
  95.                                     long dirID,
  96.                                     const CStr255 &folderName,    // Name of the folder to search for
  97.                                     short * outRefNum,// Location of the found folder
  98.                                     long * outDirID);
  99.     // Creates a folder inside a folder
  100.     static OSErr CreateFolderInFolder(short refNum,         // Parent directory/volume
  101.                                     long dirID,
  102.                                     const CStr255 &folderName,    // Name of the new folder
  103.                                     short * outRefNum,    // Location of the created folder
  104.                                     long * outDirID);
  105.     // Creates a folder spec from folder id;
  106.     static OSErr FolderSpecFromFolderID(short vRefNum, long dirID, FSSpec& folderSpec);
  107.     // Returns a full pathname to the given file
  108.     static char * PathNameFromFSSpec(const FSSpec& inSpec, Boolean wantLeafName );
  109.     // PathNameFromFSSpec + hex encoding
  110.     static char * EncodedPathNameFromFSSpec(const FSSpec& inSpec, Boolean wantLeafName );    
  111.     // Returns file spec from full pathname
  112.     static OSErr FSSpecFromPathname(char * PathNamePtr, FSSpec* outSpec);
  113.     // Changes the creator/file 
  114.     static OSErr SetFileTypeCreator(OSType creator, OSType type, const FSSpec * fileSpec);
  115.     // Set or clear a finder flag for the file (e.g. custom icon bit)
  116.     static OSErr SetFileFinderFlag(const FSSpec& fileSpec, Uint16 flagMask, Uint8 value);
  117.     // Is this FSSpec a folder?
  118.     static Boolean IsFolder(const FSSpec& spec);
  119.     // Force the Finder to refresh a window
  120.     static OSErr   UpdateFinderDisplay(const FSSpec& spec);
  121.     // Creates an alias file with a given name/location pointing to the specified target
  122.     static void    MakeAliasFile(const FSSpec& aliasSpec, const FSSpec& target);
  123.     // Copy a file
  124.     static OSErr   CopyFile(const FSSpec& srcSpec, const FSSpec &dstDirSpec, const CStr255 ©Name);
  125.     
  126. // ÑÑ Application searching
  127.  
  128.     // Finds an application given a its sig. Returns err if app is not found
  129.     // Copied almost exactly from LaunchBySignature volume search in MacApp
  130.     static OSErr FindApplication(OSType sig, FSSpec& file);
  131.     // Finds app on given volume
  132.     static OSErr FindAppOnVolume(OSType sig, short vRefNum, FSSpec& thefile);
  133.  
  134. // ÑÑ╩WWW utilities
  135.     // Given a URL, it returns a 'reasonable' file name
  136.     static CStr31     FileNameFromURL (const char * url);
  137.     // Returns a URL corresponding to the file spec. (See code for more info)
  138.     static char *     GetURLFromFileSpec(const FSSpec &inSpec);
  139.     // Encodes the Macintosh full path with hex escapes
  140.     static char *     EncodeMacPath(char * path, Boolean prependSlash = true);
  141.     // Decodes the Macintosh full path from one encoded with EncodeMacPath
  142.     static char *     DecodeMacPath(char * path);
  143.     // File spec from URL. Reverses GetURLFromFileSpec
  144.     static OSErr     FSSpecFromLocalUnixPath(const char * url, FSSpec * outSpec, Boolean resolveAlias = true);
  145.     // Convert a unix path into a mac one.  Called by FSSpecFromLocalUnixPath().
  146.     static char* MacPathFromUnixPath(const char* unixPath);
  147.     
  148. // Misc file utilities
  149.  
  150.     // Does the file exist?
  151.     static Boolean    FileExists( const FSSpec& fsSpec );
  152.     // Does the file have data fork?
  153.     static Boolean    FileHasDataFork(const FSSpec& fsSpec );
  154.     // Does the file have resource fork?
  155.     static Boolean    FileHasResourceFork(const FSSpec& fsSpec );
  156.     // Just does an FSSpec copy, because we cannot do assignments with them
  157.     static void        CopyFSSpec(const FSSpec & srcSpec, FSSpec & destSpec);
  158.     // Given a folder FSSpec, returns a folder ID
  159.     static OSErr    GetFolderID(FSSpec& folderSpec, long& dirID);
  160.     // Recursively, deletes a folder
  161.     // if noErr, deletion was successful
  162.     // otherwise, returns the last encountered error when deleting
  163.     static OSErr    DeleteFolder(const FSSpec& folderSpec);
  164. };
  165.  
  166. // Iterates through all the files inside the folder.
  167. // Does not deal with deletions during iteration
  168. class CFileIter    {
  169. public:
  170.     CFileIter(const FSSpec &folderSpec);
  171.     ~CFileIter(){};
  172.     Boolean Next(FSSpec &nextFile);
  173.     Boolean Next(FSSpec &nextFile, FInfo& finderInfo, Boolean& folder);
  174. private:
  175.     FSSpec fDir;
  176.     short     fIndex;    // Index for looping
  177. };
  178.  
  179. //-----------------------------------------------------------------------------
  180. // More Utilities
  181. //-----------------------------------------------------------------------------
  182.  
  183. void
  184. WriteCString (LStream * s, const char * c);
  185.  
  186. void
  187. WriteChar (LStream * s, char c);
  188.  
  189. void fe_FileNameFromContext( MWContext* context, const char* url, CStr31& defaultName );
  190.