Browse Source

nicer way of parsing barcode 'fragments', bar to decimal table support

master
Lynne Megido 12 months ago
parent
commit
d290dbadad
Signed by: lynnesbian GPG Key ID: F0A184B5213D9F90
  1. 88
      .idea/.idea.auspostcode.dir/.idea/workspace.xml
  2. 112
      Barcode.cs
  3. 2
      BaseConversion.cs
  4. 9
      Program.cs

88
.idea/.idea.auspostcode.dir/.idea/workspace.xml

@ -20,12 +20,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="373c5ee6-3f10-450b-8af6-fc4dd99290b0" name="Default Changelist" comment="initial commit, as they say">
<change afterPath="$PROJECT_DIR$/BaseConversion.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.auspostcode.dir/.idea/contentModel.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.auspostcode.dir/.idea/contentModel.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.auspostcode.dir/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.auspostcode.dir/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Barcode.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Barcode.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Folder.DotSettings.user" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/BaseConversion.cs" beforeDir="false" afterPath="$PROJECT_DIR$/BaseConversion.cs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Program.cs" beforeDir="false" afterPath="$PROJECT_DIR$/Program.cs" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -40,6 +37,7 @@
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/87DBB896-6B20-4A14-9B6A-7AB43642646D/42/0e5622b9/Number.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/A5F6779E-8C83-415E-AF7A-DAB44F32C608/c6/1054d372/ThrowHelper.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/A5F6779E-8C83-415E-AF7A-DAB44F32C608/84/0eb89c24/Enumerable.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/87DBB896-6B20-4A14-9B6A-7AB43642646D/88/6811f6df/String.cs" root0="SKIP_HIGHLIGHTING" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
@ -98,7 +96,7 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1602593942454</updated>
<workItem from="1602593947835" duration="11001000" />
<workItem from="1602593947835" duration="13843000" />
</task>
<task id="LOCAL-00001" summary="initial commit, as they say">
<created>1602597326777</created>
@ -150,86 +148,86 @@
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state x="727" y="366" width="424" height="478" key="FileChooserDialogImpl/0.0.1920.1054@0.0.1920.1054" timestamp="1602597073977" />
<state width="1878" height="374" key="GridCell.Tab.0.bottom" timestamp="1602604911216">
<state width="1878" height="374" key="GridCell.Tab.0.bottom" timestamp="1602607646431">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.0.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911216" />
<state width="1878" height="374" key="GridCell.Tab.0.center" timestamp="1602604911213">
<state width="1878" height="374" key="GridCell.Tab.0.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646431" />
<state width="1878" height="374" key="GridCell.Tab.0.center" timestamp="1602607646431">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.0.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911213" />
<state width="1878" height="374" key="GridCell.Tab.0.left" timestamp="1602604911211">
<state width="1878" height="374" key="GridCell.Tab.0.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646431" />
<state width="1878" height="374" key="GridCell.Tab.0.left" timestamp="1602607646431">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.0.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911211" />
<state width="1878" height="374" key="GridCell.Tab.0.right" timestamp="1602604911214">
<state width="1878" height="374" key="GridCell.Tab.0.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646431" />
<state width="1878" height="374" key="GridCell.Tab.0.right" timestamp="1602607646431">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.0.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911214" />
<state width="1878" height="374" key="GridCell.Tab.1.bottom" timestamp="1602604911221">
<state width="1878" height="374" key="GridCell.Tab.0.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646431" />
<state width="1878" height="374" key="GridCell.Tab.1.bottom" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.1.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911221" />
<state width="1878" height="374" key="GridCell.Tab.1.center" timestamp="1602604911218">
<state width="1878" height="374" key="GridCell.Tab.1.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.1.center" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.1.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911218" />
<state width="1878" height="374" key="GridCell.Tab.1.left" timestamp="1602604911217">
<state width="1878" height="374" key="GridCell.Tab.1.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.1.left" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.1.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911217" />
<state width="1878" height="374" key="GridCell.Tab.1.right" timestamp="1602604911220">
<state width="1878" height="374" key="GridCell.Tab.1.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.1.right" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.1.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911220" />
<state width="1878" height="374" key="GridCell.Tab.2.bottom" timestamp="1602604911227">
<state width="1878" height="374" key="GridCell.Tab.1.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.2.bottom" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.2.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911227" />
<state width="1878" height="374" key="GridCell.Tab.2.center" timestamp="1602604911224">
<state width="1878" height="374" key="GridCell.Tab.2.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.2.center" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.2.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911224" />
<state width="1878" height="374" key="GridCell.Tab.2.left" timestamp="1602604911223">
<state width="1878" height="374" key="GridCell.Tab.2.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.2.left" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.2.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911223" />
<state width="1878" height="374" key="GridCell.Tab.2.right" timestamp="1602604911225">
<state width="1878" height="374" key="GridCell.Tab.2.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.2.right" timestamp="1602607646287">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.2.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911225" />
<state width="1878" height="374" key="GridCell.Tab.3.bottom" timestamp="1602604911232">
<state width="1878" height="374" key="GridCell.Tab.2.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646287" />
<state width="1878" height="374" key="GridCell.Tab.3.bottom" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.3.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911232" />
<state width="1878" height="374" key="GridCell.Tab.3.center" timestamp="1602604911230">
<state width="1878" height="374" key="GridCell.Tab.3.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.3.center" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.3.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911230" />
<state width="1878" height="374" key="GridCell.Tab.3.left" timestamp="1602604911229">
<state width="1878" height="374" key="GridCell.Tab.3.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.3.left" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.3.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911229" />
<state width="1878" height="374" key="GridCell.Tab.3.right" timestamp="1602604911231">
<state width="1878" height="374" key="GridCell.Tab.3.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.3.right" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.3.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911231" />
<state width="1878" height="374" key="GridCell.Tab.4.bottom" timestamp="1602604911236">
<state width="1878" height="374" key="GridCell.Tab.3.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.4.bottom" timestamp="1602607646289">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.4.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911236" />
<state width="1878" height="374" key="GridCell.Tab.4.center" timestamp="1602604911234">
<state width="1878" height="374" key="GridCell.Tab.4.bottom/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646289" />
<state width="1878" height="374" key="GridCell.Tab.4.center" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.4.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911234" />
<state width="1878" height="374" key="GridCell.Tab.4.left" timestamp="1602604911233">
<state width="1878" height="374" key="GridCell.Tab.4.center/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.4.left" timestamp="1602607646288">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.4.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911233" />
<state width="1878" height="374" key="GridCell.Tab.4.right" timestamp="1602604911235">
<state width="1878" height="374" key="GridCell.Tab.4.left/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646288" />
<state width="1878" height="374" key="GridCell.Tab.4.right" timestamp="1602607646289">
<screen x="0" y="0" width="1920" height="1054" />
</state>
<state width="1878" height="374" key="GridCell.Tab.4.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602604911235" />
<state width="1878" height="374" key="GridCell.Tab.4.right/0.0.1920.1054@0.0.1920.1054" timestamp="1602607646289" />
<state x="738" y="92" key="SettingsEditor" timestamp="1602603890848">
<screen x="0" y="0" width="1920" height="1054" />
</state>

