Skip to content

Instantly share code, notes, and snippets.

@clehner
Created September 26, 2014 17:06
Show Gist options
  • Save clehner/73e04251679aedfd8b40 to your computer and use it in GitHub Desktop.
Save clehner/73e04251679aedfd8b40 to your computer and use it in GitHub Desktop.
OpenTest: check PBOpen and PBOpenDF vs. FSOpen and OpenDF in MPW libraries
#include <stdio.h>
#include <string.h>
#include <StandardFile.h>
#include <Memory.h>
#include <Files.h>
#include <Devices.h>
OSErr MyOpenDF(ConstStr255Param fileName, short vRefNum, short *refNum)
{
OSErr oe;
ParamBlockRec pb;
memset(&pb, 0, sizeof(pb));
pb.ioParam.ioNamePtr = (StringPtr)fileName;
pb.ioParam.ioVRefNum = vRefNum;
oe = PBOpenDFSync(&pb);
*refNum = pb.ioParam.ioRefNum;
return oe;
}
OSErr MyOpen(ConstStr255Param fileName, short vRefNum, short *refNum)
{
OSErr oe;
ParamBlockRec pb;
memset(&pb, 0, sizeof(pb));
pb.ioParam.ioNamePtr = (StringPtr)fileName;
pb.ioParam.ioVRefNum = vRefNum;
oe = PBOpenSync(&pb);
*refNum = pb.ioParam.ioRefNum;
return oe;
}
void main () {
short refNum;
OSErr oe;
char cFileName[256];
StringPtr fileName;
char buffer[] = "test stuff";
long bytes = sizeof buffer;
int len;
printf("Enter filename to write to:\n");
scanf("%s", cFileName);
len = strlen(cFileName);
printf("got filename (%u): %s\n", len, cFileName);
// convert filename to pascal string
fileName = (StringPtr)NewPtr(len+1);
fileName[0] = len;
BlockMove(cFileName, fileName+1, len);
oe = Create(fileName, 0, 'MPW ', 'TEXT');
if (oe == dupFNErr) {
printf("Replacing existing file\n");
} else if (oe != noErr) {
printf("Create: %d\n", oe);
return;
}
printf("\nTrying Apple glue open functions:\n\n");
printf("Trying OpenDF\n");
oe = OpenDF(fileName, 0, &refNum);
if (oe != noErr) {
printf("OpenDF: %d\n", oe);
} else {
printf("OpenDF success.\n");
printf("Closing file.\n");
FSClose(refNum);
}
printf("Trying FSOpen\n");
oe = FSOpen(fileName, 0, &refNum);
if (oe != noErr) {
printf("FSOpen: %d\n", oe);
return;
} else {
printf("FSOpen success.\n");
printf("Closing file.\n");
FSClose(refNum);
}
printf("\nTrying my glue open functions:\n\n");
printf("Trying OpenDF\n");
oe = MyOpenDF(fileName, 0, &refNum);
if (oe != noErr) {
printf("OpenDF: %d\n", oe);
} else {
printf("OpenDF success.\n");
printf("Closing file.\n");
FSClose(refNum);
}
printf("Trying FSOpen\n");
oe = MyOpen(fileName, 0, &refNum);
if (oe != noErr) {
printf("FSOpen: %d\n", oe);
return;
} else {
printf("FSOpen success.\n");
}
printf("\n");
oe = FSWrite(refNum, &bytes, buffer);
if (oe != noErr) {
printf("FSWrite: %d\n", oe);
return;
} else {
printf("FSWrite success\n");
}
printf("Closing file.\n");
FSClose(refNum);
}
# File: OpenTest.make
# Target: OpenTest
MAKEFILE = OpenTest.make
�MondoBuild� = {MAKEFILE} # Make blank to avoid rebuilds when makefile is modified
ObjDir = :
Includes =
Sym-68K = -sym off
COptions = {Includes} {Sym-68K} -model near
### Source Files ###
SrcFiles = �
OpenTest.c
### Object Files ###
ObjFiles-68K = �
"{ObjDir}OpenTest.c.o"
### Libraries ###
LibFiles-68K = �
"{Libraries}MathLib.o" �
"{CLibraries}StdCLib.o" �
"{CLibraries}IOStreams.o" �
"{CLibraries}CPlusLib.o" �
"{Libraries}SIOW.o" �
"{Libraries}MacRuntime.o" �
"{Libraries}IntEnv.o" �
"{Libraries}ToolLibs.o" �
"{Libraries}Interface.o"
### Default Rules ###
.c.o � .c {�MondoBuild�}
{C} {depDir}{default}.c -o {targDir}{default}.c.o {COptions}
### Build Rules ###
OpenTest �� {ObjFiles-68K} {LibFiles-68K} {�MondoBuild�}
ILink �
-o {Targ} �
{ObjFiles-68K} �
{LibFiles-68K} �
{Sym-68K} �
-mf -d �
-t 'APPL' �
-c 'siow' �
-model near �
-state rewrite �
-compact -pad 0
If "{Sym-68K}" =~ /-sym �[nNuU]�/
ILinkToSYM {Targ}.NJ -mf -sym 3.2 -c 'sade'
End
OpenTest �� "{RIncludes}"SIOW.r {�MondoBuild�}
Rez "{RIncludes}"SIOW.r -o {Targ} -append
#Duplicate {Targ} ":OpenTest:"
### Required Dependencies ###
"{ObjDir}OpenTest.c.o" � OpenTest.c
### Optional Dependencies ###
### Build this target to generate "include file" dependencies. ###
Dependencies � $OutOfDate
MakeDepend �
-append {MAKEFILE} �
-ignore "{CIncludes}" �
-objdir "{ObjDir}" �
-objext .o �
{Includes} �
Enter filename to write to:
ioj
got filename (3): ioj
Trying Apple glue open functions:
Trying OpenDF
OpenDF success.
Closing file.
Trying FSOpen
FSOpen success.
Closing file.
Trying my glue open functions:
Trying OpenDF
OpenDF success.
Closing file.
Trying FSOpen
FSOpen success.
FSWrite success
Closing file.
Enter filename to write to:
ok
got filename (2): ok
Trying Apple glue open functions:
Trying OpenDF
OpenDF success.
Closing file.
Trying FSOpen
FSOpen success.
Closing file.
Trying my glue open functions:
Trying OpenDF
OpenDF: -50
Trying FSOpen
FSOpen success.
FSWrite success
Closing file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment