home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-05-09 | 20.5 KB | 716 lines | [TEXT/KAHL] |
- // parm.c process parameters of wildcard AppleScript extension
- //
- // 93/11/17 File created
- // 93/12/08 *** released Wild 0.1.0 ***
- // 93/12/09 moved common files to the THINK C Folder
- // 94/01/24 removed the licensing mechanism with NO_LICENCE
- // 94/01/24 *** released Wild 1.0.0 ***
- // 94/05/09 Totally removed the licensing mechanism and added the GNU comments
- //
- //--------------------------------------------------------------------------------------------------
- // Copyright © 1993, 1994 by Rainbow Hill Pty Ltd.
- //
- // This program is free software; you can redistribute it and/or modify it under the terms of
- // the GNU General Public License as published by the Free Software Foundation; either version 2
- // of the License, or any later version.
- //
- // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- // See the GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License along with this program;
- // if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- //
-
- //---------------------------- Apple includes
- #include <AppleEvents.h>
- #include <Errors.h>
- #include <Folders.h>
-
- //---------------------------- C includes
- #include <string.h>
-
- //---------------------------- other includes
- #include "wild.h"
- #include "parm.h"
-
- //---------------------------- constant definitions
- #define DUMMY_ERROR_STRING 0
-
- //---------------------------- type definitions
-
- // structure to pass parameters to/from local functions
- struct args_struct {
- AppleEvent *event; // --> AE
- short parNameCode; // --> string number in the parameter names STR# resource
- AEKeyword parID; // --> parameter ID specified in the 'aete' resource
- Ptr result; // <-- value of the parameter
- Boolean *doFlag; // <-- value of the enable flag (when the parm itself is not a flag)
- char *errMess; // <-- error message
- };
- typedef struct args_struct args_t;
-
- //---------------------------- function prototypes
-
- // doDialog accepts input parameters from the user
- static OSErr doDialog(wild_parm_t *, char *); // parms, errMess
-
- // fillDialog fills in the dialog with the parameter values
- static void fillDialog(Handle *, wild_parm_t *);
-
- // getFlag extracts a Boolean from the AE
- // it assumes that value is initialised to its default before executing the function
- static OSErr getFlag(args_t *);
-
- // getOneChar extracts the first character of a string from the AE
- // it assumes that value and doFlag are initialised to their defaults before executing the function
- static OSErr getOneChar(args_t *);
-
- // getOSType extracts up to four characters from the AE
- // the enabling flag always defaults to false
- // the parameter itself has no default
- static OSErr getOSType(args_t *);
-
- // getString extracts a string from the AE
- // it ignores the enabling flag and returns the empty string as default
- static OSErr getString(args_t *);
-
- // setError sets up an error string
- static void setError(
- char *, // <-- here it returns the error string
- short, // --> string number in the parameter names STR# resource
- short // --> string number in the error messages STR# resource
- );
-
- //*************************************************************************************** parm_get
- OSErr parm_get(
- AppleEvent *event, // --> AE to process
- wild_parm_t *parms, // <-- parms
- char *errMess // <-- error message
- ) {
-
- OSErr retVal;
- args_t args;
- unsigned char pStr[WILD_nameLength];
-
- // initialise arguments common to all functions
- args.event = event;
- args.errMess = errMess;
-
- // attempt to retrieve the long wildcard
- parms->doLong = WILD_doLongDef;
- parms->longW = WILD_longDef;
- args.parNameCode = WILD_longName;
- args.parID = WILD_kAElong;
- args.result = (Ptr)&parms->longW;
- args.doFlag = &parms->doLong;
- retVal = getOneChar(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
- if (parms->longW == ':') {
- retVal = bdNamErr;
- setError(errMess, WILD_longName, WILD_badWildErrStr);
- goto DONE_LBL; // --->
- }
-
- // attempt to retrieve the short wildcard
- parms->doShort = WILD_doShortDef;
- parms->shortW = WILD_shortDef;
- args.parNameCode = WILD_shortName;
- args.parID = WILD_kAEshort;
- args.result = (Ptr)&parms->shortW;
- args.doFlag = &parms->doShort;
- retVal = getOneChar(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
- if (parms->shortW == ':') {
- retVal = bdNamErr;
- setError(errMess, WILD_shortName, WILD_badWildErrStr);
- goto DONE_LBL; // --->
- }
-
- // attempt to retrieve the creator
- args.parNameCode = WILD_creatorName;
- args.parID = WILD_kAEcreator;
- args.result = (Ptr)&parms->creator;
- args.doFlag = &parms->doCreator;
- retVal = getOSType(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // attempt to retrieve the file type
- args.parNameCode = WILD_typeName;
- args.parID = WILD_kAEtype;
- args.result = (Ptr)&parms->type;
- args.doFlag = &parms->doType;
- retVal = getOSType(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // attempt to retrieve the files flag
- parms->files = WILD_filesDef;
- args.parNameCode = WILD_filesName;
- args.parID = WILD_kAEfiles;
- args.result = (Ptr)&parms->files;
- retVal = getFlag(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // attempt to retrieve the folders flag
- parms->folders = WILD_foldersDef;
- args.parNameCode = WILD_foldersName;
- args.parID = WILD_kAEfolders;
- args.result = (Ptr)&parms->folders;
- retVal = getFlag(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // attempt to retrieve the case sensitive flag
- parms->caseS = WILD_caseDef;
- args.parNameCode = WILD_caseName;
- args.parID = WILD_kAEcase;
- args.result = (Ptr)&parms->caseS;
- retVal = getFlag(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // attempt to retrieve the selection string
- args.parNameCode = WILD_onlyName;
- args.parID = WILD_kAEonly;
- args.result = (Ptr)parms->only;
- retVal = getString(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // attempt to retrieve the dialog flag
- parms->doDialog = WILD_dialogDef;
- args.parNameCode = WILD_dlogName;
- args.parID = WILD_kAEdialog;
- args.result = (Ptr)&parms->doDialog;
- retVal = getFlag(&args);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- if (parms->doDialog) retVal = doDialog(parms, errMess);
- if (retVal != noErr) goto DONE_LBL; // --->
-
- // when not case sensitive, convert wildcards, creator, file type, and selection to upper case
- if (parms->caseS == false) {
- pStr[0] = 1;
- if (parms->doLong) {
- pStr[1] = parms->longW;
- UprString(pStr, true);
- parms->longW = pStr[1];
- }
- if (parms->doShort) {
- pStr[1] = parms->shortW;
- UprString(pStr, true);
- parms->shortW = pStr[1];
- }
-
- pStr[0] = 4;
- if (parms->doCreator) {
- memcpy((void *)&pStr[1], (const void *)&parms->creator, 4);
- UprString(pStr, true);
- memcpy((void *)&parms->creator, (const void *)&pStr[1], 4);
- }
- if (parms->doType) {
- memcpy((void *)&pStr[1], (const void *)&parms->type, 4);
- UprString(pStr, true);
- memcpy((void *)&parms->type, (const void *)&pStr[1], 4);
- }
-
- if (parms->only[0] != '\0') {
- pStr[0] = strlen(parms->only);
- memcpy((void *)&pStr[1], (const void *)parms->only, (Size)pStr[0]);
- UprString(pStr, true);
- memcpy((void *)parms->only, (const void *)&pStr[1], (Size)pStr[0]);
- }
- }
-
- DONE_LBL:
-
- return (retVal);
- } // parm_get
-
-
- //--------------------------------------------------------------------------------------- doDialog
- static OSErr doDialog(wild_parm_t *parms, char *errMess) {
-
- OSErr retVal;
- DialogPtr dlogPtr;
- short dlogItemNo;
- short dlogItemType;
- Rect dlogItemBox;
- char dlogStr[256];
- Handle dlogItemHdl[WILD_dlogItemArraySize];
- short kItem;
- wild_parm_t tempParms;
- short k;
- short j;
-
- retVal = noErr;
-
- // display the dialog with the current values
- dlogPtr = GetNewDialog(WILD_dlogRsrc, nil, (Ptr)-1L);
-
- // get all the item handles to speed up the dialog handling
- for (kItem = 1; kItem <= WILD_dlogItemArraySize; kItem++) {
- GetDItem(dlogPtr, kItem, &dlogItemType, &dlogItemHdl[kItem], &dlogItemBox);
- }
-
- // fill in the dialog with the original values
- fillDialog(dlogItemHdl, parms);
-
- // highlight
- SelIText(dlogPtr, WILD_dlogOnlyNo, 0, 32767);
-
- // keep updating the values until the user is satisfied or gives up
- do {
- ModalDialog(nil, &dlogItemNo);
-
- switch (dlogItemNo) {
-
- case WILD_dlogRestoreNo:
- fillDialog(dlogItemHdl, parms);
- break;
-
- case WILD_dlogDefaultNo:
- tempParms.files = WILD_filesDef;
- tempParms.folders = WILD_foldersDef;
- tempParms.caseS = WILD_caseDef;
- tempParms.doLong = WILD_doLongDef;
- tempParms.doShort = WILD_doShortDef;
- tempParms.doCreator = false;
- tempParms.doType = false;
- tempParms.longW = WILD_longDef;
- tempParms.shortW = WILD_shortDef;
- tempParms.only[0] = '\0';
- fillDialog(dlogItemHdl, &tempParms);
- break;
-
- case WILD_dlogDoCreatorNo:
- case WILD_dlogDoTypeNo:
- case WILD_dlogDoLongNo:
- case WILD_dlogDoShortNo:
- case WILD_dlogFilesNo:
- case WILD_dlogFoldersNo:
- case WILD_dlogCaseNo:
- SetCtlValue(
- (ControlHandle)dlogItemHdl[dlogItemNo],
- 1 - GetCtlValue((ControlHandle)dlogItemHdl[dlogItemNo])
- );
- break;
-
- case WILD_dlogCreatorNo:
- case WILD_dlogTypeNo:
- GetIText(dlogItemHdl[dlogItemNo], dlogStr);
- if (dlogStr[0] > 4) {
- dlogStr[0] = 4;
- SetIText(dlogItemHdl[dlogItemNo], dlogStr);
- }
- break;
-
- case WILD_dlogLongNo:
- case WILD_dlogShortNo:
- GetIText(dlogItemHdl[dlogItemNo], dlogStr);
- if (dlogStr[0] > 1) {
- dlogStr[0] = 1;
- SetIText(dlogItemHdl[dlogItemNo], dlogStr);
- }
- break;
-
- case WILD_dlogCopyrightNo:
- case WILD_dlogIconNo:
- case WILD_dlogOkNo:
- case WILD_dlogStopNo:
- case WILD_dlogOnlyNo:
- case WILD_dlogOnlyNameNo:
- default:
- break;
- }
- } while (dlogItemNo != WILD_dlogOkNo && dlogItemNo != WILD_dlogStopNo);
-
- // decide whether to abort the whole operation or to return the new setting
- if (dlogItemNo == WILD_dlogStopNo) {
- retVal = userCanceledErr;
- }
- else {
- retVal = noErr;
-
- // copy the flags from the dialog to the parameter structure ...
- parms->doCreator = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoCreatorNo]);
- parms->doType = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoTypeNo]);
- parms->doLong = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoLongNo]);
- parms->doShort = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoShortNo]);
- parms->files = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogFilesNo]);
- parms->folders = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogFoldersNo]);
- parms->caseS = GetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogCaseNo]);
-
- // ... the creator ...
- GetIText(dlogItemHdl[WILD_dlogCreatorNo], dlogStr);
- k = dlogStr[0];
- for (j = 0; j < k; j++) dlogStr[j] = dlogStr[j + 1]; // move to word boundary
- for (; j < 4; j++) dlogStr[j] = WILD_defOSchar;
- parms->creator = *(OSType *)dlogStr;
-
- // ... the file type ...
- GetIText(dlogItemHdl[WILD_dlogTypeNo], dlogStr);
- k = dlogStr[0];
- for (j = 0; j < k; j++) dlogStr[j] = dlogStr[j + 1]; // move to word boundary
- for (; j < 4; j++) dlogStr[j] = WILD_defOSchar;
- parms->type = *(OSType *)dlogStr;
-
- // ... wildcard characters ...
- GetIText(dlogItemHdl[WILD_dlogLongNo], dlogStr);
- parms->longW = dlogStr[1];
- GetIText(dlogItemHdl[WILD_dlogShortNo], dlogStr);
- parms->shortW = dlogStr[1];
-
- // ... and the selection
- GetIText(dlogItemHdl[WILD_dlogOnlyNo], dlogStr);
- memcpy((void *)parms->only, (const void *)&dlogStr[1], (Size)dlogStr[0]);
- parms->only[dlogStr[0]] = '\0';
-
- // check the consistency of the input
- if (parms->doCreator == true && parms->creator == 0L) {
- retVal = errAEWrongDataType;
- setError(errMess, WILD_creatorName, WILD_noStringErrStr);
- }
- if (parms->doType == true && parms->type == 0L) {
- retVal = errAEWrongDataType;
- setError(errMess, WILD_typeName, WILD_noStringErrStr);
- }
- }
-
- // make the DITL resource purgeable and get rid of the whole dialog
- DisposDialog(dlogPtr);
-
- RETURN_LBL:
-
- return (retVal);
- } // doDialog
-
- //--------------------------------------------------------------------------------------- fillDialog
- static void fillDialog(Handle *dlogItemHdl, wild_parm_t *values) {
- char aChar;
- char pStr[WILD_nameLength];
-
- // update the simple flags of the dialog
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogFilesNo], values->files);
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogFoldersNo], values->folders);
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogCaseNo], values->caseS);
-
- // update the long wildcard of the dialog
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoLongNo], values->doLong);
- if (values->doLong)
- pStr[1] = values->longW;
- else
- pStr[1] = WILD_longDef;
- pStr[0] = 1;
- SetIText(dlogItemHdl[WILD_dlogLongNo], pStr);
-
- // update the short wildcard of the dialog
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoShortNo], values->doShort);
- if (values->doShort)
- aChar = values->shortW;
- else
- aChar = WILD_shortDef;
- pStr[0] = 1;
- pStr[1] = aChar; // keep 'aChar' for creator and file type
- SetIText(dlogItemHdl[WILD_dlogShortNo], pStr);
-
- // update the creator of the dialog
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoCreatorNo], values->doCreator);
- if (values->doCreator) {
- memcpy((void *)&pStr[1], (const void *)&values->creator, 4);
- }
- else {
- pStr[1] = aChar;
- pStr[2] = aChar;
- pStr[3] = aChar;
- pStr[4] = aChar;
- }
- pStr[0] = 4;
- SetIText(dlogItemHdl[WILD_dlogCreatorNo], pStr);
-
- // update the file type of the dialog
- SetCtlValue((ControlHandle)dlogItemHdl[WILD_dlogDoTypeNo], values->doType);
- if (values->doType) {
- memcpy((void *)&pStr[1], (const void *)&values->type, 4);
- }
- else {
- pStr[1] = aChar;
- pStr[2] = aChar;
- pStr[3] = aChar;
- pStr[4] = aChar;
- }
- pStr[0] = 4;
- SetIText(dlogItemHdl[WILD_dlogTypeNo], pStr);
-
- // update the selection of the dialog
- if (values->only[0] == '\0') {
- pStr[0] = '\0';
- }
- else {
- pStr[0] = strlen(values->only);
- memcpy((void *)&pStr[1], (const void *)values->only, (Size)pStr[0]);
- }
- SetIText(dlogItemHdl[WILD_dlogOnlyNo], pStr);
-
- } // fillDialog
-
- //--------------------------------------------------------------------------------------- getFlag
- static OSErr getFlag(args_t *args) {
- OSErr retVal;
- DescType typeCode;
- Size actualSize;
- OSType fourBytes;
-
- retVal = AEGetParamPtr(
- args->event,
- args->parID,
- typeWildCard,
- &typeCode,
- (Ptr)&fourBytes,
- (Size)4,
- &actualSize
- );
- if (retVal == noErr) {
- if (typeCode == typeTrue) {
- *(Boolean *)args->result = true;
- }
- else if (typeCode == typeFalse) {
- *(Boolean *)args->result = false;
- }
- else {
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_wrongDataErrStr);
- goto DONE_LBL; // --->
- }
- }
- else if (retVal == errAEDescNotFound) {
-
- // keep the default
- retVal = noErr;
- }
- else {
-
- // there was a problem
- setError(args->errMess, args->parNameCode, DUMMY_ERROR_STRING);
- goto DONE_LBL; // --->
- }
-
- DONE_LBL:
-
- return (retVal);
- } // getFlag
-
- //--------------------------------------------------------------------------------------- getOneChar
- static OSErr getOneChar(args_t *args) {
- OSErr retVal;
- DescType typeCode;
- Size actualSize;
- char aChar;
-
- retVal = AEGetParamPtr(
- args->event,
- args->parID,
- typeWildCard,
- &typeCode,
- (Ptr)&aChar,
- (Size)1,
- &actualSize
- );
- if (retVal == noErr) {
- if (actualSize == 0) {
- if (typeCode == typeTrue) {
-
- // enable the default wildcarding character
- *args->doFlag = true;
- }
- else if (typeCode == typeFalse) {
- *args->doFlag = false;
- }
- else {
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_wrongDataErrStr);
- goto DONE_LBL; // --->
- }
- }
- else if (typeCode != typeChar && typeCode != typeStyledText && typeCode != typeIntlText) {
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_wrongDataErrStr);
- goto DONE_LBL; // --->
- }
- else {
-
- // the user typed the parameter name followed by a value
- *args->doFlag = true;
- *(char *)args->result = aChar;
- }
- }
- else if (retVal == errAEDescNotFound) {
-
- // keep the default
- retVal = noErr;
- }
- else {
-
- // there was a problem
- setError(args->errMess, args->parNameCode, DUMMY_ERROR_STRING);
- goto DONE_LBL; // --->
- }
-
- DONE_LBL:
-
- return (retVal);
- } // getOneChar
-
- //--------------------------------------------------------------------------------------- getOSType
- static OSErr getOSType(args_t *args) {
- OSErr retVal;
- DescType typeCode;
- Size actualSize;
- OSType fourBytes;
-
- retVal = AEGetParamPtr(
- args->event,
- args->parID,
- typeWildCard,
- &typeCode,
- (Ptr)&fourBytes,
- (Size)4,
- &actualSize
- );
- if (retVal == noErr) {
- if (actualSize == 0) {
- if (typeCode == typeTrue) {
-
- // the user typed 'with ...'
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_noStringErrStr);
- goto DONE_LBL; // --->
- }
- else if (typeCode == typeFalse) {
- *args->doFlag = false;
- }
- else {
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_wrongDataErrStr);
- goto DONE_LBL; // --->
- }
- }
- else if (typeCode != typeChar && typeCode != typeStyledText && typeCode != typeIntlText) {
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_wrongDataErrStr);
- goto DONE_LBL; // --->
- }
- else {
-
- // the user typed: creator "..."
- *args->doFlag = true;
- *(OSType *)args->result = fourBytes;
- while (actualSize < 4) ((char *)args->result)[actualSize++] = WILD_defOSchar;
- }
- }
- else if (retVal == errAEDescNotFound) {
-
- // set the default which is always false
- retVal = noErr;
- *args->doFlag = false;
- }
- else {
-
- // there was a problem
- setError(args->errMess, args->parNameCode, DUMMY_ERROR_STRING);
- goto DONE_LBL; // --->
- }
-
- DONE_LBL:
-
- return (retVal);
- } // getOSType
-
- //--------------------------------------------------------------------------------------- getString
- static OSErr getString(args_t *args) {
- OSErr retVal;
- DescType typeCode;
- Size actualSize;
- char aChar;
-
- retVal = AEGetParamPtr(
- args->event,
- args->parID,
- typeWildCard,
- &typeCode,
- args->result,
- (Size)WILD_maxFilenameLen,
- &actualSize
- );
- if (retVal == noErr) {
-
- // we only accept two possibilities:
- // 1. we actually read something and it is of a text type (the user typed "par string")
- // then we terminate the result with a '\0'
- // 2. we read nothing but the type is typeFalse (the user typed "without par")
- // then we are done, because the result was set to '\0' at the beginning
- if (
- actualSize != 0
- &&
- (typeCode == typeChar || typeCode == typeStyledText || typeCode == typeIntlText)
- ) {
-
- // the user typed the parameter name followed by a value
- args->result[actualSize] = '\0';
- }
- else {
-
- args->result[0] = '\0';
-
- if (actualSize != 0 || typeCode != typeFalse) {
-
- // the user typed something that we cannot accept
- retVal = errAEWrongDataType;
- setError(args->errMess, args->parNameCode, WILD_wrongDataErrStr);
- goto DONE_LBL; // --->
- }
- } // either actualSize was zero or the parameter was not of a text type
- } // we successfully got a parameter
- else {
-
- args->result[0] = '\0';
-
- if (retVal == errAEDescNotFound) {
-
- // we did not get a parameter at all
- retVal = noErr;
- }
- else {
-
- // there was a problem
- setError(args->errMess, args->parNameCode, DUMMY_ERROR_STRING);
- goto DONE_LBL; // --->
- }
- } // we didn't get a parameter successfully
-
- DONE_LBL:
-
- return (retVal);
- } // getString
-
- //--------------------------------------------------------------------------------------- setError
- static void setError(
- char *where, // <-- here it returns the error string
- short kPar, // --> string number in the parameter names STR# resource
- short kMess // --> string number in the error messages STR# resource
- ) {
- short k;
- char es[256];
-
- GetIndString(es, WILD_parmNamesRsrc, kPar);
- k = es[0];
- strncpy(where, (const char *)&es[1], (Size)k);
- if (kMess == DUMMY_ERROR_STRING) {
- where[k] = '\0';
- }
- else {
- where[k++] = ':';
- where[k++] = ' ';
- GetIndString(es, WILD_errMessRsrc, kMess);
- strncpy(&where[k], (const char *)&es[1], (Size)es[0]);
- where[k + es[0]] = '\0';
- }
- } // setError
-