112
Barcode.cs

@ -5,14 +5,27 @@ using System.Text.RegularExpressions;
namespace AusPostCode {
public class Barcode {
public enum BarcodeFragment {
FormatControlCode,
SortingCode,
CustomerInformation,
ErrorCorrection,
private readonly struct BarcodeFragment {
public EncodingFormat TypicalFormat { get; }
public int Length { get;}
public int Offset { get; }
public BarcodeFragment(EncodingFormat typicalFormat, int length, int offset) {
TypicalFormat = typicalFormat;
Length = length;
Offset = offset;
}
}
public enum EncodingFormat {
private readonly Dictionary<string, BarcodeFragment> _fragments = new Dictionary<string, BarcodeFragment> {
["FormatControlCode"] = new BarcodeFragment(EncodingFormat.N, 4, 2),
["SortingCode"] = new BarcodeFragment(EncodingFormat.N, 16, 6),
["CustomerInformation2"] = new BarcodeFragment(EncodingFormat.C, 16, 22),
["CustomerInformation3"] = new BarcodeFragment(EncodingFormat.C, 31, 22),
["ErrorCorrection"] = new BarcodeFragment(EncodingFormat.BarToDecimal, 12, - 16),
};
private enum EncodingFormat {
N,
C,
BarToDecimal,
@ -20,7 +33,7 @@ namespace AusPostCode {
private int _formatCode;
public Dictionary<int, string> FormatTable = new Dictionary<int, string> {
private readonly Dictionary<int, string> _formatTable = new Dictionary<int, string> {
[0] = "Null Customer Barcode",
[11] = "Standard Customer Barcode",
[52] = "Customer Business Reply Paid",
@ -45,31 +58,82 @@ namespace AusPostCode {
}
// process Format Control Code
_formatCode = int.Parse(Decode(GetFragment(BarcodeFragment.FormatControlCode), EncodingFormat.N));
_formatCode = int.Parse(GetFragment(_fragments["FormatControlCode"]));
Console.WriteLine(Format);
// process Sorting Code Field
SortingCode = int.Parse(Decode(GetFragment(BarcodeFragment.SortingCode), EncodingFormat.N));
SortingCode = int.Parse(GetFragment(_fragments["SortingCode"]));
Console.WriteLine($"Sorting code: {SortingCode}");
// process Customer Information, if necessary
// TODO: actually do this, and get a code to test it on. also, ensure that customer codes are the right length
if (_formatCode == 59) {
Console.WriteLine("This code has a Customer Information field, but I don't know how to process that yet 0uo");
} else if (_formatCode == 62) {
Console.WriteLine("This code has a Customer Information field, but I don't know how to process that yet 0uo");
}
// process Reed-Solomon Error Correction Bars
// TODO: actually validate the barcode
Console.WriteLine(BaseConversion.FromBase(GetFragment(_fragments["ErrorCorrection"]), 64));
}
public int SortingCode { get; set; }
public int CustomerInformation { get; set; }
public string Format => FormatTable.ContainsKey(_formatCode) ? FormatTable[_formatCode] : $"Unknown ({_formatCode})";
private List<string> Warnings { get; set; } = new List<string>();
private string Code { get; set; }
private string GetFragment(BarcodeFragment barcodeFragment) {
return barcodeFragment switch {
BarcodeFragment.FormatControlCode => Code.Substring(2, 4),
BarcodeFragment.SortingCode => Code.Substring(6, 16),
BarcodeFragment.CustomerInformation when _formatCode == 59 => Code.Substring(22, 16),
BarcodeFragment.CustomerInformation when _formatCode == 62 => Code.Substring(22, 31),
BarcodeFragment.CustomerInformation => null, // format doesn't support the customer information field
BarcodeFragment.ErrorCorrection => Code.Substring(Code.Length - 15,
12), // the error correction bars are always immediately before the stop bars
_ => throw new ArgumentOutOfRangeException(nameof(barcodeFragment), barcodeFragment, null),
};
public string Format => _formatTable.ContainsKey(_formatCode) ? _formatTable[_formatCode] : $"Unknown ({_formatCode})";
private List<string> Warnings { get; } = new List<string>();
private string Code { get; }
private string GetFragment(BarcodeFragment fragment, bool decode = true) {
var data = Code.Substring(fragment.Offset < 0 ? Code.Length + fragment.Offset : fragment.Offset, fragment.Length);
if (!decode) {
return data;
}
// bool badData;
var chunkLength = fragment.TypicalFormat == EncodingFormat.N ? 2 : 3;
var rx = new Regex("^[0123]+$");
var sb = new StringBuilder();
if (!rx.IsMatch(data)) {
throw new ArgumentException("Input length must be a quaternary number.", nameof(data));
}
for (var i = 0; i < data.Length; i += chunkLength) {
var chunk = data.Substring(i, chunkLength);
switch (fragment.TypicalFormat) {
case EncodingFormat.N:
// format N supports the digits 0 through 9, and nothing else.
// digits 0 through 8 are stored as their ternary representations, while 9 is stored as "30".
if (chunk == "30") {
sb.Append(9);
}
else if (chunk.Contains("3")) {
// not a ternary number
throw new ArgumentException($"{chunk} is not a valid identifier for format {data.ToString()}.");
}
else {
sb.Append(BaseConversion.FromBase(chunk, 3));
}
break;
case EncodingFormat.C:
break;
case EncodingFormat.BarToDecimal:
var digitInt = BaseConversion.FromBase(chunk, 4);
sb.Insert(0, BaseConversion.ToBase(digitInt, 64));
break;
default:
throw new ArgumentOutOfRangeException(nameof(fragment.TypicalFormat), fragment.TypicalFormat, null);
}
}
return sb.ToString();
}
private static string Decode(string input, EncodingFormat format) {

2
BaseConversion.cs

@ -6,7 +6,7 @@ namespace AusPostCode {
public static class BaseConversion {
// honestly surprised c# doesn't do this out of the box
private static readonly char[] Characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
private static readonly char[] Characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-=".ToCharArray();
// thanks to https://stackoverflow.com/a/35004409
private static readonly Dictionary<char, int> CharacterIndices =

9
Program.cs

@ -22,8 +22,15 @@ using CommandLine;
namespace AusPostCode {
internal class Program {
private static void Main(string[] args) {
Console.WriteLine("Hello World!");
// test code consisting of the following:
// - start bars
// - fcc of 0101 == 11 == standard customer barcode
// - sorting code of 2112011202120020 == 75152506
// - a filler bar of 3
// - a reed-solomon code of 001101103221 == 10826817
// - end bars
const string testCode = "1301012112011202120020300110110322113";
var barcode = new Barcode(testCode);
}

Loading…
Cancel
Save