syncfiles/convert.c
Dietrich Epp 910039b77a Convert files incrementally
The previous implementation read the entire file into memory and then
processed it. This version uses reasonably-sized buffers and can handle
files of any size.

Since this new conversion code is a bit more complicated, a test suite
has been added which can be run on ordinary Unix systems.

GitOrigin-RevId: acc7be277103fad1da2d0ca16d1a84be11802fbf
2021-03-24 04:20:45 -04:00

60 lines
1.2 KiB
C

// convert.c - Conversion helper functions.
#include "convert.h"
#include "defs.h"
#include "mac_from_unix_data.h"
#include <CursorCtl.h>
#include <Files.h>
#include <MacErrors.h>
#include <Quickdraw.h>
int convert_read(short ref, long *count, void *data) {
OSErr err;
SpinCursor(1);
err = FSRead(ref, count, data);
switch (err) {
case noErr:
return kConvertOK;
case eofErr:
return kConvertEOF;
default:
print_errcode(err, "could not read source file");
return kConvertError;
}
}
int convert_write(short ref, long count, const void *data) {
OSErr err;
SpinCursor(1);
err = FSWrite(ref, &count, data);
if (err == noErr) {
return kConvertOK;
}
print_errcode(err, "could not write temp file");
return kConvertError;
}
static unsigned short *gFromUnixData;
// Get the table for converting from Unix to Macintosh.
unsigned short *mac_from_unix_data(void) {
Ptr ptr, src, dest;
if (gFromUnixData != NULL) {
return gFromUnixData;
}
ptr = NewPtr(FROM_UNIX_DATALEN);
if (ptr == NULL) {
print_memerr(FROM_UNIX_DATALEN);
return NULL;
}
src = (void *)kFromUnixData;
dest = ptr;
UnpackBits(&src, &dest, FROM_UNIX_DATALEN);
gFromUnixData = (void *)ptr;
return gFromUnixData;
}