• MacTech Network:
  • Tech Support
  • |
  • MacForge.net
  • |
  • Apple News
  • |
  • Register Domains
  • |
  • SSL Certificates
  • |
  • iPod Deals
  • |
  • Mac Deals
  • |
  • Mac Book Shelf

MAC TECH

  • Home
  • Magazine
    • About MacTech in Print
    • Issue Table of Contents
    • Subscribe
    • Risk Free Sample
    • Back Issues
    • MacTech DVD
  • Archives
    • MacTech Print Archives
    • MacMod
    • MacTutor
    • FrameWorks
    • develop
  • Forums
  • News
    • MacTech News
    • MacTech Blog
    • MacTech Reviews and KoolTools
    • Whitepapers, Screencasts, Videos and Books
    • News Scanner
    • Rumors Scanner
    • Documentation Scanner
    • Submit News or PR
    • MacTech News List
  • Store
  • Apple Expo
    • by Category
    • by Company
    • by Product
  • Job Board
  • Editorial
    • Submit News or PR
    • Writer's Kit
    • Editorial Staff
    • Editorial Calendar
  • Advertising
    • Benefits of MacTech
    • Mechanicals and Submission
    • Dates and Deadlines
    • Submit Apple Expo Entry
  • User
    • Register for Ongoing Raffles
    • Register new user
    • Edit User Settings
    • Logout
  • Contact
    • Customer Service
    • Webmaster Feedback
    • Submit News or PR
    • Suggest an article
  • Connect Tools
    • MacTech Live Podcast
    • RSS Feeds
    • Twitter

ADVERTISEMENT
Volume Number:12
Issue Number:7
Column Tag:Tips & Tidbits

Tips & Tidbits

By Steve Sisak

Get Version Number String

Here’s a useful function that gets a version number string, suitable for display in a splash screen or an About box, directly from your 'vers' resource.

GetVersNumString is a handy function that reads an application’s 'vers' resource, constructs the version number, and returns it in a Pascal string. It may not be the most efficient way to do this, but it does the job. This function will not include the third piece of the version number if it equals zero (i.e., print “1.1” not “1.1.0”). The if statement controlling this decision is easy to remove; more ambitious programmers might make it an optional parameter. ;-)

I’ve also included Str255-copy and Str255-concatenation routines to make this tip complete, just in case you don’t already have your own available.

Michael Trent

/* GetVersNumString
 * Reads 'vers' resource #1 and constructs a string like “\p1.0d3”...
 * If it can't find 'vers' #1, it returns an empty string.
 * NOTE: The application resource file must be the active resource file!
 */
void GetVersNumString(Str255 versStr)
{
 Handle versHdl;
 long   version;
 unsigned char   ver1,ver2,ver3,relStatus,prerelNum;
 Str255 tmp;

    // clear string;
 versStr[0]=0;

[Actually, you should switch to the application resource file and use Get1Resource() here - sgs ]

    // read version no. information
 versHdl = GetResource('vers',1);
 if (!versHdl) {
 return;
 }
 version = *((long *)(*versHdl));
 ReleaseResource(versHdl);

    // Set ver1-3, relStatus, prerelNum from the version info.
    // Note that the first two bytes are in an unusual format.
 ver1 = ((char *)&version)[0];
 ver1 = (((ver1 & 0xF0) >> 4) * 10) + (ver1 & 0x0F);
 ver2 = (((char *)&version)[1] & 0xF0) >> 4;
 ver3 = (((char *)&version)[1] & 0x0F);
 relStatus = ((char *)&version)[2];
 prerelNum = ((char *)&version)[3];

    // Insert v1 and v2 into our version string.
 NumToString((long)ver1,tmp);
 PStringCat(versStr,tmp);
 PStringCat(versStr,"\p.");
 NumToString((long)ver2,tmp);
 PStringCat(versStr,tmp);
    // For convenience, we only print the third number if it is non-zero.
    // If you always want all three numbers, remove the if-statement.
 if (ver3) {
 PStringCat(versStr,"\p.");
 NumToString((long)ver3,tmp);
 PStringCat(versStr,tmp);
 }

    // If the release status is development, alpha, or beta, add a
    // ‘d’, ‘a’, or ‘b’ to our version string.
 switch(relStatus){
 case 0x20:      // development
 PStringCat(versStr,"\pd");
 break;
 case 0x40:      // alpha
 PStringCat(versStr,"\pa");
 break;
 case 0x60:      // beta
 PStringCat(versStr,"\pb");
 break;
 default:
 ;
 }

    // lastly, if we’ve added a ‘d’, ‘a’, or ‘b’, print the pre-release
    // number at the end.
 if (relStatus != 0x80) {
 NumToString((long)prerelNum,tmp);
 PStringCat(versStr,tmp);
 }
}

/* PStringCopy
 * Copy pascal string a into b.
 */
void PStringCopy(Str255 a, Str255 b)
{
 BlockMoveData(a,b, a[0] + 1);
}

/* PStringCat
 * Concatenate pascal strings a & b, return in a. If there isn’t enough
 * room in our array to join both strings, return what we can.
 */
void PStringCat(Str255 a, Str255 b)
{
 short len;

 if ((a[0] + b[0]) > 255)
 len = 255-a[0];
 else
 len = b[0];

 BlockMoveData(&(b[1]),&(a[a[0]+1]),len);
 a[0] += len;
}

 
MacTech Only Search:
Community Search:

 
 
 

 
 
 
 
 
  • SPREAD THE WORD:
  • Slashdot
  • Digg
  • Del.icio.us
  • Reddit
  • Newsvine
  • Generate a short URL for this page:



MacTech Magazine. www.mactech.com
Toll Free 877-MACTECH, Outside US/Canada: 805-494-9797
MacTech is a registered trademark of Xplain Corporation. Xplain, "The journal of Apple technology", Apple Expo, Explain It, MacDev, MacDev-1, THINK Reference, NetProfessional, Apple Expo, MacTech Central, MacTech Domains, MacNews, MacForge, and the MacTutorMan are trademarks or service marks of Xplain Corporation. Sprocket is a registered trademark of eSprocket Corporation. Other trademarks and copyrights appearing in this printing or software remain the property of their respective holders.
All contents are Copyright 1984-2010 by Xplain Corporation. All rights reserved. Theme designed by Icreon.
 
Nov. 20: Take Control of Syncing Data in Sow Leopard' released
Nov. 19: Cocktail 4.5 (Leopard Edition) released
Nov. 19: macProVideo offers new Cubase tutorials
Nov. 18: S Stardom anounces Safe Capsule, a companion piece for Apple's
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live
Nov. 17: Ableton releases Max for Live