]> git.sur5r.net Git - glabels/blobdiff - libglbarcode/lgl-barcode-postnet.c
Updated Czech translation
[glabels] / libglbarcode / lgl-barcode-postnet.c
index 96b4132e1f6d33e54793652567c8255f74a97bb6..630848371b2005ac91935dbc3d7b31df130f6ba1 100644 (file)
@@ -2,20 +2,20 @@
  *  lgl-barcode-postnet.c
  *  Copyright (C) 2001-2010  Jim Evins <evins@snaught.com>.
  *
- *  This file is part of gLabels.
+ *  This file is part of libglbarcode.
  *
- *  gLabels is free software: you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
+ *  libglbarcode is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Lesser General Public License as published by
  *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
- *  gLabels is distributed in the hope that it will be useful,
+ *  libglbarcode 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.
+ *  GNU Lesser General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
+ *  You should have received a copy of the GNU Lesser General Public License
+ *  along with libglbarcode.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
@@ -67,17 +67,16 @@ static gchar *frame_symbol = "1";
 /*===========================================*/
 /* Local function prototypes                 */
 /*===========================================*/
-static gchar       *postnet_encode    (const gchar *digits);
+static gint         postnet_validate_data (const gchar *data);
 
-static gboolean     is_length_valid   (const gchar *digits,
-                                       gint         n);
+static gchar       *postnet_encode        (const gchar *digits);
 
-static lglBarcode  *postnet_vectorize (const gchar *code);
+static lglBarcode  *postnet_vectorize     (const gchar *code);
 
 
 
 /****************************************************************************/
-/* Generate list of lines that form the barcode for the given digits.       */
+/* Generate new Postnet barcode structure from data.                        */
 /****************************************************************************/
 lglBarcode *
 lgl_barcode_postnet_new (lglBarcodeType  type,
@@ -87,45 +86,47 @@ lgl_barcode_postnet_new (lglBarcodeType  type,
                          gdouble         h,
                          const gchar    *data)
 {
+        gint                n_digits;
         gchar              *code;
         lglBarcode         *bc;
 
-        /* Validate code length for all subtypes. */
+        /* Validate data and length for all subtypes. */
+        n_digits = postnet_validate_data (data);
         switch (type)
         {
 
         case LGL_BARCODE_TYPE_POSTNET:
-                if (!is_length_valid (data, 5) &&
-                    !is_length_valid (data, 9) &&
-                    !is_length_valid (data, 11))
+                if ( (n_digits !=  5) &&
+                     (n_digits !=  9) &&
+                     (n_digits != 11) )
                 {
                         return NULL;
                 }
                 break;
 
         case LGL_BARCODE_TYPE_POSTNET_5:
-                if (!is_length_valid (data, 5))
+                if ( n_digits != 5 )
                 {
                         return NULL;
                 }
                 break;
 
         case LGL_BARCODE_TYPE_POSTNET_9:
-                if (!is_length_valid (data, 9))
+                if ( n_digits != 9 )
                 {
                         return NULL;
                 }
                 break;
 
         case LGL_BARCODE_TYPE_POSTNET_11:
-                if (!is_length_valid (data, 11))
+                if ( n_digits != 11 )
                 {
                         return NULL;
                 }
                 break;
 
         case LGL_BARCODE_TYPE_CEPNET:
-                if (!is_length_valid (data, 8))
+                if ( n_digits !=  8 )
                 {
                         return NULL;
                 }
@@ -153,6 +154,37 @@ lgl_barcode_postnet_new (lglBarcodeType  type,
 }
 
 
+/*--------------------------------------------------------------------------*/
+/* PRIVATE.  Validate data, returning number of digits if valid.            */
+/*--------------------------------------------------------------------------*/
+static gint
+postnet_validate_data (const gchar *data)
+{
+        gchar *p;
+        gint   i;
+
+        if (!data)
+        {
+                return 0;
+        }
+
+        for ( p = (gchar *)data, i=0; *p != 0; p++ )
+        {
+                if (g_ascii_isdigit (*p))
+                {
+                        i++;
+                }
+                else if ( (*p != '-') && (*p != ' ') )
+                {
+                        /* Only allow digits, dashes, and spaces. */
+                        return 0;
+                }
+        }
+
+        return i;
+}
+
+
 /*--------------------------------------------------------------------------*/
 /* PRIVATE.  Generate string of symbols, representing barcode.              */
 /*--------------------------------------------------------------------------*/
@@ -168,11 +200,11 @@ postnet_encode (const gchar *data)
         code = g_string_new (frame_symbol);
 
         sum = 0;
-        for (p = (gchar *)data, len = 0; (*p != 0) && (len < 11); p++)
+        for ( p = (gchar *)data, len = 0; (*p != 0) && (len < 11); p++ )
         {
                 if (g_ascii_isdigit (*p))
                 {
-                        /* Only translate valid characters (0-9) */
+                        /* Only translate the digits (0-9) */
                         d = (*p) - '0';
                         sum += d;
                         code = g_string_append (code, symbols[d]);
@@ -191,33 +223,6 @@ postnet_encode (const gchar *data)
 }
 
 
-/*--------------------------------------------------------------------------*/
-/* PRIVATE.  Validate specific length of string (for subtypes).             */
-/*--------------------------------------------------------------------------*/
-static gboolean
-is_length_valid (const gchar *data,
-                 gint         n)
-{
-        gchar *p;
-        gint   i;
-
-        if (!data)
-        {
-                return FALSE;
-        }
-
-        for (p = (gchar *)data, i=0; *p != 0; p++)
-        {
-                if (g_ascii_isdigit (*p))
-                {
-                        i++;
-                }
-        }
-
-        return (i == n);
-}
-
-
 /*--------------------------------------------------------------------------*/
 /* PRIVATE.  Vectorize encoded barcode.                                     */
 /*--------------------------------------------------------------------------*/
@@ -232,7 +237,7 @@ postnet_vectorize (const gchar *code)
 
         /* Now traverse the code string and create a list of lines */
         x = POSTNET_HORIZ_MARGIN;
-        for (p = (gchar *)code; *p != 0; p++)
+        for ( p = (gchar *)code; *p != 0; p++ )
         {
                 y = POSTNET_VERT_MARGIN;
                 switch (*p)