home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / EMBL Search / Sources / util.c < prev    next >
Encoding:
Text File  |  1992-05-04  |  22.3 KB  |  562 lines  |  [TEXT/KAHL]

  1. h"
  2.  
  3. /*
  4. ******************************* Prototypes ***************************
  5. */
  6.  
  7. #include "util.h"
  8. #include "pstr.h"
  9. #include "events.h"
  10. #include "window.h"
  11.  
  12. /*
  13. ******************************* Global variables *********************
  14. */
  15.  
  16. extern CursHandle    gCursor[8];
  17. extern Boolean gHasAliasMgr;
  18. extern Boolean gHasFSSpec;
  19. extern short gAppResRef;                            /* Application rsrc file ref num */
  20. extern Boolean gInBackground;
  21.  
  22. char gError[256];
  23. Boolean bErrorOccurred = FALSE;
  24.  
  25. static short lgCurCursor=0;
  26. static long    gCursorTime;
  27.  
  28.  
  29. /****************************** Byte manipulation utilities *******************
  30. *    ISO format is least significant byte first, Mac format is reversed
  31. *    order.
  32. */
  33.  
  34. /**************************************
  35. *    Converts ISO 4-byte integer "val" to Mac long integer.
  36. *    Return value:    the value of "val" in Mac format
  37. *    Side-effect:    "val" becomes converted to Mac format
  38. */
  39.  
  40. u_long ConvertLong(u_long *val)
  41. {
  42.     register u_byte temp;
  43.     
  44.     temp=((u_byte *)val)[0];
  45.     ((u_byte *)val)[0]=((u_byte *)val)[3];
  46.     ((u_byte *)val)[3]=temp;
  47.     
  48.     temp=((u_byte *)val)[1];
  49.     ((u_byte *)val)[1]=((u_byte *)val)[2];
  50.     ((u_byte *)val)[2]=temp;
  51.     
  52.     return(*val);
  53. }
  54.  
  55.  
  56. /**************************************
  57. *    Converts ISO 2-byte integer to Mac integer. in place and also
  58. *    Return value:    the value of "val" in Mac format
  59. *    Side-effect:     "val" becomes converted to Mac format
  60. */
  61.  
  62. u_short ConvertShort(u_short *val)
  63. {
  64.     register u_byte temp;
  65.     
  66.     temp=((u_byte *)val)[0];
  67.     *val <<= 8;
  68.     ((u_byte *)val)[1]=temp;
  69.     
  70.     return(*val);
  71. }
  72.  
  73.  
  74. /******************************* String manipulation utilities *******************/
  75.  
  76.  
  77. /**************************************
  78. *    Converts a string "str" to uppercase.
  79. *    Return value:    Pointer to the converted string
  80. *    Side-effect:     the converted string in "str"
  81. */
  82.  
  83. char * str2upper(char *str)
  84. {
  85.     register char *s = str;
  86.     
  87.     if( !str )
  88.         return(NULL);
  89.         
  90.     while( *s = toupper(*s) )
  91.         ++s;
  92.         
  93.     return( str );
  94. }
  95.  
  96.  
  97. /**************************************
  98. *    Removes trailing blanks from a string "str".
  99. *    Return value:    Pointer to the converted string
  100. *    Side-effect:     the converted string in "str"
  101. */
  102.  
  103. char * rtrim(char *str)
  104. {
  105.     register short p;
  106.     
  107.     if (!str || *str == EOS)
  108.         return(str);
  109.         
  110.     p=strlen(str)-1;
  111.     while ( p >= 0 && isspace(str[p]) )
  112.         --p;
  113.         
  114.     str[p + 1] = EOS;
  115.     
  116.     return( str );
  117. }
  118.  
  119. /**************************************
  120. *    Removes leading blanks from a string "str".
  121. *    Return value:    Pointer to the converted string
  122. *    Side-effect:     the converted string in "str"
  123. */
  124.  
  125. char * ltrim(char *str)
  126. {
  127.     register char *p;
  128.     
  129.     if (!str || *str == EOS)
  130.         return(str);
  131.         
  132.     p = str;
  133.     while(*p && isspace(*p))
  134.         ++p;
  135.         
  136.     if(p != str)
  137.         strcpy(str,p);
  138.     
  139.     return( str );
  140. }
  141.  
  142. /**************************************
  143. *    Pads "str" to "len" characters with "c". "str" must be long enough to hold "len"
  144. *  characters!!!
  145. *    Return value:    Pointer to the converted string
  146. *    Side-effect:     the converted string in "str"
  147. */
  148.  
  149. char * rpad(char *str,char c,short len)
  150. {
  151.     register short i;
  152.     
  153.     for(i=strlen(str); i < len; ++i)
  154.             str[i] = c;
  155.     str[len] = EOS;
  156.  
  157.     return(str);
  158. }
  159.  
  160.  
  161. /**************************************
  162. *    Removes all blanks from "str".
  163. *    Return value:    Pointer to converted string
  164. *    Side-effect:     the converted string in "str"
  165. */
  166.  
  167. char * compress(char *str)
  168. {
  169.     register char *s,*t=str;
  170.     
  171.     for(s=str;*s;++s)
  172.         if( !isspace(*s) )
  173.             *str++ = *s;
  174.  
  175.     *str=EOS;
  176.     return(t);
  177. }
  178.  
  179. /**************************************
  180. *    Checks for EMBL-style line type identifiers
  181. *    Return value:    TRUE, if line starts with "id",
  182.                         FALSE, if not
  183. */
  184.  
  185. Boolean linetype(char *line, char *id)
  186. {
  187.     return( strncmp(line,id,strlen(id)) == 0);
  188. }
  189.  
  190.  
  191. /************************* File handling utilities *********************************/
  192.  
  193.  
  194. /**************************************
  195. *    This routine is used to center an SFPutFile/SFGetFile dialog
  196. *    Return value: 
  197. */
  198.  
  199. void CenterSFDlg(short which, Point *where)
  200. {
  201.     DialogTHndl    myHandle;
  202.     short            width, height;
  203.     short            mBarHeight=GetMBarHeight();            /* subtract menu bar height */
  204.     
  205.     /* get the resource and calculate width and height */    
  206.     myHandle=(DialogTHndl)GetResource('DLOG',which);
  207.     if(myHandle == NULL) {
  208.         /* set default values acc. to IM-I */
  209.         if(which = putDlgID) {
  210.             height = 184;  width = 304;
  211.         }
  212.         else {
  213.             height = 200; width = 384;
  214.         }
  215.     }
  216.     
  217.     else {
  218.         height=(**myHandle).boundsRect.bottom-(**myHandle).boundsRect.top;
  219.         width=(**myHandle).boundsRect.right-(**myHandle).boundsRect.left;
  220.     }
  221.     
  222.     where->v=((screenBits.bounds.bottom-screenBits.bounds.top)-height)/2 + mBarHeight;
  223.     where->h=((screenBits.bounds.right-screenBits.bounds.left)-width)/2;
  224. }
  225.  
  226. /**************************************
  227. *    Delete a Mac file
  228. *    Return value:    noErr, if successful
  229. *                        OS error code, if error occurred
  230. */
  231.  
  232. OSErr DeleteMacFile(StringPtr fName, short vRefNum)
  233. {
  234.     OSErr err;
  235.     
  236.     err=MyResolveAlias(fName,&vRefNum);
  237.     err=FSDelete(fName,vRefNum);
  238.     
  239.     return(err);
  240. }
  241.  
  242.  
  243. /**************************************
  244. *    Create a new Mac file
  245. *    Return value:    noErr, if successful
  246. *                        OS error code, if error occurred
  247. */
  248.  
  249. OSErr CreateMacFile(StringPtr fName, short vRefNum, OSType creator, OSType type,
  250.                           Boolean bShowErrMsg)
  251. {
  252.     OSErr err;
  253.     
  254.     /* Delete any old version */
  255.     DeleteMacFile(fName,vRefNum);
  256.     
  257.     if( (err=Create(fName,vRefNum,creator,type)) != noErr && bShowErrMsg) {
  258.         sprintf(gError,LoadErrorStr(ERR_CREATEFILE,FALSE),PtoCstr(fName),err);
  259.         CtoPstr((char *)fName);
  260.        ErrorMsg(0);
  261.     }
  262.     return(err);
  263. }
  264.  
  265.  
  266. /**************************************
  267. *    Open a Mac file
  268. *    Return value:    noErr, if successful
  269. *                        OS error code, if error occurred
  270. *    Side-effect:    file id in "output"
  271. */
  272.  
  273. OSErr OpenMacFile(StringPtr fName, short vRefNum, short *output, Boolean bShowErrMsg)
  274. {
  275.     OSErr err;
  276.     
  277.     if( (err=MyResolveAlias(fName,&vRefNum)) == noErr ) {
  278.         err=FSOpen(fName,vRefNum,output);
  279.     }
  280.         
  281.     if( err != noErr && bShowErrMsg) {
  282.         sprintf(gError,LoadErrorStr(ERR_OPENFILE,FALSE),PtoCstr(fName),err);
  283.        CtoPstr((char *)fName);
  284.           ErrorMsg(0);
  285.     }
  286.  
  287.     return(err);
  288. }
  289.  
  290. /**************************************
  291. *    Open a Mac file in READONLY mode.
  292. *    This is useful for reading in the index file so that they canbe moved to a shared
  293. *    disk.
  294. *    Return value:    noErr, if successful
  295. *                        OS error code, if error occurred
  296. *    Side-effect:    file id in "output"
  297. */
  298.  
  299. OSErr OpenMacFileReadOnly(StringPtr fName, short vRefNum, short *output,
  300.         Boolean bShowErrMsg)
  301. {
  302.     OSErr err;
  303.     ParamBlockRec myPB;
  304.     
  305.     if( (err=MyResolveAlias(fName,&vRefNum)) == noErr ) {
  306.         myPB.ioParam.ioNamePtr = fName;
  307.         myPB.ioParam.ioVRefNum = vRefNum;
  308.         myPB.ioParam.ioVersNum = 0;
  309.         myPB.ioParam.ioPermssn = fsRdPerm;
  310.         myPB.ioParam.ioMisc = NULL;
  311.         
  312.         err=PBOpen(&myPB,FALSE);
  313.         *output = myPB.ioParam.ioRefNum;
  314.     }
  315.         
  316.     if( err != noErr && bShowErrMsg) {
  317.         sprintf(gError,LoadErrorStr(ERR_OPENFILE,FALSE),PtoCstr(fName),err);
  318.        CtoPstr((char *)fName);
  319.           ErrorMsg(0);
  320.     }
  321.  
  322.     return(err);
  323. }
  324.  
  325.  
  326. /**************************************
  327. *    Write to a Mac file
  328. *    Return value:    noErr, if successful
  329. *                        OS error code, if error occurred
  330. *    Side-effect:     number of bytes written successfully in "count"
  331. */
  332.  
  333. OSErr WriteMacFile(short output, long *count, void *what, StringPtr fName,
  334.                         Boolean bShowErrMsg)
  335. {
  336.     OSErr err;
  337.     
  338.     if( (err=FSWrite(output, count, what)) != noErr  && bShowErrMsg) {
  339.         sprintf(gError,LoadErrorStr(ERR_WRITEFILE,FALSE),PtoCstr(fName),err);
  340.       CtoPstr((char *)fName);
  341.        ErrorMsg(0);
  342.      }
  343.      
  344.      return(err);
  345. }
  346.  
  347.  
  348. /**************************************
  349. *    Read from a Mac file
  350. *    Return value:    noErr, if successful
  351. *                        OS error code, if error occurred
  352. *    Side-effect:    number of bytes read successfully in "count"
  353. */
  354.  
  355. OSErr ReadMacFile(short input, long *count, void *what, StringPtr fName,
  356.                         Boolean bShowErrMsg)
  357. {
  358.     OSErr err;
  359.     
  360.     if( (err=FSRead(input, count, what)) != noErr && bShowErrMsg) {
  361.         sprintf(gError,LoadErrorStr(ERR_READFILE,FALSE),PtoCstr(fName),err);
  362.       CtoPstr((char *)fName);
  363.        ErrorMsg(0);
  364.      }
  365.      
  366.      return(err);
  367. }
  368.  
  369.  
  370. /**************************************
  371. *    Converts a System 7 FSSpec to System 6 HFS specification
  372. *    Return value:    noErr, if successful
  373. *                        OS error code, if error occurred
  374. */
  375.  
  376. OSErr FSSpecToHFS(FSSpec *theFSS,short *wdRefNum,StringPtr fName)
  377. {
  378.     pstrcpy(fName,theFSS->name);
  379.     return( OpenWD(theFSS->vRefNum,theFSS->parID,kApplSignature,wdRefNum) );
  380. }
  381.                 
  382.  
  383. /**************************************
  384. *    Ask user for new file name
  385. *    Return value:    TRUE, if successful
  386. *                        FALSE, if error occurred
  387. *    Side-effect:    new filename in newFName
  388. */
  389.  
  390. Boolean GetNewFilename(Str255 oldFName,short *newVRefNum,Str255 newFName)
  391. {
  392.     SFReply    reply;
  393.     Point        where;
  394.     Str255    prompt;
  395.     
  396.     /* Deactivate current front window. SFPut/GetFile sends an activate event
  397.         when it quits but not when it opens ! */
  398.     HandleActivates(FrontWindow(),0);
  399.     GetIndString(prompt,OTHERS,PROMPTSTR);
  400.     where.h=((screenBits.bounds.right-screenBits.bounds.left)-304)/2;
  401.     where.v=((screenBits.bounds.bottom-screenBits.bounds.top)-184)/2+20;
  402.     SFPutFile(where, prompt, oldFName, NULL,&reply);
  403.     
  404.     if (reply.good) {
  405.         pstrcpy(newFName,reply.fName);
  406.         *newVRefNum = reply.vRefNum;
  407.         return(TRUE);
  408.     }
  409.     else return(FALSE);
  410. }
  411.  
  412. /**************************************
  413. *    Get real vRefNum from volume name (without colon!)
  414. */
  415.  
  416. Boolean GetVRefNumFromName(StringPtr vName, short *vRefNum)
  417. {
  418.     ParamBlockRec myPB;
  419.     Str255 ioName;
  420.     OSErr ret;
  421.     
  422.     pstrcpy(ioName,vName);
  423.     pstrcat(ioName,"\p:");
  424.     
  425.     myPB.volumeParam.ioNamePtr = ioName;
  426.     myPB.volumeParam.ioVRefNum = 0x8000;
  427.     myPB.volumeParam.ioVolIndex = -1;
  428.     
  429.     if((ret = PBGetVInfo(&myPB,FALSE)) == noErr)
  430.         *vRefNum = myPB.volumeParam.ioVRefNum;
  431.         
  432.     return(ret == noErr);
  433. }
  434.  
  435. /**************************************
  436. *    Get volume name from real vRefNum
  437. */
  438.  
  439. Boolean GetNameFromVRefNum(StringPtr vName, short vRefNum)
  440. {
  441.     ParamBlockRec myPB;
  442.     Str255 ioName;
  443.     OSErr ret;
  444.     
  445.     *vName = 0;
  446.     
  447.     myPB.volumeParam.ioNamePtr = vName;
  448.     myPB.volumeParam.ioVRefNum = vRefNum;
  449.     myPB.volumeParam.ioVolIndex = 0;
  450.     
  451.     return( (ret = PBGetVInfo(&myPB,FALSE)) == noErr);
  452. }
  453.  
  454.  
  455. /**************************************
  456. *    Resolve aliases if Alias Manager and new File Manager calls are available.
  457. *    Otherwise simply return fName and wdRefNum unchanged.
  458. *    Return value:    noErr, if successful
  459. *                        OS error code, if error occurred
  460. */
  461.  
  462. OSErr MyResolveAlias(StringPtr fName, short *wdRefNum)
  463. {
  464.     OSErr err;
  465.     FSSpec theFSS;
  466.     Boolean targetIsFolder,wasAliased;
  467.     
  468.     err=noErr;
  469.     if(gHasAliasMgr && gHasFSSpec) {
  470.         /* convert HFS to FSSpec */
  471.         err=FSMakeFSSpec(*wdRefNum,0L,fName,&theFSS);
  472.         /* resolve alias */
  473.         if(err == noErr) {
  474.             err=ResolveAliasFile(&theFSS,TRUE,&targetIsFolder,&wasAliased);
  475.             if(targetIsFolder)
  476.                 err=paramErr;    /* cannot open a folder */
  477.         }
  478.         /* convert FSSpec back to HFS */
  479.         if(err == noErr)
  480.             err=FSSpecToHFS(&theFSS,wdRefNum,fName);
  481.     }
  482.     return(err);
  483. }
  484.  
  485.  
  486. /********************************** General utilities *****************************/
  487.  
  488. /**************************************
  489. *    Changes GrafPort
  490. *    Return value: previous GrafPort
  491. */
  492.  
  493. GrafPtr ChangePort(GrafPtr newPort)
  494. {
  495.     GrafPtr oldPort;
  496.     
  497.     GetPort(&oldPort);
  498.     SetPort(newPort);
  499.     
  500.     return(oldPort);
  501. }
  502.  
  503. /**************************************
  504. *    Moves a relocatable block high up in memory (to prevent memory
  505. *    fragmentation) and locks it.
  506. *    Return value:    Old status of handle flags
  507. */
  508.  
  509. SignedByte LockHandleHigh(Handle theHandle)
  510. {
  511.     SignedByte    hstate;
  512.  
  513.     hstate = HGetState(theHandle);
  514.     HLockHi(theHandle);
  515.     return(hstate);
  516. }
  517.  
  518. /**************************************
  519. *    Locks a relocatable block.
  520. *    Return value:    Old status of handle flags
  521. */
  522.  
  523. SignedByte MyHLock(Handle theHandle)
  524. {
  525.     SignedByte    hstate;
  526.  
  527.     hstate = HGetState(theHandle);
  528.     HLock(theHandle);
  529.     return(hstate);
  530. }
  531.  
  532. #define TICK_PERIOD 120
  533.  
  534. /*************************************
  535. *    Draw first of our wait cursors
  536. */
  537.  
  538. void StartWaitCursor()
  539. {
  540.     SetCursor(*gCursor[lgCurCursor=0]);
  541.     gCursorTime = TickCount()+TICK_PERIOD;
  542.  
  543. }
  544.  
  545.  
  546. /*************************************
  547. *    Rotate wait cursor. Every TICK_PERIOD/60 seconds we change it. This way we
  548. *    are somewhat independent of machine speed.
  549. */
  550.  
  551. void RotateWaitCursor()
  552. {
  553.     if(TickCount() >= gCursorTime) {
  554.         gCursorTime += TICK_PERIOD;                /* approx. every two seconds */
  555.         ++lgCurCursor;
  556.         if(lgCurCursor == 8) lgCurCursor = 0;
  557.         SetCursor(*gCursor[lgCurCursor]);
  558.     }
  559. }
  560.  
  561.  
  562. /***************************** Dialog handling utilities *