home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume36 / uqwk / part01 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-11  |  4.6 KB  |  241 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "uqwk.h"
  4.  
  5. /*
  6.  *  Miscellaneous uqwk routines
  7.  */
  8.  
  9. NewConference (name)
  10. char *name;
  11. {
  12.     struct conf_ent *cp, *tmp_cp;
  13.     char *c, ndx_fname[PATH_LEN];
  14.  
  15.     /* Get space for new conference */
  16.     if (NULL == (tmp_cp = (struct conf_ent *) malloc
  17.                     (sizeof (struct conf_ent))))
  18.     {
  19.         fprintf (stderr, "%s: out of memory\n", progname);
  20.         exit (0);
  21.     }
  22.  
  23.     /* Get space for name */
  24.     if (NULL == (c = (char *) malloc (1+strlen(name))))
  25.     {
  26.         fprintf (stderr, "%s: out of memory\n", progname);
  27.         exit (0);
  28.     }
  29.  
  30.     /* Fill in conference name */
  31.     tmp_cp->name = c;
  32.     strcpy (tmp_cp->name, name);
  33.  
  34.     /* Fill in conference number */
  35.     tmp_cp->number = conf_cnt;
  36.  
  37.     /* Add to end of conference list */
  38.     if (last_conf == NULL)
  39.     {
  40.         /* This is first conference */
  41.         conf_list = tmp_cp;
  42.     }
  43.     else
  44.     {
  45.         last_conf->next = tmp_cp;
  46.     }
  47.     tmp_cp->next = NULL;
  48.     last_conf = tmp_cp;
  49.  
  50.     /* Open new index file */
  51.     if (!strcmp (name, MAIL_CONF_NAME))
  52.     {
  53.         strcpy (ndx_fname, home_dir);
  54.         strcat (ndx_fname, "/");
  55.         strcat (ndx_fname, "personal.ndx");
  56.     }
  57.     else
  58.     {
  59.         sprintf (ndx_fname, "%s/%03d.ndx", home_dir, conf_cnt);
  60.     }
  61.  
  62.     if (NULL == (ndx_fd = fopen (ndx_fname, "w")))
  63.     {
  64.         fprintf (stderr, "%s: can't open %s\n", progname, ndx_fname);
  65.         exit (0);
  66.     }
  67.  
  68.     conf_cnt++;
  69. }
  70.  
  71. PadString (s, c, n)
  72. char *s, *c;
  73. int n;
  74. /*
  75.  *  Take a null-terminated string s and copy it, space-padded or
  76.  *  truncated if necessary, into field c of n characters
  77.  */
  78. {
  79.     int len, i;
  80.     len = strlen (s);
  81.     if (len >= n)
  82.     {
  83.         strncpy (c, s, n);
  84.     }
  85.     else
  86.     {
  87.         strcpy (c, s);
  88.         Spaces (&c[len], n-len);
  89.     }
  90. }
  91.  
  92. Spaces (c, n)
  93. char *c;
  94. int n;
  95. /*
  96.  *  Fill field of n characters with spaces
  97.  */
  98. {
  99.     int i;
  100.     for (i=0; i<n; i++) c[i]=' ';
  101. }
  102.  
  103. PadNum (i, c, n)
  104. int i, n;
  105. char *c;
  106. /*
  107.  *  Format an integer i and place it, space filled, in
  108.  *  field c of n characters
  109.  */
  110. {
  111.     sprintf (buf, "%d", i);
  112.     PadString (buf, c, n);
  113. }
  114.  
  115. IntNum (i, c)
  116. int i;
  117. char c[2];
  118. /*
  119.  *  Put binary integer i into two bytes
  120.  */
  121. {
  122.     c[0] = i % 256;
  123.     c[1] = (i / 256) % 256;
  124. }
  125.  
  126. char *Fgets (c, n, fd)
  127. char *c;
  128. int n;
  129. FILE *fd;
  130. /*
  131.  *  Same as fgets, but changes trailing linefeed to a null
  132.  */
  133. {
  134.     int i;
  135.  
  136.     if (NULL == fgets (c, n, fd)) return (NULL);
  137.     i = strlen (c);
  138.     if ( (i > 0) && (c[i-1]=='\n') ) c[i-1] = 0;
  139.  
  140.     return (c);
  141. }
  142.  
  143. inttoms (i, c)
  144. int i;
  145. char c[4];
  146. /*
  147.  *  Convert an integer into the Microsoft Basic floating format.
  148.  *  This is the dumbest thing in the whole QWK standard.  Why in
  149.  *  the world store block offsets as floating point numbers?
  150.  *  Stupid!
  151.  */
  152. {
  153.     int m, e;
  154.  
  155.     if (i == 0)
  156.     {
  157.         c[0] = c[1] = c[2] = 0;
  158.         c[3] = 0x80;
  159.         return;
  160.     }
  161.  
  162.     e = 152;
  163.     m = 0x7fffff & i;
  164.  
  165.     while (!(0x800000 & m))
  166.     {
  167.         m <<= 1;
  168.         e--;
  169.     }
  170.     c[0] = 0xff & m;
  171.     c[1] = 0xff & (m >> 8);
  172.     c[2] = 0x7f & (m >> 16);
  173.     c[3] = 0xff & e;
  174. }
  175.  
  176. int LastInt (line)
  177. char *line;
  178. /*
  179.  * Find last integer on line
  180.  */
  181. {
  182.         int i, last_delim, n;
  183.         n = strlen (line);
  184.  
  185.         /* Find last delimiter */
  186.         for (i=0; i<n; i++)
  187.         {
  188.                 if ( (line[i] == ' ') || (line[i] == '-') ||
  189.                      (line[i] == ',') || (line[i] == ':') )
  190.                         last_delim = i;
  191.         }
  192.  
  193.         i = atoi (&line[last_delim+1]);
  194.         return (i);
  195. }
  196.  
  197. ParseDate (c, hp)
  198. char *c;
  199. struct qwk_hdr *hp;
  200. {
  201.         char s[PATH_LEN];
  202.         int day, mon, year, hour, minute;
  203.         char month[4];
  204.  
  205.         /* Dates come in two flavors:  with the weekday, and without.
  206.            we simply look for the comma which follows the weekday */
  207.         if (c[3] == ',')
  208.         {
  209.                 sscanf (&c[4], "%d %s %d %d:%d", &day, month, &year,
  210.                                                 &hour, &minute);
  211.         }
  212.         else
  213.         {
  214.                 sscanf (c, "%d %s %d %d:%d", &day, month, &year,
  215.                                                 &hour, &minute);
  216.         }
  217.  
  218.         /* Convert alphabetic month name to integer */
  219.         if (!strncmp (month, "Jan", 3)) mon = 1;
  220.         if (!strncmp (month, "Feb", 3)) mon = 2;
  221.         if (!strncmp (month, "Mar", 3)) mon = 3;
  222.         if (!strncmp (month, "Apr", 3)) mon = 4;
  223.         if (!strncmp (month, "May", 3)) mon = 5;
  224.         if (!strncmp (month, "Jun", 3)) mon = 6;
  225.         if (!strncmp (month, "Jul", 3)) mon = 7;
  226.         if (!strncmp (month, "Aug", 3)) mon = 8;
  227.         if (!strncmp (month, "Sep", 3)) mon = 9;
  228.         if (!strncmp (month, "Oct", 3)) mon = 10;
  229.         if (!strncmp (month, "Nov", 3)) mon = 11;
  230.         if (!strncmp (month, "Dec", 3)) mon = 12;
  231.  
  232.         /* Convert date */
  233.         sprintf (s, "%02d-%02d-%02d", mon, day, year);
  234.         PadString (s, hp->date, 8);
  235.  
  236.         /* Time */
  237.         sprintf (s, "%02d:%02d", hour, minute);
  238.         PadString (s, hp->time, 5);
  239. }
  240.  
  241.