-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecord.go
More file actions
793 lines (665 loc) · 16 KB
/
record.go
File metadata and controls
793 lines (665 loc) · 16 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
// Copyright 2017-2019 Dale Farnsworth. All rights reserved.
// Dale Farnsworth
// 1007 W Mendoza Ave
// Mesa, AZ 85210
// USA
//
// dale@farnsworth.org
// This file is part of Codeplug.
//
// Codeplug is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU Lesser General Public
// License as published by the Free Software Foundation.
//
// Codeplug 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Codeplug. If not, see <http://www.gnu.org/licenses/>.
// Package codeplug implements access to MD380-style codeplug files.
// It can read/update/write both .rdt files and .bin files.
package codeplug
import (
"fmt"
"sort"
"strconv"
"strings"
l "github.com/dalefarnsworth-dmr/debug"
)
// A Record represents a record within a Codeplug.
type Record struct {
*rDesc
fDesc *map[FieldType]*fDesc
rIndex int
}
// An rDesc contains a record type's dynamic information.
type rDesc struct {
*recordInfo
codeplug *Codeplug
records []*Record
cachedListNames *[]string
}
// A recordInfo contains a record type's static information.
type recordInfo struct {
rType RecordType
typeName string
max int
offset int
size int
delDesc *delDesc
fieldInfos []*fieldInfo
nameFieldType FieldType
index int
namePrefix string
names []string
}
// A RecordType represents a record's type
type RecordType string
// A delDesc contains the location and value of a deleted record indicator.
type delDesc struct {
offset uint8
size uint8
value byte
}
// deleteRecord removes record i from the slice: records
func deleteRecord(records *[]*Record, i int) {
copy((*records)[i:], (*records)[i+1:])
(*records)[len(*records)-1] = nil
*records = (*records)[:len(*records)-1]
}
type fieldRef struct {
rType RecordType
fType FieldType
}
// loadRecords loads all the records in rDesc from the codeplug's file.
func (rd *rDesc) loadRecords() {
records := rd.records
records = make([]*Record, rd.max)
cp := rd.codeplug
length := 0
for rIndex := range records {
if !rd.recordIsDeleted(cp, rIndex) {
r := records[rIndex]
if r == nil {
r = cp.newRecord(rd.rType, rIndex)
}
r.load()
nameField := r.NameField()
if nameField != nil && nameField.String() == "" {
continue
}
records[length] = r
length++
}
}
rd.records = records[:length]
for _, r := range rd.records {
r.makeNameUnique()
}
}
// newField creates and returns the address of a new field of the given type.
func (r *Record) NewField(fType FieldType) *Field {
if r.fDesc == nil {
m := make(map[FieldType]*fDesc)
r.fDesc = &m
}
fd := (*r.fDesc)[fType]
f := new(Field)
if fd == nil {
for _, fi := range r.rDesc.fieldInfos {
if fi.fType == fType {
fd = &fDesc{fi, r, make([]*Field, 0)}
break
}
}
if fd == nil {
// bad field type
fd = &fDesc{r.rDesc.fieldInfos[0], r, make([]*Field, 0)}
}
fd.record = r
fd.fields = make([]*Field, 0)
(*r.fDesc)[fType] = fd
}
f.fDesc = fd
f.value = newValue(fd.valueType)
f.SetDefault()
return f
}
// addField adds the given field to the record.
func (r *Record) addField(f *Field) error {
if len(f.fields) >= f.max {
return fmt.Errorf("too many fields: %s", string(f.fType))
}
fd := (*r.fDesc)[f.fType]
f.fIndex = len(fd.fields)
fd.fields = append(fd.fields, f)
return nil
}
// load replaces the record's contents with the fields found in
// the codeplug.
func (r *Record) load() {
ri := r.rDesc.recordInfo
for i := range ri.fieldInfos {
fi := ri.fieldInfos[i]
fi.index = i
if fi.max == 0 {
fi.max = 1
}
fi.recordInfo = ri
fd := &fDesc{fieldInfo: fi}
(*r.fDesc)[fi.fType] = fd
if fi.valueType == VtName || fi.valueType == VtContactName {
ri.nameFieldType = fi.fType
}
fd.record = r
}
for _, fd := range *r.fDesc {
fi := fd.fieldInfo
fields := make([]*Field, fi.max)
length := 0
for fIndex := range fields {
if !fd.fieldDeleted(r, fIndex) {
f := fields[fIndex]
if f == nil {
f = &Field{}
}
f.fDesc = fd
f.fIndex = fIndex
f.value = newValue(fi.valueType)
f.load()
span := f.span
if span != nil {
if span.scale == 0 {
span.scale = 1
}
if span.interval == 0 {
span.interval = 1
}
}
fields[length] = f
length++
}
}
fd.fields = fields[:length]
}
}
// valid returns nil if all fields in the record are valid.
func (r *Record) valid() error {
errStr := ""
for _, fType := range r.FieldTypes() {
for _, f := range r.Fields(fType) {
if err := f.valid(); err != nil {
errStr += f.FullTypeName() + ": " + err.Error() + "\n"
}
}
}
if errStr != "" {
return fmt.Errorf("%s", errStr)
}
return nil
}
// stores stores all all fields of the record into the given byte slice.
func (r *Record) store() {
for _, fd := range *r.fDesc {
for fIndex := 0; fIndex < fd.max; fIndex++ {
if fIndex < len(fd.fields) {
fd.fields[fIndex].store()
} else {
fd.deleteField(r, fIndex)
}
}
}
}
// FieldTypes return all valid FieldTypes for the record.
func (r *Record) FieldTypes() []FieldType {
fds := *r.fDesc
indexedStrs := make(map[int]string)
indexes := make([]int, 0, len(fds))
for fType, fd := range fds {
index := fd.fieldInfo.index
indexes = append(indexes, index)
indexedStrs[index] = string(fType)
}
sort.Ints(indexes)
fTypes := make([]FieldType, len(indexes))
for i, index := range indexes {
fTypes[i] = FieldType(indexedStrs[index])
}
return fTypes
}
func (r *Record) AllFieldTypes() []FieldType {
fTypes := make([]FieldType, 0)
for _, fi := range r.rDesc.fieldInfos {
fTypes = append(fTypes, fi.fType)
}
return fTypes
}
// Fields returns a slice of all fields of the given type in the record.
func (r *Record) Fields(fType FieldType) []*Field {
fDesc := (*r.fDesc)[fType]
if fDesc == nil {
return nil
}
return fDesc.fields
}
// Field returns the first field of the given type in the record.
func (r *Record) Field(fType FieldType) *Field {
fields := r.Fields(fType)
if len(fields) == 0 {
return nil
}
return fields[0]
}
func (r *Record) AllFields() []*Field {
fields := make([]*Field, 0)
for _, fType := range r.FieldTypes() {
for _, f := range r.Fields(fType) {
fields = append(fields, f)
}
}
return fields
}
// MaxFields returns the maximum number of fields of the given type for
// record.
func (r *Record) MaxFields(fType FieldType) int {
return (*r.fDesc)[fType].max
}
// Type returns the record's type.
func (r *Record) Type() RecordType {
return r.rType
}
// TypeName returns the name of the record's type.
func (r *Record) TypeName() string {
return r.typeName
}
// Index returns the slice index of the record.
func (r *Record) Index() int {
return r.rIndex
}
func (r *Record) FullTypeName() string {
s := r.typeName
if r.max > 1 {
name := r.Name()
if r.names != nil {
name = r.names[r.rIndex]
}
if name == "" {
name = fmt.Sprintf("%d", r.rIndex)
}
s += fmt.Sprintf("[%s]", name)
}
return s
}
// Index set the index of the record.
func (r *Record) SetIndex(index int) {
r.rIndex = index
}
// Codeplug returns the codeplug for the record.
func (r *Record) Codeplug() *Codeplug {
return r.codeplug
}
// NameField returns the field containing the record's name.
func (r *Record) NameField() *Field {
if (*r.fDesc)[r.nameFieldType] == nil {
return nil
}
return r.Field(r.nameFieldType)
}
// NameType returns the fieldtype containing the record's name field.
func (r *Record) NameFieldType() FieldType {
return r.nameFieldType
}
// Name returns the record's name.
func (r *Record) Name() string {
f := r.NameField()
if f != nil {
dValue, deferred := f.value.(deferredValue)
if deferred {
return dValue.str
}
return r.NameField().String()
}
prefix := r.NamePrefix()
if prefix != "" {
return fmt.Sprintf("%s %d", prefix, r.rIndex+1)
}
names := r.Names()
if len(names) > 0 && r.rIndex < len(names) {
return names[r.rIndex]
}
return ""
}
func (r *Record) NamePrefix() string {
return r.rDesc.recordInfo.namePrefix
}
func (r *Record) Names() []string {
return r.rDesc.recordInfo.names
}
func (r *Record) MaxRecords() int {
return r.rDesc.recordInfo.max
}
// makeNameUnique renames the record to make it different than all of
// the passed names.
func (r *Record) makeNameUnique() error {
nameField := r.NameField()
if nameField == nil {
return nil
}
if r.Name() == "" {
return nil
}
name := r.Name()
existingRecordWithName := r.codeplug.FindRecordByName(r.rType, name)
if existingRecordWithName == nil {
return nil
}
if r == existingRecordWithName {
return nil
}
name = strings.TrimSpace(name)
baseName := strings.TrimRight(name, "0123456789")
suffix := strings.TrimPrefix(name, baseName)
if suffix == "" {
suffix = "2"
}
n64, err := strconv.ParseInt(suffix, 10, 32)
if err != nil {
l.Fatal("trailing digits not numeric")
}
n := int(n64)
maxNameLen := nameField.bitSize / 16
for {
suffix := fmt.Sprintf("%d", n)
for len(baseName)+len(suffix) > maxNameLen {
if len(baseName) <= 0 {
break
}
baseName = baseName[:len(baseName)-1]
}
newName := baseName + fmt.Sprintf("%d", n)
if r.codeplug.FindRecordByName(r.rType, newName) == nil {
nameField.value = newValue(nameField.ValueType())
nameField.value.setString(nameField, newName, false)
return nil
}
n += 1
}
return fmt.Errorf("too many record copies")
}
// ListNames returns a slice of the names of all records in the rDesc.
func (rd *rDesc) ListNames() *[]string {
lenCachedListNames := 0
if rd.cachedListNames != nil {
lenCachedListNames = len(*rd.cachedListNames)
}
recordsLen := len(rd.records)
if lenCachedListNames == 0 && recordsLen > 0 {
names := make([]string, recordsLen)
for i, r := range rd.records {
name := r.Name()
if name == "" {
name = rd.namePrefix + fmt.Sprintf("%d", i+1)
}
names[i] = name
}
rd.cachedListNames = &names
}
return rd.cachedListNames
}
// MemberListNames returns a slice of possible member list names
func (rd *rDesc) MemberListNames(filter func(r *Record) bool) *[]string {
if rd.rType != RtContacts {
if filter == nil {
return rd.ListNames()
}
names := make([]string, 0)
for i, r := range rd.records {
if !filter(r) {
continue
}
name := r.Name()
if name == "" {
name = rd.namePrefix + fmt.Sprintf("%d", i+1)
}
names = append(names, name)
}
return &names
}
names := make([]string, 0, len(rd.records))
for _, r := range rd.records {
if filter != nil && !filter(r) {
continue
}
typeField := r.Field(FtDcCallType)
if typeField.String() == "Group" {
names = append(names, r.Name())
}
}
return &names
}
// recordIsDeleted returns true if the record at rIndex is deleted.
func (rd *rDesc) recordIsDeleted(cp *Codeplug, rIndex int) bool {
dd := rd.delDesc
if dd == nil {
return false
}
offset := rd.offset + rIndex*rd.size + int(dd.offset)
for i := 0; i < int(dd.size); i++ {
if cp.bytes[offset+i] != dd.value {
return false
}
}
return true
}
// deleteRecord marks the record at rIndex as deleted.
func (rd *rDesc) deleteRecord(cp *Codeplug, rIndex int) {
dd := rd.delDesc
if dd == nil {
l.Fatal("can't delete record type:", rd.rType)
return
}
offset := rd.offset + rIndex*rd.size + int(dd.offset)
for i := 0; i < int(dd.size); i++ {
cp.bytes[offset+i] = dd.value
}
}
func (r *Record) NewFieldWithValue(fType FieldType, index int, str string) (*Field, error) {
f := r.NewField(fType)
f.fIndex = index
if f.mustDeferValue(str) {
f.deferValue(str)
return f, nil
}
err := f.setInitialString(str)
if err != nil {
return f, err
}
return f, nil
}
func (r *Record) newFieldWithDeferredValue(fType FieldType, index int, str string) *Field {
f := r.NewField(fType)
f.fIndex = index
f.deferValue(str)
return f
}
func (r *Record) MoveField(dIndex int, f *Field) {
sIndex := f.fIndex
r.RemoveField(f)
if sIndex < dIndex {
dIndex--
}
f.fIndex = dIndex
r.InsertField(f)
}
func (r *Record) InsertField(f *Field) error {
fType := f.fType
i := f.fIndex
fields := (*r.fDesc)[fType].fields
fields = append(fields[:i], append([]*Field{f}, fields[i:]...)...)
for i, f := range fields {
f.fIndex = i
}
(*r.fDesc)[fType].fields = fields
return nil
}
func (r *Record) RemoveField(f *Field) {
fType := f.fType
index := -1
fields := r.Fields(fType)
for i, field := range fields {
if field == f {
index = i
break
}
}
if index < 0 {
l.Fatal("RemoveField: bad field", f.String(), f.GetString())
}
deleteField(&fields, index)
for i, f := range fields {
f.fIndex = i
}
(*r.fDesc)[fType].fields = fields
}
func (or *Record) Copy() *Record {
r := new(Record)
r.rDesc = or.rDesc
r.rIndex = 0
for _, fType := range or.FieldTypes() {
for _, of := range or.Fields(fType) {
str := of.String()
str = removeSuffix(of, str)
str = AddSuffix(of, str)
f, _ := r.NewFieldWithValue(of.fType, of.fIndex, str)
f.resolveDeferredValue()
r.addField(f)
}
}
return r
}
func recordNames(records []*Record) []string {
names := make([]string, len(records))
for i, r := range records {
names[i] = r.Name()
}
return names
}
func (r *Record) FindFieldByName(fType FieldType, name string) *Field {
fields := (*r.fDesc)[fType].fields
for _, f := range fields {
if f.String() == name {
return f
}
}
return nil
}
func (r *Record) FindFieldsByName(fType FieldType, name string) []*Field {
foundFields := make([]*Field, 0)
fields := (*r.fDesc)[fType].fields
for _, f := range fields {
if f.String() == name {
foundFields = append(foundFields, f)
}
}
return foundFields
}
func (r *Record) HasFieldType(fType FieldType) bool {
for _, fi := range r.fieldInfos {
if fi.fType == fType {
return true
}
}
return false
}
func DependentRecords(records []*Record) (newRecords []*Record, depRecords []*Record) {
dRecsMap := make(map[string]bool)
for _, r := range records {
dRecsMap[r.FullTypeName()] = true
}
depRecords = make([]*Record, 0)
for _, r := range records {
depRecords = append(depRecords, r.dependentRecords(dRecsMap)...)
}
return records, depRecords
}
func (r *Record) dependentRecords(dRecsMap map[string]bool) []*Record {
dRecs := make([]*Record, 0)
for _, fType := range r.FieldTypes() {
fields := r.Fields(fType)
if len(fields) == 0 {
continue
}
rType := fields[0].listRecordType
if rType == "" {
continue
}
for _, f := range fields {
dr := r.codeplug.FindRecordByName(rType, f.String())
if dr == nil {
continue
}
drTypeName := dr.FullTypeName()
if dRecsMap[drTypeName] {
continue
}
dRecsMap[drTypeName] = true
dRecs = append(dRecs, dr.dependentRecords(dRecsMap)...)
dRecs = append(dRecs, dr)
}
}
return dRecs
}
func (r *Record) InCodeplug() bool {
records := r.codeplug.records(r.rType)
for _, rec := range records {
if rec == r {
return true
}
}
return false
}
func (r *Record) NameExists() bool {
name := r.Name()
rv := r.codeplug.FindRecordByName(r.rType, name) != nil
return rv
}
func fieldRefFields(cp *Codeplug, fieldRefs []fieldRef) []*Field {
fields := make([]*Field, 0)
for _, fRef := range fieldRefs {
fields = append(fields, cp.fields(fRef.rType, fRef.fType)...)
}
return fields
}
func (r *Record) fieldRefs() []fieldRef {
fieldRefs := make([]fieldRef, 0)
for _, fieldRef := range rTypeFieldRefs[r.rType] {
if r.codeplug.HasRecordType(fieldRef.rType) {
fieldRefs = append(fieldRefs, fieldRef)
}
}
return fieldRefs
}
func RecordsRemoved(change *Change) {
cp := change.Codeplug()
for _, r := range change.records {
name := r.Name()
for _, f := range fieldRefFields(cp, r.fieldRefs()) {
if f.listRecordType != r.rType {
continue
}
if f.String() != name {
continue
}
if f.max > 1 {
r := f.record
change := r.RemoveFieldsChange([]*Field{f})
r.RemoveField(f)
change.Complete()
continue
}
f.SetString(f.defaultValue)
}
}
}