Indent
[pazpar2-moved-to-github.git] / src / normalize7bit.c
1 /* $Id: normalize7bit.c,v 1.3 2007-09-07 10:27:14 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4    This file is part of Pazpar2.
5
6    Pazpar2 is free software; you can redistribute it and/or modify it under
7    the terms of the GNU General Public License as published by the Free
8    Software Foundation; either version 2, or (at your option) any later
9    version.
10
11    Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12    WARRANTY; without even the implied warranty of MERCHANTABILITY or
13    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14    for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Pazpar2; see the file LICENSE.  If not, write to the
18    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19    02111-1307, USA.
20 */
21
22 /** \file normalize7bit.c
23     \brief char and string normalization for 7bit ascii only
24 */
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29
30 #if HAVE_CONFIG_H
31 #include "cconfig.h"
32 #endif
33
34 #include "normalize7bit.h"
35
36
37 char * normalize7bit_generic(char * str, const char * rm_chars)
38 {
39     char *p, *pe;
40     for (p = str; *p && isspace(*p); p++)
41         ;
42     for (pe = p + strlen(p) - 1;
43          pe > p && strchr(rm_chars, *pe); pe--)
44         *pe = '\0';
45     return p;
46 }
47
48
49
50 char * normalize7bit_mergekey(char *buf, int skiparticle)
51 {
52     char *p = buf, *pout = buf;
53
54     if (skiparticle)
55     {
56         char firstword[64];
57         char articles[] = "the den der die des an a "; // must end in space
58
59         while (*p && !isalnum(*p))
60             p++;
61         pout = firstword;
62         while (*p && *p != ' ' && pout - firstword < 62)
63             *(pout++) = tolower(*(p++));
64         *(pout++) = ' ';
65         *(pout++) = '\0';
66         if (!strstr(articles, firstword))
67             p = buf;
68         pout = buf;
69     }
70
71     while (*p)
72     {
73         while (*p && !isalnum(*p))
74             p++;
75         while (isalnum(*p))
76             *(pout++) = tolower(*(p++));
77         if (*p)
78             *(pout++) = ' ';
79         while (*p && !isalnum(*p))
80             p++;
81     }
82     if (buf != pout)
83         do {
84             *(pout--) = '\0';
85         }
86         while (pout > buf && *pout == ' ');
87     
88     return buf;
89 }
90
91 // Extract what appears to be years from buf, storing highest and
92 // lowest values.
93 int extract7bit_years(const char *buf, int *first, int *last)
94 {
95     *first = -1;
96     *last = -1;
97     while (*buf)
98     {
99         const char *e;
100         int len;
101
102         while (*buf && !isdigit(*buf))
103             buf++;
104         len = 0;
105         for (e = buf; *e && isdigit(*e); e++)
106             len++;
107         if (len == 4)
108         {
109             int value = atoi(buf);
110             if (*first < 0 || value < *first)
111                 *first = value;
112             if (*last < 0 || value > *last)
113                 *last = value;
114         }
115         buf = e;
116     }
117     return *first;
118 }
119
120
121
122 /*
123  * Local variables:
124  * c-basic-offset: 4
125  * indent-tabs-mode: nil
126  * End:
127  * vim: shiftwidth=4 tabstop=8 expandtab
128  */