77 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88 *
99 * IDENTIFICATION
10- * $PostgreSQL: pgsql/contrib/isn/isn.c,v 1.8 2008/01/01 19:45:45 momjian Exp $
10+ * $PostgreSQL: pgsql/contrib/isn/isn.c,v 1.9 2008/11/28 18:04:00 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
1717#include "fmgr.h"
1818#include "utils/builtins.h"
1919
20- PG_MODULE_MAGIC ;
21-
2220#include "isn.h"
23-
2421#include "EAN13.h"
2522#include "ISBN.h"
2623#include "ISMN.h"
2724#include "ISSN.h"
2825#include "UPC.h"
2926
27+ PG_MODULE_MAGIC ;
28+
3029#define MAXEAN13LEN 18
3130
3231enum isn_type
3332{
3433INVALID ,ANY ,EAN13 ,ISBN ,ISMN ,ISSN ,UPC
3534};
3635
37- static const char * isn_names []= {"EAN13/UPC/ISxN" ,"EAN13/UPC/ISxN" ,"EAN13" ,"ISBN" ,"ISMN" ,"ISSN" ,"UPC" };
36+ static const char * const isn_names []= {"EAN13/UPC/ISxN" ,"EAN13/UPC/ISxN" ,"EAN13" ,"ISBN" ,"ISMN" ,"ISSN" ,"UPC" };
3837
3938static bool g_weak = false;
4039static bool g_initialized = false;
@@ -58,8 +57,7 @@ static bool g_initialized = false;
5857 * Check if the table and its index is correct (just for debugging)
5958 */
6059#ifdef ISN_DEBUG
61- static
62- bool
60+ static bool
6361check_table (const char * (* TABLE )[2 ],const unsigned TABLE_index [10 ][2 ])
6462{
6563const char * aux1 ,
@@ -139,8 +137,7 @@ check_table(const char *(*TABLE)[2], const unsigned TABLE_index[10][2])
139137 * Formatting and conversion routines.
140138 *---------------------------------------------------------*/
141139
142- static
143- unsigned
140+ static unsigned
144141dehyphenate (char * bufO ,char * bufI )
145142{
146143unsigned ret = 0 ;
@@ -165,8 +162,7 @@ dehyphenate(char *bufO, char *bufI)
165162 *
166163 * Returns the number of characters acctually hyphenated.
167164 */
168- static
169- unsigned
165+ static unsigned
170166hyphenate (char * bufO ,char * bufI ,const char * (* TABLE )[2 ],const unsigned TABLE_index [10 ][2 ])
171167{
172168unsigned ret = 0 ;
@@ -276,8 +272,7 @@ hyphenate(char *bufO, char *bufI, const char *(*TABLE)[2], const unsigned TABLE_
276272 *
277273 * Returns the weight of the number (the check digit value, 0-10)
278274 */
279- static
280- unsigned
275+ static unsigned
281276weight_checkdig (char * isn ,unsigned size )
282277{
283278unsigned weight = 0 ;
@@ -303,8 +298,7 @@ weight_checkdig(char *isn, unsigned size)
303298 *
304299 * Returns the check digit value (0-9)
305300 */
306- static
307- unsigned
301+ static unsigned
308302checkdig (char * num ,unsigned size )
309303{
310304unsigned check = 0 ,
@@ -341,8 +335,7 @@ checkdig(char *num, unsigned size)
341335 * If errorOK is false, ereport a useful error message if the ean13 is bad.
342336 * If errorOK is true, just return "false" for bad input.
343337 */
344- static
345- bool
338+ static bool
346339ean2isn (ean13 ean ,bool errorOK ,ean13 * result ,enum isn_type accept )
347340{
348341enum isn_type type = INVALID ;
@@ -445,8 +438,7 @@ ean2isn(ean13 ean, bool errorOK, ean13 * result, enum isn_type accept)
445438 * ean2UPC/ISxN --- Convert in-place a normalized EAN13 string to the corresponding
446439 *UPC/ISxN string number. Assumes the input string is normalized.
447440 */
448- static inline
449- void
441+ static inline void
450442ean2ISBN (char * isn )
451443{
452444char * aux ;
@@ -463,17 +455,17 @@ ean2ISBN(char *isn)
463455else
464456* aux = check + '0' ;
465457}
466- static inline
467- void
458+
459+ static inline void
468460ean2ISMN (char * isn )
469461{
470462/* the number should come in this format: 979-0-000-00000-0 */
471463/* Just strip the first part and change the first digit ('0') to 'M' */
472464hyphenate (isn ,isn + 4 ,NULL ,NULL );
473465isn [0 ]= 'M' ;
474466}
475- static inline
476- void
467+
468+ static inline void
477469ean2ISSN (char * isn )
478470{
479471unsigned check ;
@@ -488,8 +480,8 @@ ean2ISSN(char *isn)
488480isn [8 ]= check + '0' ;
489481isn [9 ]= '\0' ;
490482}
491- static inline
492- void
483+
484+ static inline void
493485ean2UPC (char * isn )
494486{
495487/* the number should come in this format: 000-000000000-0 */
@@ -505,8 +497,7 @@ ean2UPC(char *isn)
505497 *
506498 * Returns the ean13 value of the string.
507499 */
508- static
509- ean13
500+ static ean13
510501str2ean (const char * num )
511502{
512503ean13 ean = 0 ;/* current ean */
@@ -530,8 +521,7 @@ str2ean(const char *num)
530521 * If errorOK is false, ereport a useful error message if the string is bad.
531522 * If errorOK is true, just return "false" for bad input.
532523 */
533- static
534- bool
524+ static bool
535525ean2string (ean13 ean ,bool errorOK ,char * result ,bool shortType )
536526{
537527const char * (* TABLE )[2 ];
@@ -677,8 +667,7 @@ ean2string(ean13 ean, bool errorOK, char *result, bool shortType)
677667 * if the input string ends with '!' it will always be treated as invalid
678668 * (even if the check digit is valid)
679669 */
680- static
681- bool
670+ static bool
682671string2ean (const char * str ,bool errorOK ,ean13 * result ,
683672enum isn_type accept )
684673{
@@ -1110,7 +1099,6 @@ make_valid(PG_FUNCTION_ARGS)
11101099PG_RETURN_EAN13 (val );
11111100}
11121101
1113- #ifdef ISN_WEAK_MODE
11141102/* this function temporarily sets weak input flag
11151103 * (to lose the strictness of check digit acceptance)
11161104 * It's a helper function, not intended to be used!!
@@ -1119,18 +1107,13 @@ PG_FUNCTION_INFO_V1(accept_weak_input);
11191107Datum
11201108accept_weak_input (PG_FUNCTION_ARGS )
11211109{
1110+ #ifdef ISN_WEAK_MODE
11221111g_weak = PG_GETARG_BOOL (0 );
1123- PG_RETURN_BOOL (g_weak );
1124- }
11251112#else
1126- PG_FUNCTION_INFO_V1 (accept_weak_input );
1127- Datum
1128- accept_weak_input (PG_FUNCTION_ARGS )
1129- {
11301113/* function has no effect */
1131- PG_RETURN_BOOL (false);
1132- }
11331114#endif /* ISN_WEAK_MODE */
1115+ PG_RETURN_BOOL (g_weak );
1116+ }
11341117
11351118PG_FUNCTION_INFO_V1 (weak_input_status );
11361119Datum