Main Page | Data Structures | Directories | File List | Data Fields | Globals

lclib.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------- *
00002  * screen.c
00003  * This file is part of lincity.
00004  * Lincity is copyright (c) I J Peters 1995-1997, (c) Greg Sharp 1997-2001.
00005  * Portions copyright (c) Corey Keasling, 2001.
00006  * ---------------------------------------------------------------------- */
00007 
00008 #include "lcconfig.h"
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "lcstring.h"
00012 #include "cliglobs.h"
00013 #include "lcintl.h"
00014 
00015 char* 
00016 current_month (int current_time)
00017 {
00018     return _(months[(current_time % NUMOF_DAYS_IN_YEAR) / NUMOF_DAYS_IN_MONTH]);
00019 }
00020 
00021 int 
00022 current_year (int current_time)
00023 {
00024     return current_time / NUMOF_DAYS_IN_YEAR;
00025 }
00026 
00027 void 
00028 format_number5 (char* str, int num)
00029 {
00030     int num_sign = num >= 0 ? 1 : -1;
00031     if (num_sign == 1) {
00032         if (num < 99999) {
00033             sprintf (str, "%5d", num);
00034         } else if (num < 9999999) {
00035             sprintf (str, "%4dK", num / 1000);
00036         } else {
00037             sprintf (str, "%4dM", num / 1000000);
00038         }
00039     } else {
00040         int num_absval = num_sign * num;
00041         if (num_absval < 9999) {
00042             sprintf (str, "%5d", num);
00043         } else if (num_absval < 999999) {
00044             sprintf (str, "%4dK", num_sign * (num_absval / 1000));
00045         } else {
00046             sprintf (str, "%4dM", num_sign * (num_absval / 1000000));
00047         }
00048     }
00049 }
00050 
00051 void 
00052 num_to_ansi(char * s, size_t size, long num) 
00053 {
00054   int triplets = 0;
00055   float numf = (float)num;  
00056 
00057   while (numf > 1000) {
00058     numf /= 1000;
00059     triplets++;
00060   }
00061 
00062   switch(triplets) 
00063     {
00064     case 0: triplets = ' '; break;
00065     case 1: triplets = 'k'; break; // kila
00066     case 2: triplets = 'm'; break; // mega
00067     case 3: triplets = 'g'; break; // giga 
00068     case 4: triplets = 't'; break; // tera 
00069     case 5: triplets = 'p'; break; // peta
00070     default: triplets = '?'; break;
00071     }
00072       
00073   if (size == 4)  /* to make up for format_pos_number4.  Eeewwwwwww. */ 
00074     if (numf < 10) 
00075       snprintf(s, size + 1, "%1.1f%c", numf, triplets);
00076     else 
00077       snprintf(s,size + 1, "%3.0f%c", numf, triplets);
00078   else
00079     snprintf(s, size, "%3.1f%c", numf, triplets);
00080 }
00081 
00082 void 
00083 num_to_ansi_unit(char * s, size_t size, long num, char unit) 
00084 {
00085   int triplets = 0;
00086   float numf = (float)num;
00087 
00088   while (numf > 1000) {
00089     numf /= 1000;
00090     triplets++;
00091   }
00092 
00093   switch(triplets) 
00094     {
00095     case 0: triplets = ' '; break;
00096     case 1: triplets = 'k'; break; // kila
00097     case 2: triplets = 'm'; break; // mega
00098     case 3: triplets = 'g'; break; // giga 
00099     case 4: triplets = 't'; break; // tera 
00100     case 5: triplets = 'p'; break; // peta
00101     default: triplets = '?'; break;
00102     }
00103       
00104   if (size == 4)  /* to make up for format_pos_number4 */
00105     if (numf < 10) 
00106       snprintf(s, size, "%4.1f%c%c", numf, triplets, unit);
00107     else 
00108       snprintf(s,size, "%4.0f%c%c", numf, triplets, unit);
00109   else
00110     snprintf(s, size, "%5.1f%c%c", numf, triplets, unit);
00111 }
00112 
00113 /* commify: take a number and convert it to a string grouped into triplets
00114    with commas; returns number of characters written, excluding trailing zero
00115 */
00116 int 
00117 commify (char *str, size_t size, int argnum)
00118 {
00119     size_t count = 0;
00120     int i = 0;
00121     int triad = 1;
00122     int num = argnum;
00123     int kludge = 1;
00124 
00125     if (num < 0) 
00126         count += snprintf(str, size, "-");
00127 
00128     num = abs(argnum);
00129     
00130     for (; num >= 1000; num /= 1000, triad++, kludge *= 1000);
00131 
00132     num = abs(argnum);
00133 
00134     for (; triad > 0; i++, triad--) {
00135 
00136         if (i == 0) 
00137             if (triad == 1)
00138                 count += snprintf(str + count, size - count, "%d", num);
00139             else
00140                 count += snprintf(str + count, size - count, "%d,", 
00141                                   num ? num / kludge : num);
00142         else if (triad == 1)
00143             count += snprintf(str + count, size - count, "%03d", 
00144                               num ? num / kludge : num);
00145         else
00146             count += snprintf(str + count, size - count, "%03d,",
00147                               num ? num / kludge : num);
00148 
00149         if (num) /* don't divide by zero */
00150             num %= kludge;
00151 
00152         kludge /= 1000;
00153     }
00154 
00155     return count;
00156 }
00157 
00158 /* GCS - make sure that the string has length at least size-1 */
00159 void
00160 pad_with_blanks (char* str, int size)
00161 {
00162   while (*str) {
00163     size--;
00164     str++;
00165   }
00166   while (size-- > 1) {
00167     *str++ = ' ';
00168   }
00169   *str = '\0';
00170 }
00171 
00172 void 
00173 format_pos_number4 (char* str, int num)
00174 {
00175   num_to_ansi(str, 4, num);
00176 }
00177 
00178 void 
00179 format_power(char * str, size_t size, long power)
00180 {
00181   num_to_ansi_unit(str, size, power, 'w');
00182 }
00183 
00184 int
00185 min_int (int i1, int i2)
00186 {
00187   return i1 < i2 ? i1 : i2;
00188 }
00189 
00190 int
00191 max_int (int i1, int i2)
00192 {
00193   return i1 > i2 ? i1 : i2;
00194 }
00195 
00196 void * 
00197 lcalloc (size_t size) 
00198 {
00199   void * tmp;
00200   tmp = malloc(size);
00201   if (tmp == NULL) {
00202     printf("couldn't malloc %d bytes!  Dying.\n",size);
00203     exit(-1);
00204   }
00205   
00206   return tmp;
00207 }

Generated on Sun Dec 26 11:23:24 2004 for lincity by  doxygen 1.3.9.1