bc_util.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of CONCORDE                                           */
00004 /*                                                                          */
00005 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
00006 /*  Vasek Chvatal, and William Cook                                         */
00007 /*                                                                          */
00008 /*  Permission is granted for academic research use.  For other uses,       */
00009 /*  contact the authors for licensing options.                              */
00010 /*                                                                          */
00011 /*  Use at your own risk.  We make no guarantees about the                  */
00012 /*  correctness or usefulness of this code.                                 */
00013 /*                                                                          */
00014 /****************************************************************************/
00015 
00016 /****************************************************************************/
00017 /*                                                                          */
00018 /*               MISCELLANEOUS UTILITY ROUTINES                             */
00019 /*                                                                          */
00020 /*                           TSP CODE                                       */
00021 /*                                                                          */
00022 /*  Written by:  Applegate, Bixby, Chvatal, and Cook                        */
00023 /*  Date: October 12, 1995                                                  */
00024 /*  Date: September 28, 1997                                                */
00025 /*        April 7, 2003 (bico)                                              */
00026 /*                                                                          */
00027 /*    EXPORTED FUNCTIONS:                                                   */
00028 /*                                                                          */
00029 /*  unsigned int CCutil_nextprime (unsigned int x)                          */
00030 /*    FINDS the smallest positive prime >= x                                */
00031 /*                                                                          */
00032 /*  int CCutil_our_gcd (int a, int b)                                       */
00033 /*    COMPUTES gcd(a,b)                                                     */
00034 /*    -gcd(a,b) is always >= 0                                              */
00035 /*    -a and b can be negative, positive, or zero                           */
00036 /*                                                                          */
00037 /*  int CCutil_our_lcm (int a, int b)                                       */
00038 /*    COMPUTES lcm(a,b)                                                     */
00039 /*    -lcm(a,b) is always >= 0                                              */
00040 /*    -a and b can be negative, positive, or zero                           */
00041 /*                                                                          */
00042 /*  char *CCutil_strchr (char *s, int c)                                    */
00043 /*    RETURNS a pointer to the first occurrence of c in s, or NULL if c     */
00044 /*    does not occur in s                                                   */
00045 /*                                                                          */
00046 /*  char *CCutil_strrchr (char *s, int c)                                   */
00047 /*    RETURNS a pointer to the last occurrence of c in s, or NULL if c      */
00048 /*    does not occur in s                                                   */
00049 /*                                                                          */
00050 /*  const char *CCutil_strchr_c (const char *s, int c)                      */
00051 /*    RETURNS a pointer to the first occurrence of c in s, or NULL if c     */
00052 /*    does not occur in s.  A variant for const strings.                    */
00053 /*                                                                          */
00054 /*  const char *CCutil_strrchr_c (const char *s, int c)                     */
00055 /*    RETURNS a pointer to the last occurrence of c in s, or NULL if c      */
00056 /*    does not occur in s.  A variant for const strings.                    */
00057 /*                                                                          */
00058 /*  char *CCutil_strdup (const char *s)                                     */
00059 /*    RETURNS a pointer to a copy of s, allocated with CC_SAFE_MALLOC,      */
00060 /*    or NULL if unable to allocate space for the string                    */
00061 /*                                                                          */
00062 /*  char *CCutil_strdup2 (const char *s)                                    */
00063 /*    RETURNS a pointer to a copy of s up until the first whitespace,       */
00064 /*    allocated with CC_SAFE_MALLOC, or NULL if unable to allocate space    */
00065 /*    for the string.                                                       */
00066 /*                                                                          */
00067 /*  void CCutil_readstr (FILE *f, char *s, int len)                         */
00068 /*    READS a string from f into s.  The string is terminated by a          */
00069 /*    whitespace character (space, tab, newline) or EOF.  The entire        */
00070 /*    string including the terminating character is read, but only at       */
00071 /*    most len characters are stored in s, including the terminating        */
00072 /*    NULL.                                                                 */
00073 /*                                                                          */
00074 /*  void CCutil_printlabel (void)                                           */
00075 /*    PRINTS information identifying a machine                              */
00076 /*                                                                          */
00077 /*  int CCutil_print_command (int ac, char **av)                            */
00078 /*    PRINTS the command line                                               */
00079 /*                                                                          */
00080 /****************************************************************************/
00081 
00082 #include "bc_machdefs.h"
00083 #include "bc_macrorus.h"
00084 #include "bc_util.h"
00085 extern int gethostname (char *,
00086                         size_t);
00087 
00088 
00089 static int isprime (unsigned int x);
00090 
00091 
00092 unsigned int CCutil_nextprime (unsigned int x)
00093 {
00094   if (x < 3)
00095     return 3;
00096   x |= 1;
00097   while (!isprime (x))
00098     x += 2;
00099   return x;
00100 }
00101 
00102 static int isprime (unsigned int p)
00103 {
00104   unsigned int i;
00105 
00106   if ((p & 1) == 0)
00107     return 0;
00108   for (i = 3; i * i <= p; i += 2)
00109   {
00110     if (p % i == 0)
00111       return 0;
00112   }
00113   return 1;
00114 }
00115 
00116 int CCutil_our_gcd (int a,
00117                     int b)
00118 {
00119   int c;
00120 
00121   if (a < 0)
00122     a = -a;
00123   if (b < 0)
00124     b = -b;
00125   if (a > b)
00126     CC_SWAP (a, b, c);
00127 
00128   while (a)
00129   {
00130     c = b % a;
00131     b = a;
00132     a = c;
00133   }
00134   return b;
00135 }
00136 
00137 int CCutil_our_lcm (int a,
00138                     int b)
00139 {
00140   int c;
00141 
00142   if (a < 0)
00143     a = -a;
00144   if (b < 0)
00145     b = -b;
00146 
00147   c = CCutil_our_gcd (a, b);
00148 
00149   return (a / c) * b;
00150 }
00151 
00152 char *CCutil_strchr (char *s,
00153                      int c)
00154 {
00155   while (*s)
00156   {
00157     if (*s == c)
00158       return s;
00159     s++;
00160   }
00161   return (char *) NULL;
00162 }
00163 
00164 const char *CCutil_strchr_c (const char *s,
00165                              int c)
00166 {
00167   while (*s)
00168   {
00169     if (*s == c)
00170       return s;
00171     s++;
00172   }
00173   return (char *) NULL;
00174 }
00175 
00176 char *CCutil_strrchr (char *s,
00177                       int c)
00178 {
00179   char *l = (char *) NULL;
00180 
00181   while (*s)
00182   {
00183     if (*s == c)
00184       l = s;
00185     s++;
00186   }
00187   return l;
00188 }
00189 
00190 const char *CCutil_strrchr_c (const char *s,
00191                               int c)
00192 {
00193   const char *l = (char *) NULL;
00194 
00195   while (*s)
00196   {
00197     if (*s == c)
00198       l = s;
00199     s++;
00200   }
00201   return l;
00202 }
00203 
00204 char *CCutil_strdup (const char *s)
00205 {
00206   char *p = CC_SAFE_MALLOC (strlen (s) + 1, char);
00207 
00208   if (p == (char *) NULL)
00209   {
00210     fprintf (stderr, "Out of memory in CCutil_strdup\n");
00211     return (char *) NULL;
00212   }
00213   strcpy (p, s);
00214   return p;
00215 }
00216 
00217 char *CCutil_strdup2 (const char *s)
00218 {
00219   char *p;
00220   int len;
00221 
00222   for (len = 0; s[len]; len++)
00223   {
00224     if (s[len] == ' ' || s[len] == '\r' || s[len] == '\t' || s[len] == '\n')
00225     {
00226       break;
00227     }
00228   }
00229 
00230   p = CC_SAFE_MALLOC (len + 1, char);
00231 
00232   if (p == (char *) NULL)
00233   {
00234     fprintf (stderr, "Out of memory in CCutil_strdup2\n");
00235     return (char *) NULL;
00236   }
00237   strncpy (p, s, (unsigned) len);
00238   p[len] = '\0';
00239 
00240   return p;
00241 }
00242 
00243 void CCutil_readstr (FILE * f,
00244                      char *s,
00245                      int len)
00246 {
00247   int c;
00248 
00249   while ((c = getc (f)) != EOF && c != ' ' && c != '\t' && c != '\r' &&
00250          c != '\n')
00251   {
00252     if (len > 1)
00253     {
00254       *s++ = c;
00255       len--;
00256     }
00257   }
00258   *s = '\0';
00259 }
00260 
00261 void CCutil_printlabel (void)
00262 {
00263 #ifdef CC_NETREADY
00264   char buf[1024];
00265 
00266   gethostname (buf, 1024);
00267   printf ("Host: %s  Current process id: %d\n", buf, (int) getpid ());
00268   fflush (stdout);
00269 #else
00270   printf ("No label - need function to non-NETREADY machines\n");
00271   fflush (stdout);
00272 #endif
00273 }
00274 
00275 int CCutil_print_command (int ac,
00276                           char **av)
00277 {
00278   int rval = 0;
00279   int i,
00280     cmdlen = 0;
00281   char *cmdout = (char *) NULL;
00282 
00283   for (i = 0; i < ac; i++)
00284   {
00285     cmdlen += strlen (av[i]) + 1;
00286   }
00287   cmdout = CC_SAFE_MALLOC (cmdlen, char);
00288   CCcheck_NULL (cmdout, "out of memory in print_command");
00289 
00290   cmdlen = 0;
00291   for (i = 0; i < ac; i++)
00292   {
00293     strcpy (cmdout + cmdlen, av[i]);
00294     cmdlen += strlen (av[i]);
00295     cmdout[cmdlen] = ' ';
00296     cmdlen++;
00297   }
00298   cmdout[cmdlen - 1] = '\0';
00299   printf ("%s\n", cmdout);
00300   fflush (stdout);
00301 
00302 CLEANUP:
00303 
00304   CC_IFFREE (cmdout, char);
00305   return rval;
00306 }

Generated on Thu Oct 20 14:58:40 2005 for DominoParitySeparator by  doxygen 1.4.5