?? pathname.c
字號:
/* Copyright (c) 1990-2001 Info-ZIP. All rights reserved. See the accompanying file LICENSE, version 2000-Apr-09 or later (the contents of which are also included in zip.h) for terms of use. If, for some reason, all these files are missing, the Info-ZIP license also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html*//*--------------------------------------------------------------------------- pathname.c Function dealing with the pathname. Mostly C-string work. ---------------------------------------------------------------------------*//*****************************************************************************//* Includes *//*****************************************************************************/#include <string.h>#include <stdio.h>#include <unistd.h>#include <sound.h>#include "pathname.h"#include "helpers.h"#include "macstuff.h"/*****************************************************************************//* Global Vars *//*****************************************************************************/const char ResourceMark[] = "XtraStuf.mac:"; /* see also macos.c */#include "zip.h"/*****************************************************************************//* Functions *//*****************************************************************************//* *---------------------------------------------------------------------- * * FSpFindFolder -- * * This function is a version of the FindFolder function that * returns the result as a FSSpec rather than a vRefNum and dirID. * * Results: * Results will be simaler to that of the FindFolder function. * * Side effects: * None. * *---------------------------------------------------------------------- */OSErrFSpFindFolder( short vRefNum, /* Volume reference number. */ OSType folderType, /* Folder type taken by FindFolder. */ Boolean createFolder, /* Should we create it if non-existant. */ FSSpec *spec) /* Pointer to resulting directory. */{ short foundVRefNum; long foundDirID; OSErr err; err = FindFolder(vRefNum, folderType, createFolder, &foundVRefNum, &foundDirID); if (err != noErr) { return err; } err = FSMakeFSSpecCompat(foundVRefNum, foundDirID, "\p", spec); return err;}/*** return volumename from pathname***/unsigned short GetVolumeFromPath(const char *FullPath, char *VolumeName){const char *VolEnd, *tmpPtr1;char *tmpPtr2 = VolumeName;AssertStr(FullPath,"GetVolumeFromPath")for (VolEnd = FullPath; *VolEnd != '\0' && *VolEnd != ':'; VolEnd++) ;if (*VolEnd == '\0') return 0;for (tmpPtr1 = FullPath; tmpPtr1 != VolEnd;) { *tmpPtr2++ = *tmpPtr1++; }*tmpPtr2 = '\0';return (unsigned short) strlen(VolumeName);}/***********************************//* Function FindNewExtractFolder() *//***********************************/char *FindNewExtractFolder(char *ExtractPath, Boolean uniqueFolder){char buffer[NAME_MAX], *tmpPtr, *namePtr;char *last_dotpos = ExtractPath;short count = 0, folderCount = 0;OSErr err;FSSpec Spec;long theDirID;Boolean isDirectory;unsigned short namelen, pathlen = strlen(ExtractPath);unsigned long ext_length = 0;unsigned long num_to_cut = 0;long firstpart_length = pathlen;AssertStr(ExtractPath,"FindNewExtractFolder ExtractPath == NULL")for (tmpPtr = ExtractPath; *tmpPtr; tmpPtr++) if (*tmpPtr == ':') { folderCount++; namePtr = tmpPtr; }if (folderCount > 1) { namelen = strlen(namePtr);} else { namelen = strlen(ExtractPath);}if (uniqueFolder) { for (count = 0; count < 99; count++) { memset(buffer,0,sizeof(buffer)); if (namelen >= 28) ExtractPath[pathlen-2] = 0x0; else ExtractPath[pathlen-1] = 0x0; sprintf(buffer,"%s%d",ExtractPath,count); GetCompletePath(ExtractPath, buffer, &Spec,&err); err = FSpGetDirectoryID(&Spec, &theDirID, &isDirectory); if (err == -43) break; }} else { /* Look for the last extension pos */ for (tmpPtr = ExtractPath; *tmpPtr; tmpPtr++) if (*tmpPtr == '.') last_dotpos = tmpPtr; ext_length = strlen(last_dotpos); if (ext_length < 6) { /* up to 5 chars are treated as a */ /* normal extension like ".html" or ".class" */ int nameLength = last_dotpos - ExtractPath; if (nameLength > 1) { ExtractPath[nameLength] = 0x0; } else { ExtractPath[pathlen-1] = 0x0; } } else { ExtractPath[pathlen-1] = 0x0; } GetCompletePath(ExtractPath, ExtractPath, &Spec,&err);}/* Foldernames must always end with a colon */sstrcat(ExtractPath,":");return ExtractPath;}/*** creates an archive file name***/void createArchiveName(char *thePath){char *tmpPtr, *namePtr;short folderCount = 0;unsigned short namelen, pathlen = strlen(thePath);if (thePath[pathlen-1] == ':') thePath[pathlen-1] = 0x0;for (tmpPtr = thePath; *tmpPtr; tmpPtr++) if (*tmpPtr == ':') { folderCount++; namePtr = tmpPtr; }namelen = strlen(namePtr); /* we have to eliminate illegal chars: * The name space for Mac filenames and Zip filenames (unix style names) * do both include all printable extended-ASCII characters. The only * difference we have to take care of is the single special character * used as path delimiter: * ':' on MacOS and '/' on Unix and '\' on Dos. * So, to convert between Mac filenames and Unix filenames without any * loss of information, we simply interchange ':' and '/'. Additionally, * we try to convert the coding of the extended-ASCII characters into * InfoZip's standard ISO 8859-1 codepage table. */ MakeCompatibleString(namePtr, '/', '_', '.', '-', -1); /* Avoid filenames like: "Archive..zip" */if (thePath[pathlen-1] == '.') { thePath[pathlen-1] = 0; }if (folderCount >= 1) { /* path contains at least one folder */ if (namelen >= 28) { pathlen = pathlen-4; } thePath[pathlen] = '.'; thePath[pathlen+1] = 'z'; thePath[pathlen+2] = 'i'; thePath[pathlen+3] = 'p'; thePath[pathlen+4] = 0x0; return; }else { /* path contains no folder */ FindDesktopFolder(thePath); createArchiveName(thePath); }}/*** finds the desktop-folder on a volume with** largest amount of free-space.*/void FindDesktopFolder(char *Path){char buffer[255];FSSpec volumes[50]; /* 50 Volumes should be enough */short actVolCount, volIndex = 1, VolCount = 0;OSErr err;short i, foundVRefNum;FSSpec spec;UInt64 freeBytes;UInt64 totalBytes;UInt64 MaxFreeBytes;err = OnLine(volumes, 50, &actVolCount, &volIndex);printerr("OnLine:", (err != -35) && (err != 0), err, __LINE__, __FILE__, "");MaxFreeBytes = 0;for (i=0; i < actVolCount; i++) { XGetVInfo(volumes[i].vRefNum, volumes[i].name, &volumes[i].vRefNum, &freeBytes, &totalBytes); if (MaxFreeBytes < freeBytes) { MaxFreeBytes = freeBytes; foundVRefNum = volumes[i].vRefNum; } if ((freeBytes == 0) && (MaxFreeBytes < freeBytes)) { MaxFreeBytes = freeBytes; foundVRefNum = volumes[i].vRefNum; }} FSpFindFolder(foundVRefNum, kDesktopFolderType, kDontCreateFolder,&spec); GetFullPathFromSpec(buffer, &spec , &err); sstrcat(buffer,Path); sstrcpy(Path,buffer);}/*** return the path without the filename***/char *TruncFilename(char *DirPath, const char *FilePath){char *tmpPtr;char *dirPtr = NULL;AssertStr(DirPath,"TruncFilename")Assert_it(Spec,"TruncFilename","")sstrcpy(DirPath, FilePath);for (tmpPtr = DirPath; *tmpPtr; tmpPtr++) if (*tmpPtr == ':') dirPtr = tmpPtr;if (dirPtr) *++dirPtr = '\0';else printerr("TruncFilename: FilePath has no Folders", -1, -1, __LINE__, __FILE__, FilePath);return DirPath;}/*** return only filename***/char *GetFilename(char *FileName, const char *FilePath){const char *tmpPtr;const char *dirPtr = NULL;Assert_it(FileName,"GetFilename","")Assert_it(FilePath,"GetFilename","")for (tmpPtr = FilePath; *tmpPtr; tmpPtr++) { if (*tmpPtr == ':') { dirPtr = tmpPtr; } }if (dirPtr) { ++dirPtr; /* jump over the ':' */ }else {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -