Back to index

lightning-sunbird  0.9+nobinonly
nsMacUtils.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 
00041 #include <stdlib.h>
00042 
00043 #include "nsMacUtils.h"
00044 
00045 
00046 OSErr MyNewHandle(long length, Handle *outHandle)
00047 {
00048   Handle    tempHandle = NewHandle(length);
00049   if (!tempHandle) return memFullErr;
00050   *outHandle = tempHandle;
00051   return noErr;
00052 }
00053 
00054 
00055 void MyDisposeHandle(Handle theHandle)
00056 {
00057   if (theHandle)
00058     DisposeHandle(theHandle);
00059 }
00060 
00061 
00062 void MyHLock(Handle theHandle)
00063 {
00064   HLock(theHandle);
00065 }
00066 
00067 void MyHUnlock(Handle theHandle)
00068 {
00069   HUnlock(theHandle);
00070 }
00071 
00072 #pragma mark -
00073 
00074 OSErr MyNewBlockClear(long length, void* *outBlock)
00075 {
00076   void* newData = calloc(length, 1);
00077   if (!newData) return memFullErr;
00078   *outBlock = newData;
00079   return noErr;
00080 }
00081 
00082 void MyDisposeBlock(void *dataBlock)
00083 {
00084   if (dataBlock)
00085     free(dataBlock);
00086 }
00087 
00088 #pragma mark -
00089 
00090 /*----------------------------------------------------------------------------
00091        StrCopySafe 
00092        
00093        strcpy which checks destination buffer length
00094                      
00095 ----------------------------------------------------------------------------*/
00096 void StrCopySafe(char *dst, const char *src, long destLen)
00097 {
00098        const unsigned char  *p = (unsigned char *) src - 1;
00099        unsigned char               *q = (unsigned char *) dst - 1;
00100        unsigned char        *end = (unsigned char *)dst + destLen;
00101        
00102        while ( q < end && (*++q = (*++p)) != '\0' ) {;}
00103        if (q == end)
00104               *q = '\0';
00105 }
00106 
00107 /*----------------------------------------------------------------------------
00108        CopyPascalString 
00109        
00110        Copy a pascal format string.
00111                      
00112        Entry: to = pointer to destination string.
00113                      from = pointer to source string.
00114 ----------------------------------------------------------------------------*/
00115 
00116 void CopyPascalString (StringPtr to, const StringPtr from)
00117 {
00118        BlockMoveData(from, to, *from+1);
00119 }
00120 
00121 
00122 /*----------------------------------------------------------------------------
00123        CopyCToPascalString 
00124        
00125        Copy from a C string to a Pascal string, up to maxLen chars.
00126 ----------------------------------------------------------------------------*/
00127 
00128 void CopyCToPascalString(const char *src, StringPtr dest, long maxLen)
00129 {
00130        char          *s = (char *)src;
00131        char          *d = (char *)(&dest[1]);
00132        long          count = 0;
00133               
00134        while (count < maxLen && *s) {
00135               *d ++ = *s ++;
00136               count ++;
00137        }
00138        
00139        dest[0] = (unsigned char)(count);
00140 }
00141 
00142 
00143 /*----------------------------------------------------------------------------
00144        CopyPascalToCString 
00145        
00146        Copy from a C string to a Pascal string, up to maxLen chars.
00147 ----------------------------------------------------------------------------*/
00148 
00149 void CopyPascalToCString(const StringPtr src, char *dest, long maxLen)
00150 {
00151        short  len = Min(maxLen, src[0]);
00152        
00153        BlockMoveData(&src[1], dest, len);
00154        dest[len] = '\0';
00155 }
00156 
00157 
00158 
00159 /*----------------------------------------------------------------------------
00160        GetShortVersionString
00161        
00162        Extracts the version number string from the specified 'vers' resource and
00163        concats it onto the end of the specified string (Pascal-style). The short
00164        version number string is extracted.
00165        
00166        Exit:  function result = error code.
00167                      *versionNumber = version number.
00168 ----------------------------------------------------------------------------*/
00169 
00170 OSErr GetShortVersionString(short rID, StringPtr version)
00171 {
00172        VersRecHndl          versionH;
00173        OSErr                error = resNotFound;
00174        
00175        versionH = (VersRecHndl)Get1Resource('vers', rID);             
00176        
00177        if (versionH)
00178        {
00179               CopyPascalString(version, (**versionH).shortVersion);
00180               ReleaseResource((Handle) versionH);
00181               error = noErr;
00182        }
00183        else
00184               CopyPascalString(version, (StringPtr)"\p<unknown>");
00185 
00186        return error;
00187 }
00188 
00189