home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / Weather / Weather.app / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-09  |  5.2 KB  |  251 lines

  1. /*
  2.  * A potpourri of handy little utilities.
  3.  *
  4.  * Michael Hawley
  5.  * MIT Media Laboratory
  6.  * 20 Ames St
  7.  * Cambridge, MA 02139
  8.  * mike@media-lab.mit.edu
  9.  * Copyright (c) MIT Media Laboratory 1991
  10.  */
  11. #include "util.h"
  12. #include <sys/stat.h>
  13.  
  14. char *av0;
  15.  
  16. error(a,b,c,d,e,f) int a,b,c,d,e,f; { /* printf an error msg */
  17.     printf((char *)a,b,c,d,e,f); printf("\n");
  18. }
  19.  
  20. int Verbose=0;  /* make it 1 to send copious diganostics to the console */
  21.  
  22. debug(a,b,c,d) int a,b,c,d; { /* printf an error msg */
  23.     if (Verbose) printf((char *)a,b,c,d), printf("\n");
  24. }
  25.  
  26. System(fmt, a,b,c,d,e,f,g)
  27.     char *fmt,*a,*b,*c,*d,*e,*f,*g;
  28. /*
  29.  * "printf" a system call, gripe if it failed.
  30.  */
  31. {
  32.     char t[2048];
  33.     int i=0;
  34.     sprintf(t,fmt,a,b,c,d,e,f,g);
  35.     debug("!%s",t);
  36.     if (i = system(t)) error("%s: failed! %s",av0,t);
  37.     return !i;
  38. }
  39.  
  40. char *
  41. save(s) char *s; { /* save a copy of 's' and return the pointer */
  42.     char *t = (char *)malloc(strlen(s)+1);
  43.     if (t) strcpy(t,s);
  44.     return t;
  45. }
  46.  
  47. blank(s) char *s; { /* true if 's' is blank */
  48.     while (*s == ' ' || *s=='\t' || *s == '\n') ++s;
  49.     return !*s;
  50. }
  51.  
  52. stripcomment(s) char *s; { /* truncate 's' at a comment character ('#') */
  53.     char *p = index(s,'#');
  54.     if (p && (p==s || p[-1] != '\\')) *p = '\0';
  55. }
  56.  
  57. #ifdef 0
  58. stripnl(s) char *s; { /* remove trailing \n and space from 's' */
  59.     char *p = s + strlen(s)-1;
  60.     while (p>s && (*p=='\n' || *p == ' ')) *p-- = '\0';
  61. }
  62. #endif
  63.  
  64. char *
  65. skipsp(s) char *s; {
  66.     while (*s==' ' || *s=='\t' || *s == '\n') ++s;
  67.     return s;
  68. }
  69.  
  70. squishblank(s) char *s; {
  71.     char *t=s, *start=s;
  72.     while (*t = *s){
  73.         if (t>s && (*t==' ' || *t=='\t') && (t[-1]==' '||t[-1]=='\t'))
  74.             ++s;
  75.         else
  76.             ++t, ++s;
  77.     }
  78. }
  79.  
  80. squishwhite(s) char *s; {
  81.     char *t=s, *start=s;
  82.     while (*t = *s){
  83.         if (t>s && (*t==' ' || *t=='\t' || *t=='\n') && 
  84.                    (t[-1]==' '||t[-1]=='\t'||t[-1]=='\n'))
  85.             ++s;
  86.         else
  87.             ++t, ++s;
  88.     }
  89. }
  90.  
  91. char *
  92. prefix(s,t) char *s, *t; { /* true if 't' is a prefix of 's' */
  93.     int sl = strlen(s);
  94.     t = skipsp(t);
  95.     return (*t == *s && strncmp(s,t,sl)==0)? skipsp(t+sl) : (char *)0;
  96. }
  97.  
  98. suffix(s,t) char *s, *t; { /* true if 't' is a suffix of 's' */
  99.     s = rindex(s,'.');
  100.     return s? strcmp(s,t)==0 : 0;
  101. }
  102.  
  103. char *
  104. strindex(s,t) char *s, *t; { /* return ptr to first match of 't' in 's' */
  105.     int n = strlen(t);
  106.  
  107.     if (s)
  108.         while (*s)
  109.             if (!strncmp(s, t, n))
  110.                 return s;
  111.             else
  112.                 s++;
  113.     return (char *)0;
  114. }
  115.  
  116. sub(s,a,b) char *s,a,b; { /* in 's', s/a/b/g */
  117.     while (*s){
  118.         if (*s==a) *s=b;
  119.         s++;
  120.     }
  121. }
  122.  
  123. substr(s,a,b) char *s, *a, *b; { /* like 'sub', but with strings */
  124.     char q[8192];
  125.     char *p = s;
  126.     int n = strlen(a);
  127.     for (;*p;p++){
  128.         if (*p == *a && strncmp(p,a,n)==0){
  129.             strcpy(q,p+n);
  130.             strcpy(p,b);
  131.             strcpy(p+strlen(b),q);
  132.             p += strlen(b)-1;
  133.         }
  134.     }
  135. }
  136.  
  137. stot(s,t,c) char *s, **t, c; { /* split 's' into a table 't' */
  138.     int i;
  139.     char *p;
  140.     while (p = index(s,c)){
  141.         *p++ = '\0';
  142.         *t++ = s;
  143.         s = p;
  144.     }
  145.     *t++ = s;
  146.     *t = (char *)0;
  147. }
  148.  
  149. #include <sys/stat.h>
  150.  
  151. int fMode(f) char *f; { /* return mode bits */
  152.     struct stat b;
  153.     return stat(f,&b)? 0 : b.st_mode;
  154. }
  155.  
  156. int fSize(f) char *f; { /* return mode bits */
  157.     struct stat b;
  158.     return stat(f,&b)? 0 : b.st_size;
  159. }
  160.  
  161. int fTime(f) char *f; { /* mod time of file 'f' */
  162.     struct stat b;
  163.     return stat(f,&b)? 0 : b.st_mtime;
  164. }
  165.  
  166. int fDirectory(f) char *f; { /* true if file 'f' is a directory */
  167.     struct stat b;
  168.     return stat(f,&b)? 0 : ((b.st_mode & S_IFDIR) == S_IFDIR);
  169. }
  170.  
  171. int fLink(f) char *f; { /* true if file 'f' is a symbolic link */
  172.     struct stat b;
  173.     return lstat(f,&b)? 0 : ((b.st_mode & S_IFLNK) == S_IFLNK);
  174. }
  175.  
  176. mkdirs(s,mode) char *s; { /* ensure that all the directories in 's' exist */
  177.     char t[4096], *p = t;
  178.  
  179.     strcpy(t,s);
  180.     if (*p=='/') ++p;
  181.     while (p = index(p,'/')) {
  182.     *p = '\0';
  183.         if (access(t,0)){
  184.             if (mkdir(t,mode))
  185.                 return 0;
  186.         /* fixmod(t); */
  187.     }
  188.         *p++ = '/';
  189.     }
  190.     if (access(t,0) && mkdir(t,mode)) return 0;
  191.     /* fixmod(t); */
  192.     debug("mkdir %s",t);
  193.     return 1;
  194. }
  195.  
  196. #ifdef ultrix
  197.  
  198. /* Ultrix version */
  199.  
  200. char *re_comp();
  201.  
  202. match(s,expr)
  203.     char *s, *expr;
  204. /*
  205.  * true if 's' matches regular expression 'expr'
  206.  * (a simple regex, grep-style, *not* egrep)
  207.  */
  208. {
  209.     static char p[256]="", *q=(char *)0;
  210.     static struct regex *r = (struct regex *)0;
  211.     if (q != expr){
  212.         if (strcmp(p,expr)){
  213.             if (r) free(r);
  214.             if (re_comp(expr))
  215.                 ;
  216.             else return 0;
  217.             strcpy(p,expr);
  218.         }
  219.         q = expr;
  220.     }
  221.     return re_exec(s,r)==1? 1: 0;
  222. }
  223. #else
  224.  
  225. /* NeXT version */
  226.  
  227. #include <regex.h>
  228.  
  229. match(s,expr)
  230.     char *s, *expr;
  231. /*
  232.  * true if 's' matches regular expression 'expr'
  233.  * (a simple regex, grep-style, *not* egrep)
  234.  */
  235. {
  236.     static char p[256]="", *q=(char *)0;
  237.     static struct regex *r = (struct regex *)0;
  238.     if (q != expr){
  239.         if (strcmp(p,expr)){
  240.             if (r) free(r);
  241.             if (r=re_compile(expr,0))
  242.                 ;
  243.             else return 0;
  244.             strcpy(p,expr);
  245.         }
  246.         q = expr;
  247.     }
  248.     return r? re_match(s,r)==1 : 0;
  249. }
  250. #endif
  251.