/[ascend]/trunk/base/generic/compiler/dimen_io.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/dimen_io.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 708 - (show annotations) (download) (as text)
Tue Jun 27 07:34:31 2006 UTC (17 years, 10 months ago) by johnpye
File MIME type: text/x-csrc
File size: 3134 byte(s)
Replaced some references to ascmalloc with ASC_NEW_ARRAY
1 /*
2 * Dimensions Output Routine
3 * by Tom Epperly
4 * Created: 2/14/90
5 * Version: $Revision: 1.8 $
6 * Version control file: $RCSfile: dimen_io.c,v $
7 * Date last modified: $Date: 1998/04/11 01:31:06 $
8 * Last modified by: $Author: ballan $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along with
25 * the program; if not, write to the Free Software Foundation, Inc., 675
26 * Mass Ave, Cambridge, MA 02139 USA. Check the file named COPYING.
27 */
28 #include <utilities/ascConfig.h>
29 #include <utilities/ascMalloc.h>
30 #include <general/dstring.h>
31 #include "compiler.h"
32 #include "fractions.h"
33 #include "dimen.h"
34 #include "dimen_io.h"
35
36 #ifndef lint
37 static CONST char DimenIORCSid[] = "$Id: dimen_io.c,v 1.8 1998/04/11 01:31:06 ballan Exp $";
38 #endif
39
40 static
41 void WriteFrac(FILE *f, struct fraction frac, CONST char *str, int *CONST p)
42 {
43 if (Numerator(frac)){
44 if (*p) PUTC('*',f);
45 (*p) = 1;
46 if (Denominator(frac)==1) {
47 FPRINTF(f,"%s^%d",str,Numerator(frac));
48 } else {
49 FPRINTF(f,"%s^(%d/%d)",str,Numerator(frac),Denominator(frac));
50 }
51 }
52 }
53
54 void WriteDimensions(FILE *f, CONST dim_type *dimp)
55 {
56 struct fraction frac;
57 int printed=0;
58 if (IsWild(dimp)) {
59 FPRINTF(f,"*");
60 } else {
61 int i;
62 for( i=0; i<NUM_DIMENS; i++ ) {
63 frac = GetDimFraction(*dimp,i);
64 WriteFrac(f,frac,DimName(i),&printed);
65 }
66 if (!printed) FPRINTF(f,"dimensionless");
67 }
68 }
69
70 char *WriteDimensionString(CONST dim_type *p)
71 {
72 Asc_DString ds, *dsPtr;
73 char *result;
74 int numseen = 0, i, k;
75 char expo[40];
76 if (p==NULL) {
77 return NULL;
78 }
79 if (IsWild(p)) {
80 result = ASC_NEW_ARRAY(char,2);
81 sprintf(result,"*");
82 return result;
83 }
84 dsPtr = &ds;
85 Asc_DStringInit(dsPtr);
86 for (i=0; i < NUM_DIMENS; i++) {
87 k = GetDimPower(*(p),i);
88 if (k > 0) {
89 if (numseen) {
90 Asc_DStringAppend(dsPtr,"*",1);
91 }
92 Asc_DStringAppend(dsPtr,DimName(i),-1);
93 if (k > 1) {
94 sprintf(expo,"^%d",k);
95 Asc_DStringAppend(dsPtr,expo,-1);
96 }
97 numseen = 1;
98 }
99 }
100 if (!numseen) {
101 Asc_DStringAppend(dsPtr,"1",1);
102 }
103 for (i=0; i < NUM_DIMENS; i++) {
104 k = GetDimPower(*(p),i);
105 if (k < 0) {
106 Asc_DStringAppend(dsPtr,"/",1);
107 Asc_DStringAppend(dsPtr,DimName(i),-1);
108 if (k < -1) {
109 sprintf(expo,"^%d",-k);
110 Asc_DStringAppend(dsPtr,expo,-1);
111 }
112 }
113 }
114 result = Asc_DStringResult(dsPtr);
115 return result;
116 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22