Back to home page

MITgcm

 
 

    


File indexing completed on 2018-03-02 18:44:57 UTC

view on githubraw file Latest commit ec6cf3b0 on 2003-08-26 20:45:25 UTC
ec6cf3b09d Ed H*0001 /* macpcstr.c -- niftyapp library pascal/C combination strings
                0002  */
                0003 /* (C) Copyright 1995 by Carnegie Mellon University
                0004  * All Rights Reserved.
                0005  *
                0006  * Permission to use, copy, modify, distribute, and sell this software
                0007  * and its documentation for any purpose is hereby granted without
                0008  * fee, provided that the above copyright notice appear in all copies
                0009  * and that both that copyright notice and this permission notice
                0010  * appear in supporting documentation, and that the name of Carnegie
                0011  * Mellon University not be used in advertising or publicity
                0012  * pertaining to distribution of the software without specific,
                0013  * written prior permission.  Carnegie Mellon University makes no
                0014  * representations about the suitability of this software for any
                0015  * purpose.  It is provided "as is" without express or implied
                0016  * warranty.
                0017  *
                0018  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
                0019  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
                0020  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
                0021  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                0022  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
                0023  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                0024  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                0025  * SOFTWARE.
                0026  */
                0027 /* (C) Copyright 1990, 1991 by Christopher J. Newman
                0028  * All Rights Reserved.
                0029  *
                0030  * Permission to use, copy, modify, distribute, and sell this software and its
                0031  * documentation for any purpose is hereby granted without fee, provided that
                0032  * the above copyright notice appear in all copies and that both that
                0033  * copyright notice and this permission notice appear in supporting
                0034  * documentation, and that the name of Christopher J. Newman not be used in
                0035  * advertising or publicity pertaining to distribution of the software without
                0036  * specific, written prior permission.  Christopher J. Newman makes no
                0037  * representations about the suitability of this software for any purpose.  It
                0038  * is provided "as is" without express or implied warranty.
                0039  *
                0040  * CHRISTOPHER J. NEWMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
                0041  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
                0042  * SHALL CHRISTOPHER J. NEWMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
                0043  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
                0044  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                0045  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
                0046  * OF THIS SOFTWARE.
                0047  *
                0048  * Author:  Christopher J. Newman
                0049  * Message: This is a nifty program.
                0050  *
                0051  *  Created 9/1/88, Assembly Code 6/27/90
                0052  */
                0053 
                0054 #ifdef THINK_C
                0055 typedef unsigned char PCstr;
                0056 
                0057 /* assembler function prototypes */
                0058 void PtoPCstrcpy(void);
                0059 void CtoPCstrcpy(void);
                0060 void PCtoPCstrcpy(void);
                0061 void PtoPCstrncpy(void);
                0062 void CtoPCstrncpy(void);
                0063 void PtoPCstrcat(void);
                0064 void CtoPCstrcat(void);
                0065 PCstr *PtoPCstr(void);
                0066 PCstr *CtoPCstr(void);
                0067 void SetPlen(void);
                0068 PCstr *longtoPCstr(long);   /* not in assembler */
                0069 
                0070 void PtoPCstrcpy( /* PCstr *dest, *src */ )
                0071 {
                0072     asm 68000 {
                0073         movea.l 8(sp),a0        ; a0 = src
                0074         movea.l 4(sp),a1        ; a1 = dest
                0075         clr.w   d0
                0076         move.b  (a0),d0
                0077         clr.b   1(a1,d0)
                0078     @loop:
                0079         move.b  (a0)+,(a1)+
                0080         dbf.w   d0,@loop
                0081     }
                0082 }
                0083 
                0084 void CtoPCstrcpy( /* PCstr *dest, char *src */)
                0085 {
                0086     asm 68000 {
                0087         movea.l 8(sp),a0    ; a0 = src
                0088         movea.l 4(sp),a1    ; a1 = dest
                0089         addq.l  #1,a1
                0090         moveq.l #-1,d0
                0091     @loop:
                0092         addq.w  #1,d0
                0093         move.b  (a0)+,(a1)+
                0094         bne.s   @loop
                0095         movea.l 4(sp),a1    ; a1 = dest
                0096         move.b  d0,(a1)
                0097     }
                0098 }
                0099 
                0100 void PCtoPCstrcpy( /* PCstr *dest, PCstr *src */)
                0101 {
                0102     asm 68000 {
                0103         movea.l 8(sp),a0    ; a0 = src
                0104         movea.l 4(sp),a1    ; a1 = dest
                0105         move.b  (a0)+,(a1)+
                0106     @loop:
                0107         move.b  (a0)+,(a1)+
                0108         bne.s   @loop
                0109     }
                0110 }
                0111 
                0112 void PtoPCstrncpy( /* PCstr *dest, char *src, short n */)
                0113 {
                0114     asm 68000 {
                0115         movea.l 8(sp),a0    ; a0 = src
                0116         movea.l 4(sp),a1    ; a1 = dest
                0117         move.w  12(sp),d0   ; d0 = n
                0118         clr.w   d1
                0119         move.b  (a0)+,d1
                0120         cmp.w   d0,d1
                0121         bcc.s   @skip
                0122         move.w  d1,d0
                0123     @skip:
                0124         move.b  d0,(a1)+
                0125         subq.w  #1,d0
                0126         bcs.s   @exit
                0127     @loop:
                0128         move.b  (a0)+,(a1)+
                0129         dbf     d0,@loop
                0130     @exit:
                0131     }
                0132 }
                0133 
                0134 void CtoPCstrncpy( /* PCstr *dest, char *src, short n */ )
                0135 {
                0136     asm 68000 {
                0137         movea.l 8(sp),a0    ; a0 = src
                0138         movea.l 4(sp),a1    ; a1 = dest
                0139         addq.l  #1,a1
                0140         clr.w   d1
                0141         move.w  12(sp),d0   ; d0 = n
                0142         bra.s   @skip
                0143     @loop:
                0144         addq.w  #1,d1
                0145         move.b  (a0)+,(a1)+
                0146     @skip:
                0147         dbeq.w  d0,@loop
                0148         clr.b   (a1)
                0149         movea.l 4(sp),a1    ; a1 = dest
                0150         subq.w  #1,d1
                0151         move.b  d1,(a1)
                0152     }
                0153 }
                0154 
                0155 void PtoPCstrcat( /* PCstr *dest, char *src */ )
                0156 {
                0157     asm 68000 {
                0158         movea.l 8(sp),a0    ; a0 = src
                0159         movea.l 4(sp),a1    ; a1 = dest
                0160         clr.w   d0
                0161         clr.w   d1
                0162         move.b  (a0)+,d0
                0163         move.b  (a1),d1
                0164         add.b   d0,(a1)
                0165         lea.l   1(a1,d1),a1
                0166         bra.s   @skip
                0167     @loop:
                0168         move.b  (a0)+,(a1)+
                0169     @skip:
                0170         dbf.w   d0,@loop
                0171         clr.b   (a1)
                0172     }
                0173 }
                0174 
                0175 void CtoPCstrcat( /* PCstr *dest, char *src */ )
                0176 {
                0177     asm 68000 {
                0178         movea.l 8(sp),a0    ; a0 = src
                0179         movea.l 4(sp),a1    ; a1 = dest
                0180         clr.w   d0
                0181         move.b  (a1),d0
                0182         lea.l   1(a1,d0),a1
                0183         subq.w  #1,d0
                0184     @loop:
                0185         addq.w  #1,d0
                0186         move.b  (a0)+,(a1)+
                0187         bne.s   @loop
                0188         movea.l 4(sp),a1    ; a1 = dest
                0189         move.b  d0,(a1)
                0190     }
                0191 }
                0192 
                0193 PCstr *PtoPCstr( /* char *str */ )
                0194 {
                0195     asm 68000 {
                0196         movea.l 4(sp),a0    ; a0 = str
                0197         clr.w   d0
                0198         move.b  (a0),d0
                0199         clr.b   1(a0,d0)
                0200         move.l  a0,d0
                0201     }
                0202 }
                0203 
                0204 PCstr *CtoPCstr( /* char *str */)
                0205 {
                0206     asm 68000 {
                0207         movea.l 4(sp),a0    ; a0 = str
                0208         move.b  (a0)+,d0
                0209         lea.l   (a0),a1
                0210     @loop:
                0211         move.b  (a1),d1
                0212         move.b  d0,(a1)+
                0213         move.b  d1,d0
                0214         bne.s   @loop
                0215         move.b  d0,(a1)
                0216         suba.l  a0,a1
                0217         move.l  a1,d0
                0218         move.b  d0,-(a0)
                0219         move.l  a0,d0
                0220     }
                0221 }
                0222 
                0223 void SetPlen( /* PCstr *pcstr */ )
                0224 {
                0225     asm 68000 {
                0226         movea.l 4(sp),a0    ; a0 = str
                0227         lea.l   1(a0),a1
                0228         moveq.l #-1,d0
                0229     @loop:
                0230         addq.w  #1,d0
                0231     @skip:
                0232         tst.b   (a1)+
                0233         bne.s   @loop
                0234         move.b  d0,(a0)
                0235     }
                0236 }
                0237 #else
                0238 /* C function prototypes in mac_napp.h */
                0239 #include "macnapp.h"
                0240 
                0241 void PtoPCstrcpy(dest, src)
                0242     register PCstr  *dest;
                0243     register char   *src;
                0244 {
                0245     register short  i;
                0246     
                0247     i = Pstrlen(src);
                0248     C(dest)[i] = '\0';
                0249     do {
                0250         *dest++ = *src++;
                0251     } while (i--);
                0252 }
                0253 
                0254 void CtoPCstrcpy(dest, src)
                0255     register PCstr  *dest;
                0256     register char   *src;
                0257 {
                0258     register short  i;
                0259     register char   *cpy;
                0260 
                0261     cpy = C(dest);
                0262     for (i = 0; *cpy++ = *src++; i++);
                0263     *dest = i;
                0264 }
                0265 
                0266 void PCtoPCstrcpy(dest, src)
                0267     register PCstr  *dest;
                0268     register PCstr  *src;
                0269 {
                0270     *dest++ = *src++;
                0271     while (*dest++ = *src++);
                0272 }
                0273 
                0274 void PtoPCstrncpy(PCstr *dest, char *src, short n)
                0275 {
                0276     if (Pstrlen(src) < n) n = Pstrlen(src);
                0277     *dest++ = n;
                0278     src++;
                0279     while (n--) *dest++ = *src++;
                0280     *dest++ = '\0';
                0281 }
                0282 
                0283 void CtoPCstrncpy(PCstr *dest, char *src, short n)
                0284 {
                0285     register char   *tmp;
                0286     register short  i;
                0287     
                0288     tmp = C(dest);
                0289     for (i = 0; n-- && (*tmp++ = *src++); i++);
                0290     *tmp = '\0';
                0291     *dest = i;
                0292 }
                0293 
                0294 void PtoPCstrcat(dest, src)
                0295     register PCstr  *dest;
                0296     register char   *src;
                0297 {
                0298     register short  i;
                0299     register short  j;
                0300     
                0301     i = *dest;
                0302     *dest += (j = (unsigned char) *src++);
                0303     dest += i + 1;
                0304     while (j--) *dest++ = *src++;
                0305     *dest = '\0';
                0306 }
                0307 
                0308 void CtoPCstrcat(dest, src)
                0309     register PCstr  *dest;
                0310     register char   *src;
                0311 {
                0312     register short  i;
                0313     register char   *tmp;
                0314     
                0315     tmp = (char *) dest + (i = *dest) + 1;
                0316     while (*tmp++ = *src++) i++;
                0317     *dest = i;
                0318 }
                0319 
                0320 PCstr *PtoPCstr(str)
                0321     register char   *str;
                0322 {
                0323     SetClen((PCstr*) str);
                0324 
                0325     return ((PCstr*) str);
                0326 }
                0327 
                0328 PCstr *CtoPCstr(str)
                0329     register char   *str;
                0330 {
                0331     register PCstr  i;
                0332     register char   c, d;
                0333     register char   *tmp;
                0334     
                0335     i = 0;
                0336     tmp = str;
                0337     tmp++;
                0338     c = *tmp++;
                0339     do {
                0340         d = *tmp;
                0341         *tmp++ = c;
                0342         i++;
                0343     } while (c = d);
                0344     (*(PCstr*)str) = i;
                0345         
                0346     return ((PCstr*) str);
                0347 }
                0348 
                0349 void SetPlen(pcstr)
                0350     register PCstr  *pcstr;
                0351 {
                0352     register short  i = -1;
                0353     register char   *len = C(pcstr);
                0354     
                0355     do {
                0356         i++;
                0357     } while (*len++);
                0358     
                0359     *pcstr = i;
                0360 }
                0361 #endif
                0362 
                0363 /* simple procedure to convert decimal number of 
                0364  * less than 20 digits to a PC string.
                0365  * Compiling with 68020 option makes this quite a bit more efficient.
                0366  */
                0367 PCstr *longtoPCstr(i)
                0368     register long i;
                0369 {
                0370     static PCstr            sbuf[21];
                0371     register Boolean        negflag;
                0372     register unsigned long  val, ten = 10;
                0373     register PCstr          *pos = sbuf + sizeof (sbuf) - 1;
                0374     register PCstr          *posst;
                0375 
                0376     *pos = '\0';
                0377     posst = --pos;
                0378     negflag = false;
                0379     val = i;
                0380     if (i < 0) {
                0381         negflag = true;
                0382         val = -i;
                0383     }
                0384     do {
                0385         *pos = (unsigned short) (val % ten) + '0';
                0386         pos--;
                0387     } while (val /= ten);
                0388     if (negflag) {
                0389         *pos = '-';
                0390         pos--;
                0391     }
                0392     *pos = posst - pos;
                0393 
                0394     return (pos);
                0395 }
                0396