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