home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / libhoward / part05 / msgfn.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  4KB  |  132 lines

  1. /*
  2.  * msgfn - Extract full name from mail message or news article header
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27.  
  28. MODVER ("@(#)$Header: msgfn.c,v 1.2 89/08/19 10:19:10 howard Exp $");
  29.  
  30. #include <string.h>
  31. #include <howard/malf.h>
  32. #include <howard/registers.i>
  33.  
  34. PUBLIC boolT msgfn (s, b)
  35. R3 bStrT s; /* String containing an address field.*/
  36. R4 bStrT b; /* Buffer in which to store result.*/
  37.  
  38. /* Function:
  39.  *    
  40.  * Algorithm:
  41.  *    If there's something in double quotes, copy what's in the leftmost pair.
  42.  *    If there's something in parentheses, copy what's in the leftmost pair.
  43.  *    If there's something in angle brackets, copy everything else,
  44.  *    except leading and trailing spaces.
  45.  * Returns:
  46.  *    TRUE iff a full name was copied.
  47.  * Notes:
  48.  *    1) What's in double quotes isn't always a full name, but it's a good bet.
  49.  */
  50. {
  51. R1 bStrT p1; /* Points to first delimiter.*/
  52. R2 bStrT p2; /* Points to second delimiter.*/
  53.  
  54. if (NULBSTR == s) malf1 ("msgfn: no input");
  55. if (NULBSTR == b) malf1 ("msgfn: no output");
  56. p1 = bStrChr (s, '"');
  57. if (NULBSTR != p1)
  58.    {
  59.    ++p1;
  60.    p2 = bStrChr (p1, '"');
  61.    if ((NULBSTR != p2) && (p1 != p2))
  62.       {
  63.       STRNCPY ((cStrT) b, (cStrT) p1, p2 - p1);
  64.       return (TRUE);
  65.       }
  66.    }
  67. p1 = bStrChr (s, '(');
  68. if (NULBSTR != p1)
  69.    {
  70.    ++p1;
  71.    p2 = bStrChr (p1, ')');
  72.    if ((NULBSTR != p2) && (p1 != p2))
  73.       {
  74.       STRNCPY ((cStrT) b, (cStrT) p1, p2 - p1);
  75.       return (TRUE);
  76.       }
  77.    }
  78. p1 = bStrChr (s, '<');
  79. if (NULBSTR != p1)
  80.    {
  81.    p2 = bStrChr (p1 + 1, '>');
  82.    if ((NULBSTR != p2) && ((p1 + 1) != p2))
  83.       {
  84.       while (' ' == B(*s))
  85.          ++s;
  86.       if (s != p1) STRNCPY ((cStrT) b, (cStrT) s, p1 - s);
  87.       p1 = p2 + 1;
  88.       for (p2 = strend (p1); ' ' == B(p2[-1]); --p2)
  89.          ;
  90.       if (p1 != p2) STRNCAT ((cStrT) b, (cStrT) p1, p2 - p1);
  91.       return (TRUE);
  92.       }
  93.    }
  94. return (FALSE);
  95. }
  96.  
  97. #ifdef TEST
  98. #include <howard/usage.h>
  99.  
  100. MAINVER ("@(#)$Header: msgfn.c,v 1.2 89/08/19 10:19:10 howard Exp $");
  101. USAGE ("");
  102.  
  103.  
  104. PRIVATE void t (n, s, xr, xb)
  105. int n; /* Test number.*/
  106. bStrT s; /* Test string.*/
  107. boolT xr; /* Expected return.*/
  108. bStrT xb; /* Expected stored result.*/
  109. {
  110. byteT b[1024]; /* Store result here.*/
  111.  
  112. if (xr != msgfn (s, b))
  113.    PRINTF ("%d: rc %d expected\n", n, xr);
  114. else if (xr && !bStrEQ (b, xb))
  115.    PRINTF ("%d: result %s expected %s\n", n, b, xb);
  116. }
  117.  
  118. PUBLIC int main ()
  119. {
  120. t(__LINE__, S("\"Howard Gayle\"@ericsson.se"), TRUE, S("Howard Gayle"));
  121. t(__LINE__, S("howard@ericsson.se (Howard Gayle)"), TRUE, S("Howard Gayle"));
  122. t(__LINE__, S("Howard Gayle<howard@ericsson.se>"), TRUE, S("Howard Gayle"));
  123. t(__LINE__, S("howard@ericsson.se"), FALSE, NULBSTR);
  124. mfflush (stdout, S("Standard Output"));
  125. exit (SUCCESS);
  126.  
  127. #ifdef lint
  128. return (SUCCESS);
  129. #endif
  130. }
  131. #endif
  132.