Skip to content

Commit

Permalink
Allow disabling of the country code parsing in EAN-13, allow retrieva…
Browse files Browse the repository at this point in the history
…l of the country code for consumers
  • Loading branch information
barnhill committed May 9, 2021
1 parent 4239194 commit 71d68bf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
10 changes: 5 additions & 5 deletions BarcodeStandard/BarcodeStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.3.0</Version>
<Version>2.4.0</Version>
<PackageId>BarcodeLib</PackageId>
<Company>Pnuema Productions</Company>
<Product>BarcodeLib</Product>
Expand All @@ -19,17 +19,17 @@
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIcon>upca.jpg</PackageIcon>
<PackageIconUrl />
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0.0</FileVersion>
<AssemblyVersion>2.4.0.0</AssemblyVersion>
<FileVersion>2.4.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.0-beta-20204-02" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="5.0.1" />
<PackageReference Include="System.Text.Json" Version="5.0.1" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
57 changes: 32 additions & 25 deletions BarcodeStandard/Symbologies/EAN13.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ class EAN13 : BarcodeCommon, IBarcode
private readonly Hashtable _countryCodes = new Hashtable(); //is initialized by init_CountryCodes()
private string _countryAssigningManufacturerCode = "N/A";

public EAN13(string input, bool disableCountryException = false)
public string CountryAssigningManufacturerCode { get => _countryAssigningManufacturerCode; set => _countryAssigningManufacturerCode = value; }

public EAN13(string input, bool disableCountryCode = false)
{
Raw_Data = input;
DisableCountryException = disableCountryException;
DisableCountryCode = disableCountryCode;

CheckDigit();
}

/// <summary>
/// Disables EAN13 invalid country code exception.
/// Disables EAN13 country code parsing
/// </summary>
public bool DisableCountryException { get; set; } = false;
public bool DisableCountryCode { get; set; } = false;

/// <summary>
/// Encode the raw data using the EAN-13 algorithm. (Can include the checksum already. If it doesnt exist in the data then it will calculate it for you. Accepted data lengths are 12 + 1 checksum or just the 12 data digits)
Expand Down Expand Up @@ -77,37 +79,42 @@ private string Encode_EAN13()
//add ending bars
result += "101";

//get the manufacturer assigning country
Init_CountryCodes();
_countryAssigningManufacturerCode = "N/A";
var twodigitCode = Raw_Data.Substring(0, 2);
var threedigitCode = Raw_Data.Substring(0, 3);
if (!DisableCountryCode)
{
ParseCountryCode();
}

return result;
}//Encode_EAN13

private void ParseCountryCode()
{
//get the manufacturer assigning country
Init_CountryCodes();
CountryAssigningManufacturerCode = "N/A";
var twodigitCode = Raw_Data.Substring(0, 2);
var threedigitCode = Raw_Data.Substring(0, 3);

var cc = _countryCodes[threedigitCode];
if (cc == null)
{
{
cc = _countryCodes[twodigitCode].ToString();
if (cc == null)
{
if (!DisableCountryException)
{
Error("EEAN13-3: Country assigning manufacturer code not found.");
}
}
{
Error("EEAN13-3: Country assigning manufacturer code not found.");
}
else
{
_countryAssigningManufacturerCode = cc.ToString();
{
CountryAssigningManufacturerCode = cc.ToString();
}
}
}
else
{
_countryAssigningManufacturerCode = cc.ToString();
{
CountryAssigningManufacturerCode = cc.ToString();
}

_countryCodes.Clear();

return result;
}//Encode_EAN13
_countryCodes.Clear();
}

private void Create_CountryCodeRange(int startingNumber, int endingNumber, string countryDescription)
{
Expand Down
1 change: 1 addition & 0 deletions BarcodeStandardExample/BarcodeStandardExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<EmbeddedResource Include="TestApp.resx">
<DependentUpon>TestApp.cs</DependentUpon>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
6 changes: 6 additions & 0 deletions BarcodeStandardExample/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.NETCore.Platforms" version="5.0.2" targetFramework="net462" />
<package id="NETStandard.Library" version="2.0.3" targetFramework="net462" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net462" />
</packages>

0 comments on commit 71d68bf

Please sign in to comment.