home *** CD-ROM | disk | FTP | other *** search
- /* General utilities. */
-
- #include <TCL>
- #include "GLOBAL.h"
- #include "CUtility.h"
- #include "CDiagnostic.h"
- #include "STR.h"
- #include "keys.h"
- #include "chars.h"
- #include <stdio.h>
-
- NEW void CUtility::IUtility()
- {
- }
-
- NEW StringPtr CUtility::ApplicationName()
- {
- int applRef;
- Handle h; /* I wonder what this is? */
- static Str255 result;
-
- GetAppParms(result, &applRef, &h);
- return result;
- }
-
- NEW Handle CUtility::MustNewHandle(Size size)
- {
- Handle han = NewHandle(size);
- CheckAllocation(han);
- return han;
- }
-
- NEW Ptr CUtility::MustNewPtr(Size size)
- {
- Ptr ptr = NewPtr(size);
- CheckAllocation(ptr);
- return ptr;
- }
-
- NEW Handle CUtility::MustGetResource(ResType type, int ID)
- {
- Handle han = GetResource(type, ID);
-
- CheckResource(han);
- return han;
- }
-
- /* BriefGetString() - so called because you'd better only need the string
- briefly (before the next call to it, in fact). */
-
- NEW StringPtr CUtility::BriefGetString(int rsrcID, int index)
- {
- static Str255 buffer;
-
- GetIndString(buffer, rsrcID, index);
- return buffer;
- }
-
- /* SetField and GetField. We view the patch data (editData handle) as a
- vector of bytes. However, some dialog items just affect a few bits of
- a byte (e.g. a single bit flag). Other dialog items don't start from
- zero (e.g. -50..+50 things). SetField and GetField are our general
- packing/unpacking routines. SetField takes an old byte value,
- a bit shift, min, max, and value [min..max], and sets the correct bits
- of the byte, guessing the bit width. GetField does the inverse. */
-
- LOCAL Byte mask(int bitShift, int range)
- {
- Byte b = 0;
- while (range != 0)
- {
- b |= (1<<bitShift);
- bitShift++;
- range >>= 1;
- }
-
- return b;
- }
-
- NEW int CUtility::GetField(Byte byte, int bitShift, int min, int max)
- {
- Byte m = mask(bitShift, max - min);
- int result;
-
- result = ((byte&m) >> bitShift) + min;
-
- /*diagnostic("GetField(byte=0x%02x, shift=%d, min=%d, max=%d) -> %d",
- byte, bitShift, min, max, result
- );*/
- return result;
- }
-
- NEW Byte CUtility::SetField(Byte byte, int bitShift, int min, int max, int value)
- {
- Byte m = mask(bitShift, max - min);
- Byte newByte;
-
- newByte = (byte & (~m)) | ((value-min) << bitShift);
-
- /*diagnostic(
- "GetField(byte=0x%02x, shift=%d, min=%d, max=%d, value=%d) -> 0x%02x",
- byte, bitShift, min, max, value, newByte
- );*/
-
- return newByte;
- }
-
- NEW StringPtr CUtility::FormatNumber(int num, Boolean withPlus)
- {
- static Str255 result;
-
- StringPtr p = result;
- Boolean hundreds = FALSE;
-
- if (num < 0) {
- *p++ = '-';
- num = -num;
- withPlus = FALSE;
- }
-
- if (withPlus)
- *p++ = '+';
-
- if (num >= 100) {
- *p++ = '0' + (num / 100);
- num %= 100;
- hundreds = TRUE;
- }
-
- if ((num >= 10) || hundreds) {
- *p++ = '0' + (num / 10);
- num %= 10;
- }
-
- *p++ = '0' + num;
- *p = '\0';
-
- CtoPstr((char *) result);
- return result;
- }
-
- NEW StringPtr CUtility::FormatNote(int note)
- {
- static Str255 result;
- int index = (note % 12) + 1;
-
- GetIndString(result, NOTENAMES_strs, index);
- ConcatPStrings(result, FormatNumber(note / 12 - 2, FALSE));
-
- return result;
- }
-
- NEW char *CUtility::ShowRoute(RouteRec route)
- {
- static char result[4];
-
- sprintf(result, "%c%d",
- (route.cable == MODEM)
- ? 'M'
- : (route.cable == PRINTER) ? 'P' : '-',
- route.channel
- );
-
- return result;
- }
-
- LOCAL Boolean keyIsDown(int index, int mask)
- {
- KeyMap keys;
- long L;
-
- GetKeys(&keys);
- L = keys.Key[index];
- /*diagnostic("L = %08lx, mask = %08lx", L, mask);*/
- return((L & mask) != 0L);
- }
-
- NEW Boolean CUtility::OptionKeyDown()
- {
- return(keyIsDown(WHICH_KEYWORD, OPTION_KEY));
- }
-
- NEW Boolean CUtility::CommandKeyDown()
- {
- return(keyIsDown(WHICH_KEYWORD, COMMAND_KEY));
- }
-
- /* GetFontAndSize() - looks up a string like "Monaco 12" in a STR# resource
- (with id FONTS_strs), and returns font name and size. */
- NEW void CUtility::GetFontAndSize(int index, StringPtr name, int *size)
- {
- Str255 buff;
- GetIndString(buff, FONTS_strs, index);
- PtoCstr((char *) buff);
- Assert("GetFontAndSize", sscanf((char *) buff, "%s %d", name, size) == 2);
- CtoPstr((char *) name);
- }
-
- /* Sanitise - deal with any characters >127 in a string, ready for putting
- into a MIDI bank. */
-
- NEW void CUtility::Sanitise(StringPtr buff)
- {
- int len = buff[0], i;
-
- for (i = 1; i <= len; i++)
- if (buff[i] & 0x80) buff[i] = HUH_char;
- }
-
- NEW void CUtility::Assert(char *text, Boolean condition)
- {
- if (!condition) g_Die("%s", text);
- }
-
- NEW void CUtility::HardCheckOSError(OSErr macErr)
- {
- if (macErr != noErr) gError->SevereMacError(macErr);
- }
-
- NEW void CUtility::HardCheckResError()
- {
- HardCheckOSError(ResError());
- }
-
- /* Notify() doesn't do anything with objects or the Toolbox, since we want
- to be able to call it from the MIDI Manager interrupt routines. According
- to TN184, we can call NM safely in such circumstances. (WRONG! it has
- bugs. C'est la vie.) g_Notify takes a STR# resource ID and index, and
- notifies the string, by grabbing it into a temporary buffer, putting it
- into a handle, and passing the handle. We therefore have to have a
- completion routine to dispose of the handle (which we pass in the refCon).
- */
-
- /* BE CAREFUL in myCompletion - A5 isn't valid. */
-
- static pascal void myCompletion(NMRec *nmReqPtr)
- {
- NMRemove(nmReqPtr);
- DisposPtr(nmReqPtr->nmStr);
- DisposPtr(nmReqPtr);
- }
-
- /* Note that the use of NewPtr() essentially wipes this out as an
- interrupt-level routine. */
-
- NEW void CUtility::Notify(int rsrcID, int index, int extra)
- {
- Str255 buffer1, buffer2;
- StringPtr ptr;
- int len;
- NMRec *note;
-
- note = (NMRec *) gUtility->MustNewPtr(sizeof(NMRec));
-
- GetIndString(buffer1, rsrcID, index);
- sprintf((char *) buffer2, "%#s: %#s [%d]",
- ApplicationName(), buffer1, extra
- );
- CtoPstr((char *) buffer2);
- ptr = (StringPtr) gUtility->MustNewPtr(buffer2[0] + 1);
- CopyPString(buffer2, ptr);
-
- note->qType = nmType;
- note->nmMark = 0;
- note->nmSIcon = NULL;
- note->nmSound = (Handle) -1; /* System Beep. */
- note->nmStr = ptr;
- note->nmResp = (ProcPtr) myCompletion;
-
- gUtility->HardCheckOSError(NMInstall(note));
- }
-
- NEW void CUtility::Report(int index)
- {
- gError->PostAlert(REPORT_strs, index);
- }
-
- /* g_Die - mostly pinched from CError::SevereMacError. It's a vanilla
- routine because methods can't take varargs. */
-
- GLOBAL void g_Die(format, a, b, c, d, e, f, g)
- char *format;
- {
- Str255 buffer;
-
- sprintf((char *) buffer, format, a, b, c, d, e, f, g);
-
- CtoPstr((char *) buffer);
- ParamText(buffer, "\p", "\p", "\p");
- PositionDialog('ALRT', ALRTsevereErr);
- InitCursor();
-
- if (StopAlert(ALRTsevereErr, NULL) == Cancel) {
- gApplication->Exit();
- ExitToShell();
- } else {
- HiliteMenu(NOTHING);
- gApplication->JumpToEventLoop();
- }
- }
-