home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume8
/
libhoward
/
part05
/
msgfn.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-10-01
|
4KB
|
132 lines
/*
* msgfn - Extract full name from mail message or news article header
*/
#ifndef lint
static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
#endif lint
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 1,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <howard/port.h>
#include <howard/version.h>
MODVER ("@(#)$Header: msgfn.c,v 1.2 89/08/19 10:19:10 howard Exp $");
#include <string.h>
#include <howard/malf.h>
#include <howard/registers.i>
PUBLIC boolT msgfn (s, b)
R3 bStrT s; /* String containing an address field.*/
R4 bStrT b; /* Buffer in which to store result.*/
/* Function:
*
* Algorithm:
* If there's something in double quotes, copy what's in the leftmost pair.
* If there's something in parentheses, copy what's in the leftmost pair.
* If there's something in angle brackets, copy everything else,
* except leading and trailing spaces.
* Returns:
* TRUE iff a full name was copied.
* Notes:
* 1) What's in double quotes isn't always a full name, but it's a good bet.
*/
{
R1 bStrT p1; /* Points to first delimiter.*/
R2 bStrT p2; /* Points to second delimiter.*/
if (NULBSTR == s) malf1 ("msgfn: no input");
if (NULBSTR == b) malf1 ("msgfn: no output");
p1 = bStrChr (s, '"');
if (NULBSTR != p1)
{
++p1;
p2 = bStrChr (p1, '"');
if ((NULBSTR != p2) && (p1 != p2))
{
STRNCPY ((cStrT) b, (cStrT) p1, p2 - p1);
return (TRUE);
}
}
p1 = bStrChr (s, '(');
if (NULBSTR != p1)
{
++p1;
p2 = bStrChr (p1, ')');
if ((NULBSTR != p2) && (p1 != p2))
{
STRNCPY ((cStrT) b, (cStrT) p1, p2 - p1);
return (TRUE);
}
}
p1 = bStrChr (s, '<');
if (NULBSTR != p1)
{
p2 = bStrChr (p1 + 1, '>');
if ((NULBSTR != p2) && ((p1 + 1) != p2))
{
while (' ' == B(*s))
++s;
if (s != p1) STRNCPY ((cStrT) b, (cStrT) s, p1 - s);
p1 = p2 + 1;
for (p2 = strend (p1); ' ' == B(p2[-1]); --p2)
;
if (p1 != p2) STRNCAT ((cStrT) b, (cStrT) p1, p2 - p1);
return (TRUE);
}
}
return (FALSE);
}
#ifdef TEST
#include <howard/usage.h>
MAINVER ("@(#)$Header: msgfn.c,v 1.2 89/08/19 10:19:10 howard Exp $");
USAGE ("");
PRIVATE void t (n, s, xr, xb)
int n; /* Test number.*/
bStrT s; /* Test string.*/
boolT xr; /* Expected return.*/
bStrT xb; /* Expected stored result.*/
{
byteT b[1024]; /* Store result here.*/
if (xr != msgfn (s, b))
PRINTF ("%d: rc %d expected\n", n, xr);
else if (xr && !bStrEQ (b, xb))
PRINTF ("%d: result %s expected %s\n", n, b, xb);
}
PUBLIC int main ()
{
t(__LINE__, S("\"Howard Gayle\"@ericsson.se"), TRUE, S("Howard Gayle"));
t(__LINE__, S("howard@ericsson.se (Howard Gayle)"), TRUE, S("Howard Gayle"));
t(__LINE__, S("Howard Gayle<howard@ericsson.se>"), TRUE, S("Howard Gayle"));
t(__LINE__, S("howard@ericsson.se"), FALSE, NULBSTR);
mfflush (stdout, S("Standard Output"));
exit (SUCCESS);
#ifdef lint
return (SUCCESS);
#endif
}
#endif