This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
NKDModifiedPlesseyBarcode.m
executable file
·105 lines (95 loc) · 4.73 KB
/
NKDModifiedPlesseyBarcode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// -----------------------------------------------------------------------------------
// NKDModifiedPlesseyBarcode.m
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Tue May 07 2002.
// �2002 Naked Software. All rights reserved.
// -----------------------------------------------------------------------------------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
// -----------------------------------------------------------------------------------
// You may use and redistribute this source code without limitation
// -----------------------------------------------------------------------------------
#import "NKDModifiedPlesseyBarcode.h"
@implementation NKDModifiedPlesseyBarcode
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
printsCaption: (BOOL)inPrints
// -----------------------------------------------------------------------------------
{
if ([super initWithContent:[inContent uppercaseString] printsCaption:inPrints])
{
// CRC is required for Plessey
[self generateChecksum];
// Recalculate width based with check digit
[self calculateWidth];
}
return self;
}
// -----------------------------------------------------------------------------------
-(NSString *)_encodeChar:(char)inChar
// -----------------------------------------------------------------------------------
{
switch (inChar)
{
case '0':
return [NSString stringWithFormat:@"%@%@%@%@", ZERO_BIT , ZERO_BIT , ZERO_BIT , ZERO_BIT];
case '1':
return [NSString stringWithFormat:@"%@%@%@%@", ONE_BIT , ZERO_BIT , ZERO_BIT , ZERO_BIT];
case '2':
return [NSString stringWithFormat:@"%@%@%@%@", ZERO_BIT , ONE_BIT , ZERO_BIT , ZERO_BIT];
case '3':
return [NSString stringWithFormat:@"%@%@%@%@", ONE_BIT , ONE_BIT , ZERO_BIT , ZERO_BIT];
case '4':
return [NSString stringWithFormat:@"%@%@%@%@", ZERO_BIT , ZERO_BIT , ONE_BIT , ZERO_BIT];
case '5':
return [NSString stringWithFormat:@"%@%@%@%@", ONE_BIT , ZERO_BIT , ONE_BIT , ZERO_BIT];
case '6':
return [NSString stringWithFormat:@"%@%@%@%@", ZERO_BIT , ONE_BIT , ONE_BIT , ZERO_BIT];
case '7':
return [NSString stringWithFormat:@"%@%@%@%@", ONE_BIT , ONE_BIT , ONE_BIT , ZERO_BIT];
case '8':
return [NSString stringWithFormat:@"%@%@%@%@", ZERO_BIT , ZERO_BIT , ZERO_BIT , ONE_BIT];
case '9':
return [NSString stringWithFormat:@"%@%@%@%@", ONE_BIT , ZERO_BIT , ZERO_BIT , ONE_BIT];
default:
break;
}
return @"";
}
// -----------------------------------------------------------------------------------
-(NSString *)initiator
// -----------------------------------------------------------------------------------
{
return [NSString stringWithFormat:@"%@%@%@%@", ONE_BIT, ONE_BIT, ZERO_BIT, ONE_BIT];
}
// -----------------------------------------------------------------------------------
-(NSString *)terminator
// -----------------------------------------------------------------------------------
{
return [NSString stringWithFormat:@"111%@%@%@%@", ZERO_BIT, ZERO_BIT, ONE_BIT, ONE_BIT];
}
// -----------------------------------------------------------------------------------
-(void)generateChecksum
// -----------------------------------------------------------------------------------
{
NSMutableString *newNum = [NSMutableString stringWithString:@""];
char *code = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy], *productString;
NSNumber *product;
int i, productSum=0;
// Starting from the units position, create a new number with all of
// the odd position digits in their original sequence.
for (i = strlen(code)-1; i >=0; i-=2)
[newNum appendString:[NSString stringWithFormat:@"%c", code[i]]];
// Multiply this new number by 2
product = [NSNumber numberWithInt:[newNum intValue] * 2];
// Add all of the digits of the product from step two.
productString = (char *)[[product stringValue] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
for (i = 0; i < strlen(productString); i++)
productSum += (productString[i] - '0');
// Add all of the digits not used in step one to the result in step three.
for (i = strlen(code -2); i >=0; i-=2)
productSum += (code[i] - '0');
// Determine the smallest number which when added to the result in step four
// will result in a multiple of 10. This is the check character
checkDigit = (productSum%10 == 0) ? '0' : 10 - (productSum%10) + '0';
}
@end