Skip to content

Commit

Permalink
Merge pull request #201 from barnhill/iata2of5
Browse files Browse the repository at this point in the history
IATA2of5 symbology, mark all symbology classes as internal
  • Loading branch information
barnhill authored Apr 28, 2024
2 parents ea0563f + f69e092 commit 62d0c92
Show file tree
Hide file tree
Showing 29 changed files with 187 additions and 77 deletions.
11 changes: 6 additions & 5 deletions BarcodeStandard/BarcodeCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ protected void Error(string errorMessage)
throw new Exception(errorMessage);
}

internal static bool CheckNumericOnly(string data)
internal static bool IsNumericOnly(string s)
{
foreach (var c in data)
{
if (c < '0' && c > '9') return false;
}
if (s == null || s == "") return false;

for (int i = 0; i < s.Length; i++)
if ((s[i] ^ '0') > 9)
return false;

return true;
}
Expand Down
6 changes: 5 additions & 1 deletion BarcodeStandard/BarcodeLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Xml.Serialization;
using BarcodeLib;
using BarcodeLib.Symbologies;
using BarcodeStandard.Symbologies;
using SkiaSharp;

/*
Expand All @@ -27,7 +28,7 @@ namespace BarcodeStandard
{
#region Enums
public enum Type
{ Unspecified, UpcA, UpcE, UpcSupplemental2Digit, UpcSupplemental5Digit, Ean13, Ean8, Interleaved2Of5, Interleaved2Of5Mod10, Standard2Of5, Standard2Of5Mod10, Industrial2Of5, Industrial2Of5Mod10, Code39, Code39Extended, Code39Mod43, Codabar, PostNet, Bookland, Isbn, Jan13, MsiMod10, Msi2Mod10, MsiMod11, MsiMod11Mod10, ModifiedPlessey, Code11, Usd8, Ucc12, Ucc13, Logmars, Code128, Code128A, Code128B, Code128C, Itf14, Code93, Telepen, Fim, Pharmacode }
{ Unspecified, UpcA, UpcE, UpcSupplemental2Digit, UpcSupplemental5Digit, Ean13, Ean8, Interleaved2Of5, Interleaved2Of5Mod10, Standard2Of5, Standard2Of5Mod10, Industrial2Of5, Industrial2Of5Mod10, Code39, Code39Extended, Code39Mod43, Codabar, PostNet, Bookland, Isbn, Jan13, MsiMod10, Msi2Mod10, MsiMod11, MsiMod11Mod10, ModifiedPlessey, Code11, Usd8, Ucc12, Ucc13, Logmars, Code128, Code128A, Code128B, Code128C, Itf14, Code93, Telepen, Fim, Pharmacode, IATA2of5 }
public enum SaveTypes
{ Jpg, Png, Webp, Unspecified }
public enum AlignmentPositions
Expand Down Expand Up @@ -331,6 +332,9 @@ public string GenerateBarcode(string rawData = "")
case Type.Standard2Of5: //Encode_Standard2of5();
_iBarcode = new Standard2of5(RawData, EncodedType);
break;
case Type.IATA2of5: //Encode_IATA2of5();
_iBarcode = new IATA2of5(RawData);
break;
case Type.Logmars:
case Type.Code39: //Encode_Code39();
_iBarcode = new Code39(RawData);
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/Blank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// Blank encoding template
/// Written by: Brad Barnhill
/// </summary>
class Blank: BarcodeCommon, IBarcode
internal class Blank: BarcodeCommon, IBarcode
{

#region IBarcode Members
Expand Down
6 changes: 3 additions & 3 deletions BarcodeStandard/Symbologies/Codabar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BarcodeLib.Symbologies
/// Codabar encoding
/// Written by: Brad Barnhill
/// </summary>
class Codabar: BarcodeCommon, IBarcode
internal class Codabar : BarcodeCommon, IBarcode
{
private readonly System.Collections.Hashtable Codabar_Code = new System.Collections.Hashtable(); //is initialized by init_Codabar()

Expand Down Expand Up @@ -52,14 +52,14 @@ private string Encode_Codabar()

foreach (char c in Codabar_Code.Keys)
{
if (!CheckNumericOnly(c.ToString()))
if (!IsNumericOnly(c.ToString()))
{
temp = temp.Replace(c, '1');
}//if
}//if

//now that all the valid non-numeric chars have been replaced with a number check if all numeric exist
if (!CheckNumericOnly(temp))
if (!IsNumericOnly(temp))
Error("ECODABAR-4: Data contains invalid characters.");

var result = "";
Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/Code11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// Code 11 encoding
/// Written by: Brad Barnhill
/// </summary>
class Code11 : BarcodeCommon, IBarcode
internal class Code11 : BarcodeCommon, IBarcode
{
private readonly string[] C11_Code = { "101011", "1101011", "1001011", "1100101", "1011011", "1101101", "1001101", "1010011", "1101001", "110101", "101101", "1011001" };

Expand All @@ -20,7 +20,7 @@ public Code11(string input)
/// </summary>
private string Encode_Code11()
{
if (!CheckNumericOnly(RawData.Replace("-", "")))
if (!IsNumericOnly(RawData.Replace("-", "")))
Error("EC11-1: Numeric data and '-' Only");

//calculate the checksums
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/Code128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BarcodeLib.Symbologies
/// Code 128 encoding
/// Written by: Brad Barnhill
/// </summary>
class Code128 : BarcodeCommon, IBarcode
internal class Code128 : BarcodeCommon, IBarcode
{
public static readonly char FNC1 = Convert.ToChar(200);
public static readonly char FNC2 = Convert.ToChar(201);
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/Code39.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// Code 39 encoding
/// Written by: Brad Barnhill
/// </summary>
class Code39 : BarcodeCommon, IBarcode
internal class Code39 : BarcodeCommon, IBarcode
{
private readonly System.Collections.Hashtable C39_Code = new System.Collections.Hashtable(); //is initialized by init_Code39()
private readonly System.Collections.Hashtable ExtC39_Translation = new System.Collections.Hashtable();
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/Code93.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// Code 93 encoding
/// Written by: Brad Barnhill
/// </summary>
class Code93 : BarcodeCommon, IBarcode
internal class Code93 : BarcodeCommon, IBarcode
{
private readonly System.Data.DataTable C93_Code = new System.Data.DataTable("C93_Code");

Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/EAN13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BarcodeLib.Symbologies
/// EAN-13 encoding
/// Written by: Brad Barnhill
/// </summary>
class EAN13 : BarcodeCommon, IBarcode
internal class EAN13 : BarcodeCommon, IBarcode
{
private readonly string[] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
private readonly string[] EAN_CodeB = { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" };
Expand Down Expand Up @@ -41,7 +41,7 @@ private string Encode_EAN13()
if (RawData.Length < 12 || RawData.Length > 13)
Error("EEAN13-1: Data length invalid. (Length must be 12 or 13)");

if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("EEAN13-2: Numeric Data Only");

var patterncode = EAN_Pattern[Int32.Parse(RawData[0].ToString())];
Expand Down
5 changes: 3 additions & 2 deletions BarcodeStandard/Symbologies/EAN8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// EAN-8 encoding
/// Written by: Brad Barnhill
/// </summary>
class EAN8 : BarcodeCommon, IBarcode
internal class EAN8 : BarcodeCommon, IBarcode
{
private readonly string[] EAN_CodeA = { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
private readonly string[] EAN_CodeC = { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
Expand All @@ -17,7 +17,8 @@ public EAN8(string input)
RawData = input;

//check numeric only
if (!CheckNumericOnly(RawData)) Error("EEAN8-2: Numeric only.");
if (!IsNumericOnly(RawData))
Error("EEAN8-2: Numeric only.");

CheckDigit();
}
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/FIM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BarcodeLib.Symbologies
/// FIM encoding
/// Written by: Brad Barnhill
/// </summary>
class FIM: BarcodeCommon, IBarcode
internal class FIM : BarcodeCommon, IBarcode
{
private readonly string[] FIM_Codes = { "110010011", "101101101", "110101011", "111010111", "101000101" };
public enum FIMTypes {FIM_A = 0, FIM_B, FIM_C, FIM_D, FIM_E};
Expand Down
74 changes: 74 additions & 0 deletions BarcodeStandard/Symbologies/IATA2of5.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using BarcodeLib;
using BarcodeLib.Symbologies;
using System;
using System.Collections.Generic;
using System.Text;

namespace BarcodeStandard.Symbologies
{
internal class IATA2of5 : BarcodeCommon, IBarcode
{
private readonly string[] IATA2of5_Code = { "10101110111010", "11101010101110", "10111010101110", "11101110101010", "10101110101110", "11101011101010", "10111011101010", "10101011101110", "11101010111010", "10111010111010" };

public IATA2of5(string input)
{
RawData = input;
}//Standard2of5

/// <summary>
/// Encode the raw data using the IATA 2 of 5 algorithm.
/// </summary>
private string Encode_IATA2of5()
{
//check length of input
if (RawData.Length > 17 || RawData.Length < 16)
Error("EIATA25-1: Data length invalid. (Length must be 16 or 17)");

if (!IsNumericOnly(RawData))
Error("EIATA25-2: Numeric Data Only");

//strip check digit if provided so it can be recalculated
RawData = RawData.Length == 17 ? RawData.Substring(0, 16) : RawData;

//calculate check digit
var checkdigit = CalculateMod10CheckDigit();
RawData += checkdigit.ToString();

var result = "1010";

for (int i = 0; i < RawData.Length; i++)
{
result += IATA2of5_Code[(int)char.GetNumericValue(RawData, i)];
}

//add check digit
result += IATA2of5_Code[checkdigit];

//add ending bars
result += "01101";
return result;
}//Encode_Standard2of5

private int CalculateMod10CheckDigit()
{
var sum = 0;
var even = true;

for (var i = RawData.Length - 1; i >= 0; --i)
{
//convert numeric in char format to integer and
//multiply by 3 or 1 based on if an even index from the end
sum += (RawData[i] - '0') * (even ? 3 : 1);
even = !even;
}

return (10 - sum % 10) % 10;
}

#region IBarcode Members

public string Encoded_Value => Encode_IATA2of5();

#endregion
}
}
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/ISBN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BarcodeLib.Symbologies
/// ISBN encoding
/// Written by: Brad Barnhill
/// </summary>
class ISBN : BarcodeCommon, IBarcode
internal class ISBN : BarcodeCommon, IBarcode
{
public ISBN(string input)
{
Expand All @@ -17,7 +17,7 @@ public ISBN(string input)
/// </summary>
private string Encode_ISBN_Bookland()
{
if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("EBOOKLANDISBN-1: Numeric Data Only");

var type = "UNKNOWN";
Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/ITF14.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// ITF-14 encoding
/// Written by: Brad Barnhill
/// </summary>
class ITF14 : BarcodeCommon, IBarcode
internal class ITF14 : BarcodeCommon, IBarcode
{
private readonly string[] ITF14_Code = { "NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN" };

Expand All @@ -26,7 +26,7 @@ private string Encode_ITF14()
if (RawData.Length > 14 || RawData.Length < 13)
Error("EITF14-1: Data length invalid. (Length must be 13 or 14)");

if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("EITF14-2: Numeric data only.");

var result = "1010";
Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/Interleaved2of5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BarcodeLib.Symbologies
/// Interleaved 2 of 5 encoding
/// Written by: Brad Barnhill
/// </summary>
class Interleaved2of5 : BarcodeCommon, IBarcode
internal class Interleaved2of5 : BarcodeCommon, IBarcode
{
private readonly string[] _i25Code = { "NNWWN", "WNNNW", "NWNNW", "WWNNN", "NNWNW", "WNWNN", "NWWNN", "NNNWW", "WNNWN", "NWNWN" };
private readonly Type _encodedType;
Expand All @@ -27,7 +27,7 @@ private string Encode_Interleaved2of5()
if (RawData.Length % 2 != (_encodedType == Type.Interleaved2Of5Mod10 ? 1 : 0))
Error("EI25-1: Data length invalid.");

if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("EI25-2: Numeric Data Only");

var result = "1010";
Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/JAN13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BarcodeLib.Symbologies
/// JAN-13 encoding
/// Written by: Brad Barnhill
/// </summary>
class JAN13 : BarcodeCommon, IBarcode
internal class JAN13 : BarcodeCommon, IBarcode
{
public JAN13(string input)
{
Expand All @@ -18,7 +18,7 @@ public JAN13(string input)
private string Encode_JAN13()
{
if (!RawData.StartsWith("49")) Error("EJAN13-1: Invalid Country Code for JAN13 (49 required)");
if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("EJAN13-2: Numeric Data Only");

EAN13 ean13 = new EAN13(RawData);
Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/MSI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace BarcodeLib.Symbologies
{
class MSI : BarcodeCommon, IBarcode
internal class MSI : BarcodeCommon, IBarcode
{
/// <summary>
/// MSI encoding
Expand All @@ -25,7 +25,7 @@ public MSI(string input, Type encodedType)
private string Encode_MSI()
{
//check for non-numeric chars
if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("EMSI-1: Numeric Data Only");

//get checksum
Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/Pharmacode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// Pharmacode encoding
/// Written by: Brad Barnhill
/// </summary>
class Pharmacode: BarcodeCommon, IBarcode
internal class Pharmacode : BarcodeCommon, IBarcode
{
string _thinBar = "1";
string _gap = "00";
Expand All @@ -21,7 +21,7 @@ public Pharmacode(string input)
{
RawData = input;

if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
{
Error("EPHARM-1: Data contains invalid characters (non-numeric).");
}//if
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/Postnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace BarcodeLib.Symbologies
/// Postnet encoding
/// Written by: Brad Barnhill
/// </summary>
class Postnet : BarcodeCommon, IBarcode
internal class Postnet : BarcodeCommon, IBarcode
{
private readonly string[] POSTNET_Code = { "11000", "00011", "00101", "00110", "01001", "01010", "01100", "10001", "10010", "10100" };

Expand Down
4 changes: 2 additions & 2 deletions BarcodeStandard/Symbologies/Standard2of5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BarcodeLib.Symbologies
/// Standard 2 of 5 encoding
/// Written by: Brad Barnhill
/// </summary>
class Standard2of5 : BarcodeCommon, IBarcode
internal class Standard2of5 : BarcodeCommon, IBarcode
{
private readonly string[] S25_Code = { "10101110111010", "11101010101110", "10111010101110", "11101110101010", "10101110101110", "11101011101010", "10111011101010", "10101011101110", "11101010111010", "10111010111010" };
private readonly Type _encodedType = Type.Unspecified;
Expand All @@ -24,7 +24,7 @@ public Standard2of5(string input, Type encodedType)
/// </summary>
private string Encode_Standard2of5()
{
if (!CheckNumericOnly(RawData))
if (!IsNumericOnly(RawData))
Error("ES25-1: Numeric Data Only");

var result = "11011010";
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/Symbologies/Telepen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BarcodeLib.Symbologies
/// Telepen encoding
/// Written by: Brad Barnhill
/// </summary>
class Telepen: BarcodeCommon, IBarcode
internal class Telepen : BarcodeCommon, IBarcode
{
private static readonly Hashtable Telepen_Code = new Hashtable();
private enum StartStopCode : int { START1, STOP1, START2, STOP2, START3, STOP3 };
Expand Down
Loading

0 comments on commit 62d0c92

Please sign in to comment.