TweetFollow Us on Twitter

Jun 99 Tips

Volume Number: 15 (1999)
Issue Number: 6
Column Tag: Tips & Tidbits

Jun 99 Tips

by Jeff Clites, online@mactech.com

Conversion from Intel Byte Order

Intel microprocessors store integer data in a different way than the PowerPC does. The PowerPC (operating under the Mac OS) stores the bytes in big endian order, from the most significant to the least significant, while the Intel processor uses the opposite byte order (little endian).

When you are using a Macintosh but have to read a file created on a computer with an Intel processor, or when you have to write a file for later use on an Intel machine, this difference is something you have to worry about. You can use the following routines to convert shorts and longs back and forth between the two byte orders.

#define MotorolaToIntelLong IntelToMotorolaLong
#define MotorolaToIntelShort IntelToMotorolaShort
/* Prototypes */
unsigned long IntelToMotorolaLong (unsigned long original);
unsigned short IntelToMotorolaShort 
							(unsigned short original);
/* Intel to Motorola bytes order */
/* Little endian to big endian   */
unsigned long IntelToMotorolaLong (unsigned long original)
{
	unsigned long converted;
	char *b;
	converted = 0L;
	if (original == 0) return converted;
	b = (char*)&converted;
	*b++ = (char)(original);
	*b++ = (char)(original >> 8);
	*b++ = (char)(original >> 16);
	*b++ = (char)(original >> 24);
	return converted;
}

/* Intel to Motorola bytes order */
/* Little endian to big endian   */
unsigned short IntelToMotorolaShort 
							(unsigned short original)
{
	unsigned short converted;
	char *b;
	converted = 0;
	if (original == 0) return converted;
	b = (char *)&converted;
	*b++ = (char)(original);
	*b++ = (char)(original >> 8);
	return converted;
}

Marco Bambini
marco@spiderlink.it

C++ File Streams and FSSpecs

When using ANSI C and C++ file routines, Macintosh programmers run into the unfortunate snag that these routines assume that files are properly identified by path names, but under the Mac OS they are specified through an FSSpec data structure. Of course, it is possible to obtain the full path to a file given its FSSpec, but this isn't really a solution, because on the Macintosh full path names don't uniquely identify files (for instance, if the user has more than one mounted disk with the same name).

Fortunately, Metrowerks has provided FSp_fopen, which works like C's fopen except that it uses FSSpecs instead of paths. To similarly obtain a C++ file stream you have to do a little more work, but the following example from Metrowerks' MWRon <MWRon@metrowerks.com> demonstrates how to do it with minimal fuss.

#include <iostream>
#include <fstream>
#include <SIOUX.h>
#include <unistd.h>
#include <cstring>
using namespace std;
int GetStream(FSSpec &fs, ifstream &in);
void Initialize(void);
const int PSIZE = 256;
char AppPathName[PSIZE];
char FilePathName[PSIZE];
int main()
{
	// initialize your managers
	Initialize();
	SIOUXSettings.initializeTB = FALSE;
	ifstream in;  // input file
	strcpy(AppPathName , "");		// safety
	strcpy(FilePathName , "");	// safety
	const short kNilFilterProc = nil;	// use dialog to open file
	StandardFileReply  reply;
	short numTypes = 0;
	SFTypeList typeList;
	FSSpec fs;
 	StandardGetFile( kNilFilterProc, numTypes, typeList, &reply);
	fs = reply.sfFile;
 	if(GetStream(fs, in)) exit(1); // stream failed to open
 	strcat(FilePathName, p2cstr(fs.name) );
 	c2pstr((char *)fs.name); 
	// see where we are at now
	cout << "The folder holding the executable is \n" 
 		<< getcwd(AppPathName, PSIZE) << endl;
 	cout << "The opened file's full name is \n" 
 		<< FilePathName << endl;
  	
	return 0;
}
 
int GetStream(FSSpec &fs, ifstream &in)
{
	short savedVol;
	OSErr err = 0;

	// get volume of application
	if ( (err = GetVol(0, &savedVol)) != NULL)
		return err;

	// set working volume	
	if ((err = HSetVol(0, fs.vRefNum, fs.parID)) != NULL)
		return err;
	
	// open file 
	in.open(p2cstr(fs.name));
	if(!in) { err = 1; return err;}
	c2pstr((char *)fs.name);
	getcwd(FilePathName, PSIZE);   // get working directory	
	// reset volume of application
	if( (err = SetVol(0, savedVol)) != NULL)
		return err;
	
	return err;
}

void Initialize(void)
{
	/* Initialize all the needed managers. */
	InitGraf(&qd.thePort);
	InitFonts();
	InitWindows();
	InitMenus();
	TEInit();
	InitDialogs(nil);
	InitCursor();
}
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Combo Quest (Games)
Combo Quest 1.0 Device: iOS Universal Category: Games Price: $.99, Version: 1.0 (iTunes) Description: Combo Quest is an epic, time tap role-playing adventure. In this unique masterpiece, you are a knight on a heroic quest to retrieve... | Read more »
Hero Emblems (Games)
Hero Emblems 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: ** 25% OFF for a limited time to celebrate the release ** ** Note for iPhone 6 user: If it doesn't run fullscreen on your device... | Read more »
Puzzle Blitz (Games)
Puzzle Blitz 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Puzzle Blitz is a frantic puzzle solving race against the clock! Solve as many puzzles as you can, before time runs out! You have... | Read more »
Sky Patrol (Games)
Sky Patrol 1.0.1 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0.1 (iTunes) Description: 'Strategic Twist On The Classic Shooter Genre' - Indie Game Mag... | Read more »
The Princess Bride - The Official Game...
The Princess Bride - The Official Game 1.1 Device: iOS Universal Category: Games Price: $3.99, Version: 1.1 (iTunes) Description: An epic game based on the beloved classic movie? Inconceivable! Play the world of The Princess Bride... | Read more »
Frozen Synapse (Games)
Frozen Synapse 1.0 Device: iOS iPhone Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Frozen Synapse is a multi-award-winning tactical game. (Full cross-play with desktop and tablet versions) 9/10 Edge 9/10 Eurogamer... | Read more »
Space Marshals (Games)
Space Marshals 1.0.1 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.1 (iTunes) Description: ### IMPORTANT ### Please note that iPhone 4 is not supported. Space Marshals is a Sci-fi Wild West adventure taking place... | Read more »
Battle Slimes (Games)
Battle Slimes 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: BATTLE SLIMES is a fun local multiplayer game. Control speedy & bouncy slime blobs as you compete with friends and family.... | Read more »
Spectrum - 3D Avenue (Games)
Spectrum - 3D Avenue 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: "Spectrum is a pretty cool take on twitchy/reaction-based gameplay with enough complexity and style to stand out from the... | Read more »
Drop Wizard (Games)
Drop Wizard 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: Bring back the joy of arcade games! Drop Wizard is an action arcade game where you play as Teo, a wizard on a quest to save his... | Read more »

Price Scanner via MacPrices.net

Apple’s M4 Mac minis on sale for record-low p...
B&H Photo has M4 and M4 Pro Mac minis in stock and on sale right now for up to $150 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses. Prices start at only $469: – M4... Read more
Deal Alert! Mac Studio with M4 Max CPU on sal...
B&H Photo has the standard-configuration Mac Studio model with Apple’s M4 Max CPU in stock today and on sale for $300 off MSRP, now $1699 (10-Core CPU and 32GB RAM/512GB SSD). B&H also... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.