-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNeo-CSV-Core.pck.st
1001 lines (808 loc) · 33.3 KB
/
Neo-CSV-Core.pck.st
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'From Cuis 6.0 [latest update: #5940] on 23 July 2023 at 1:46:22 pm'!
'Description Please enter a description for this package'!
!provides: 'Neo-CSV-Core' 1 2!
SystemOrganization addCategory: 'Neo-CSV-Core'!
!classDefinition: #NeoCSVReader category: 'Neo-CSV-Core'!
Object subclass: #NeoCSVReader
instanceVariableNames: 'readStream charBuffer separator stringStream fieldCount recordClass recordClassIsIndexable fieldAccessors emptyFieldValue'
classVariableNames: ''
poolDictionaries: ''
category: 'Neo-CSV-Core'!
!classDefinition: 'NeoCSVReader class' category: 'Neo-CSV-Core'!
NeoCSVReader class
instanceVariableNames: ''!
!classDefinition: #NeoCSVWriter category: 'Neo-CSV-Core'!
Object subclass: #NeoCSVWriter
instanceVariableNames: 'writeStream separator fieldWriter lineEnd fieldAccessors emptyFieldValue'
classVariableNames: ''
poolDictionaries: ''
category: 'Neo-CSV-Core'!
!classDefinition: 'NeoCSVWriter class' category: 'Neo-CSV-Core'!
NeoCSVWriter class
instanceVariableNames: ''!
!classDefinition: #NeoNumberParser category: 'Neo-CSV-Core'!
Object subclass: #NeoNumberParser
instanceVariableNames: 'stream base'
classVariableNames: ''
poolDictionaries: ''
category: 'Neo-CSV-Core'!
!classDefinition: 'NeoNumberParser class' category: 'Neo-CSV-Core'!
NeoNumberParser class
instanceVariableNames: ''!
!NeoCSVReader commentStamp: '<historical>' prior: 0!
I am NeoCSVReader.
I read a format that
- is text based (ASCII, Latin1, Unicode)
- consists of records, 1 per line (any line ending convention)
- where records consist of fields separated by a delimiter (comma, tab, semicolon)
- where every record has the same number of fields
- where fields can be quoted should they contain separators or line endings
Without further configuration, records will become Arrays of Strings.
By specifiying a recordClass and fields with optional converters most objects can be read and instanciated correctly.
MIT License.
!
!NeoCSVWriter commentStamp: '<historical>' prior: 0!
I am NeoCSVWriter.
I write a format that
- is text based (ASCII, Latin1, Unicode)
- consists of records, 1 per line (any line ending convention)
- where records consist of fields separated by a delimiter (comma, tab, semicolon)
- where every record has the same number of fields
- where fields can be quoted should they contain separators or line endings
Without further configuration, I write record objects whose fields can be enumerated using #do: such as SequenceableCollections
By specifiying fields any object can be written converting and/or quoting each field as needed.
MIT License.!
!NeoNumberParser commentStamp: '<historical>' prior: 0!
I am NeoNumberParser, an alternative number parser that needs only a minimal read stream protocol.
I accept the following syntax:
number
int
int frac
int exp
int frac exp
int
digits
- digits
frac
. digits
exp
e digits
digits
digit
digit digits
e
e
e+
e-
E
E+
E-
where digit depends on the base (2 to 36), 0 .. 9, A-Z, a-z.!
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/21/2012 22:35'!
addField
"Add the next indexable field with a pass through converter"
self
recordClassIsIndexable: true;
addFieldAccessor: [ :string | string ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 13:09'!
addField: accessor
"Add a field based on a mutator accessor accepting a field
String as argument to be sent to an instance of recordClass.
Accessor can be a Symbol or a Block"
self
recordClassIsIndexable: false;
addFieldAccessor: [ :object :string |
self applyAccessor: accessor on: object with: string ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 13:09'!
addField: accessor converter: converter
"Add a field based on a mutator accessor accepting the result of
applying the converter block on the field String read as argument
to be sent to an instance of recordClass.
Accessor can be a Symbol or a Block"
self
recordClassIsIndexable: false;
addFieldAccessor: [ :object :string |
self applyAccessor: accessor on: object with: (converter value: string) ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/26/2012 20:49'!
addFieldAt: key
"Add a field that will be stored under key in recordClass as String"
self
recordClassIsIndexable: false;
addFieldAccessor: [ :object :string |
object at: key put: string ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/26/2012 20:50'!
addFieldAt: key converter: converter
"Add a field that will be stored under key in recordClass as the result of
applying the converter block on the field String read as argument"
self
recordClassIsIndexable: false;
addFieldAccessor: [ :object :string |
object at: key put: (converter value: string) ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/21/2012 23:00'!
addFieldConverter: converter
"Add the next indexable field with converter block,
accepting a String and returning a specific object"
self
recordClassIsIndexable: true;
addFieldAccessor: converter! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/5/2013 11:06'!
addIgnoredField
"Add a field that should be ignored, should not become part of the record"
self addFieldAccessor: nil! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'EstebanMaringolo 10/3/2014 12:14'!
addIgnoredFields: count
"Add a count of consecutive ignored fields to receiver."
count timesRepeat: [ self addIgnoredField ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/14/2012 12:32'!
close
readStream ifNotNil: [
readStream close.
readStream := nil ]! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2014 15:32'!
emptyFieldValue: object
"Set the value to be used when reading empty or missing fields.
The default is nil. Empty or missing fields are never set
when the record class is non-indexeabe."
emptyFieldValue := object! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/21/2012 23:01'!
fieldCount: anObject
"Set the field count up front.
This will be used when reading records as Arrays.
This instance variable will be set and used automatically based on the first record seen.
If set, the fieldAccessors collection defines (overrides) the fieldCount."
fieldCount := anObject! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/21/2012 22:29'!
initialize
super initialize.
recordClass := Array.
recordClassIsIndexable := true.
separator := $,! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/25/2012 21:15'!
on: aReadStream
"Initialize on aReadStream, which should be a character stream that
implements #next, #atEnd and (optionally) #close."
readStream := aReadStream! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/5/2013 14:26'!
recordClass: anObject
"Set the object class to instanciate while reading records.
Unless the objets are integer indexable, you have to specify fields as well."
recordClass := anObject! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2014 15:32'!
recordClassIsIndexable: boolean
"Set whether recordClass should be treated as an indexable sequenceable collection
class that implements #new: and #streamContents and whose instances implement #at:put:
If false, fields accessors have to be provided. The default is true.
Will be set automatically when field accessors or converters are set."
recordClassIsIndexable := boolean ! !
!NeoCSVReader methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/17/2012 16:24'!
separator: character
"Set the field separator character to use, defaults to comma"
self assert: character isCharacter.
separator := character ! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/16/2012 13:33'!
addFieldAccessor: block
fieldAccessors
ifNil: [
fieldAccessors := Array with: block ]
ifNotNil: [
fieldAccessors := fieldAccessors copyWith: block ]! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 5/13/2013 10:56'!
applyAccessor: accessor on: object with: value
"Use accessor to assign value on a property of object.
Accessor can be a block or mutator symbol."
"If Symbol implemented #value:value: this could be implemented more elegantly."
accessor isBlock
ifTrue: [ accessor value: object value: value ]
ifFalse: [ object perform: accessor with: value ]! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/25/2012 20:28'!
nextChar
^ charBuffer
ifNil: [
readStream next ]
ifNotNil: [ | char |
char := charBuffer.
charBuffer := nil.
^ char ]! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/25/2012 20:23'!
peekChar
^ charBuffer
ifNil: [
charBuffer := readStream next ]! !
!NeoCSVReader methodsFor: 'private' stamp: 'hlsf 7/21/2023 23:25:59'!
peekEndOfLine
| char |
char := self peekChar codePoint.
^ (char == 10 "Character lf" ) or: [ char == 13 "Character cr" ]! !
!NeoCSVReader methodsFor: 'private' stamp: 'hlsf 7/23/2023 13:38:33'!
peekFor: character
self peekChar = character
ifTrue: [
self nextChar.
^ true ].
^ false! !
!NeoCSVReader methodsFor: 'private' stamp: 'hlsf 7/23/2023 13:38:28'!
peekQuote
^ self peekChar = $"! !
!NeoCSVReader methodsFor: 'private' stamp: 'hlsf 7/23/2023 13:38:21'!
peekSeparator
^ self peekChar = separator! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 1/15/2014 09:54'!
readAtEndOrEndOfLine
^ self atEnd or: [ self readEndOfLine ]
! !
!NeoCSVReader methodsFor: 'private' stamp: 'hlsf 7/21/2023 23:26:12'!
readEndOfLine
| char |
char := self peekChar codePoint.
char == 10 "Character lf"
ifTrue: [
self nextChar.
^ true ].
char == 13 "Character cr"
ifTrue: [
self nextChar.
(self atEnd not and: [ self peekChar codePoint == 10 "Character lf" ])
ifTrue: [
self nextChar ].
^ true ].
^ false
! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 5/13/2014 11:43'!
readEndOfQuotedField
(self readQuote and: [ self peekQuote not ])
ifTrue: [
^ true ].
"A double quote inside a quoted field is an embedded quote (escaped)"
^ false! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 1/14/2014 23:47'!
readQuote
^ self peekFor: $"! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 1/14/2014 23:47'!
readSeparator
^ self peekFor: separator! !
!NeoCSVReader methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/14/2012 12:32'!
stringStreamContents: block
"Like String streamContents: block
but reusing the underlying buffer for improved efficiency"
stringStream
ifNil: [
stringStream := (String new: 32) writeStream ].
stringStream reset.
block value: stringStream.
^ stringStream contents! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:30'!
addFields: accessors
"Add fields based on a collection of accessors, not doing any conversions."
accessors do: [ :each |
self addField: each ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:30'!
addFieldsAt: keys
"Add fields based on a collection of keys for #at:put: not doing any conversions"
keys do: [ :each |
self addFieldAt: each ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:34'!
addFloatField
"Add a field for indexable records parsed as Float"
self addFieldConverter: [ :string | NeoNumberParser parse: string ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:35'!
addFloatField: accessor
"Add a field with accessor parsed as Float"
self
addField: accessor
converter: [ :string | NeoNumberParser parse: string ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:49'!
addFloatFieldAt: key
"Add a field for key for #at:put: parsed as Float"
self
addFieldAt: key
converter: [ :string | NeoNumberParser parse: string ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:47'!
addIntegerField
"Add a field for indexable records parsed as Integer"
self addFieldConverter: [ :string | NeoNumberParser parse: string ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:48'!
addIntegerField: accessor
"Add a field with accessor parsed as Integer"
self
addField: accessor
converter: [ :string | NeoNumberParser parse: string ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:49'!
addIntegerFieldAt: key
"Add a field for key for #at:put: parsed as Integer"
self
addFieldAt: key
converter: [ :string | NeoNumberParser parse: string ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:48'!
addSymbolField
"Add a field for indexable records read as Symbol"
self addFieldConverter: [ :string | string asSymbol ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:48'!
addSymbolField: accessor
"Add a field with accessor read as Symbol"
self
addField: accessor
converter: [ :string | string asSymbol ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/5/2013 14:49'!
addSymbolFieldAt: key
"Add a field for key for #at:put: read as Symbol"
self
addFieldAt: key
converter: [ :string | string asSymbol ]! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/10/2015 21:59'!
namedColumnsConfiguration
"Assuming there is a header row that has not yet been read,
configure the receiver to read each row as a Dictionary where
each field is stored under a column name.
Note that each field is read as a string."
self recordClass: Dictionary.
self addFieldsAt: (self readHeader collect: [ :each | each asSymbol ])! !
!NeoCSVReader methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/13/2014 11:44'!
skipHeader
"Read a record, presumably a header, with the intention of skipping it.
This should normally be called only at the beginning and only once.
This sets the fieldCount (but fieldAccessors overrides fieldCount)."
self readHeader! !
!NeoCSVReader methodsFor: 'testing' stamp: 'SvenVanCaekenberghe 6/25/2012 20:47'!
atEnd
^ charBuffer == nil and: [ readStream atEnd ]! !
!NeoCSVReader methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 6/25/2012 14:45'!
do: block
"Execute block for each record until I am at end."
[ self atEnd ]
whileFalse: [
block value: self next ]! !
!NeoCSVReader methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 6/21/2012 22:30'!
next
"Read the next record.
I will return an instance of recordClass."
^ recordClassIsIndexable
ifTrue: [ self readNextRecordAsArray ]
ifFalse: [ self readNextRecordAsObject ]! !
!NeoCSVReader methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 5/13/2014 15:34'!
readHeader
"Read a record, presumably a header and return the header field names.
This should normally be called only at the beginning and only once.
This sets the fieldCount (but fieldAccessors overrides fieldCount)."
| names |
names := Array streamContents: [ :out |
[ self atEnd or: [ self readEndOfLine ] ]
whileFalse: [
out nextPut: self readField.
(self readSeparator and: [ self atEnd or: [ self peekEndOfLine ] ])
ifTrue: [ out nextPut: emptyFieldValue ] ] ].
fieldCount := names size.
^ names! !
!NeoCSVReader methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 10/6/2014 17:34'!
select: filter
"Read and collect records that satisfy filter into an Array until there are none left.
Return the array."
^ Array streamContents: [ :stream |
self
select: filter
thenDo: [ :each | stream nextPut: each ] ]! !
!NeoCSVReader methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 10/6/2014 17:33'!
select: filter thenDo: block
"Execute block for each record that satisfies filter until I am at end."
[ self atEnd ]
whileFalse: [
| record |
record := self next.
(filter value: record)
ifTrue: [ block value: record ] ]! !
!NeoCSVReader methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 6/25/2012 14:45'!
upToEnd
"Read and collect records into an Array until there are none left.
Return the array."
^ Array streamContents: [ :stream |
self do: [ :each | stream nextPut: each ] ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 6/14/2012 17:24'!
readField
^ self peekQuote
ifTrue: [
self readQuotedField ]
ifFalse: [
self readUnquotedField ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 1/15/2014 10:11'!
readFieldAndSeparator
| field |
field := self readField.
self readSeparator.
^ field! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 5/13/2014 15:34'!
readFirstRecord
"This is only used for array based records when there are no field accessors or
when there is no field count, to obtain a field count based on the first record"
^ recordClass streamContents: [ :stream |
[ self atEnd or: [ self readEndOfLine ] ]
whileFalse: [
stream nextPut: self readField.
(self readSeparator and: [ self atEnd or: [ self peekEndOfLine ] ])
ifTrue: [ stream nextPut: emptyFieldValue ] ] ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 1/15/2014 09:55'!
readNextRecord
| record |
record := recordClass new: fieldCount.
fieldAccessors
ifNil: [ self readNextRecordWithoutFieldAccessors: record ]
ifNotNil: [ self readNextRecordWithFieldAccessors: record ].
self readAtEndOrEndOfLine.
^ record! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 5/5/2013 13:17'!
readNextRecordAsArray
fieldAccessors ifNotNil: [
fieldCount := fieldAccessors count: [ :each | each notNil ] ].
^ fieldCount
ifNil: [ | record |
record := self readFirstRecord.
fieldCount := record size.
record ]
ifNotNil: [
self readNextRecord ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 5/13/2014 15:35'!
readNextRecordAsObject
| object |
object := recordClass new.
fieldAccessors do: [ :each | | rawValue |
rawValue := self readFieldAndSeparator.
"Note that empty/missing fields are not set"
(rawValue = emptyFieldValue or: [ each isNil ])
ifFalse: [ each value: object value: rawValue ] ].
self readAtEndOrEndOfLine.
^ object! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 9/18/2014 09:50'!
readNextRecordWithFieldAccessors: record
| fieldIndex |
fieldIndex := 1.
fieldAccessors do: [ :each | | rawValue |
rawValue := self readFieldAndSeparator.
"nil field accessors are used to ignore fields"
each
ifNotNil: [
rawValue = emptyFieldValue
ifTrue: [ record at: fieldIndex put: emptyFieldValue ]
ifFalse: [ record at: fieldIndex put: (each value: rawValue) ].
fieldIndex := fieldIndex + 1 ] ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 9/18/2014 09:42'!
readNextRecordWithoutFieldAccessors: record
1 to: fieldCount do: [ :each |
record at: each put: self readFieldAndSeparator ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 5/13/2014 15:36'!
readQuotedField
| field |
self readQuote.
field := self stringStreamContents: [ :stream |
[ self atEnd or: [ self readEndOfQuotedField ] ]
whileFalse: [
stream nextPut: self nextChar ] ].
^ field isEmpty
ifTrue: [ emptyFieldValue ]
ifFalse: [ field ]! !
!NeoCSVReader methodsFor: 'private - reading' stamp: 'SvenVanCaekenberghe 5/13/2014 15:36'!
readUnquotedField
(self atEnd or: [ self peekSeparator or: [ self peekEndOfLine ] ])
ifTrue: [ ^ emptyFieldValue ].
^ self stringStreamContents: [ :stream |
[ self atEnd or: [ self peekSeparator or: [ self peekEndOfLine ] ] ]
whileFalse: [
stream nextPut: self nextChar ] ]! !
!NeoCSVReader class methodsFor: 'instance creation' stamp: 'SvenVanCaekenberghe 6/25/2012 21:15'!
on: readStream
"Initialize on readStream, which should be a character stream that
implements #next, #atEnd and (optionally) #close."
^ self new
on: readStream;
yourself! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 11:09'!
addConstantField: string
"Add a constant field to be written using fieldWriter"
self addFieldAccessor: [ :object |
self writeField: string ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 11:09'!
addEmptyField
"Add an empty field to be written using fieldWriter"
self addFieldAccessor: [ :object |
self writeField: '' ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 13:10'!
addField: accessor
"Add a field based on an accessor to be written using fieldWriter.
Accessor can be a Symbol or a Block"
self addFieldAccessor: [ :object |
self writeField: (accessor value: object) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/23/2014 11:19'!
addFieldAt: key
"Add a field based on a key to be written using fieldWriter"
self addFieldAccessor: [ :object |
self writeField: (object at: key ifAbsent: [ '' ]) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 13:10'!
addObjectField: accessor
"Add a field based on an accessor to be written as an #object field.
Accessor can be a Symbol or a Block"
self addFieldAccessor: [ :object |
self writeObjectField: (accessor value: object) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/26/2012 20:30'!
addObjectFieldAt: key
"Add a field based on a key to be written as an #object field"
self addFieldAccessor: [ :object |
self writeObjectField: (object at: key) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 13:10'!
addQuotedField: accessor
"Add a field based on an accessor to be written as a #quoted field.
Accessor can be a Symbol or a Block"
self addFieldAccessor: [ :object |
self writeQuotedField: (accessor value: object) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/26/2012 20:30'!
addQuotedFieldAt: key
"Add a field based on a key to be written as a #quoted field"
self addFieldAccessor: [ :object |
self writeQuotedField: (object at: key) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 5/13/2013 13:10'!
addRawField: accessor
"Add a field based on an accessor to be written as a #raw field.
Accessor can be a Symbol or a Block"
self addFieldAccessor: [ :object |
self writeRawField: (accessor value: object) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/26/2012 20:31'!
addRawFieldAt: key
"Add a field based on a key to be written as a #raw field"
self addFieldAccessor: [ :object |
self writeRawField: (object at: key) ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/14/2012 10:10'!
close
writeStream ifNotNil: [
writeStream close.
writeStream := nil ]! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 9/18/2014 09:21'!
emptyFieldValue: object
"Set the empty field value to object.
When reading fields from records to be written out,
if the field value equals the emptyFieldValue,
it will be considered an empty field and written as such."
emptyFieldValue := object! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/17/2012 16:31'!
fieldWriter: symbol
"Set the field write to be used, either #quoted (the default), #raw or #object.
This determines how field values will be written in the general case.
#quoted will wrap fields #asString in double quotes and escape embedded double quotes
#raw will write fields #asString as such (no separator, double quote or end of line chars allowed)
#object will #print: fields (no separator, double quote or end of line chars allowed)"
self assert: (#(quoted raw object) includes: symbol).
fieldWriter := ('write', symbol capitalized, 'Field:') asSymbol
! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/15/2012 19:16'!
initialize
super initialize.
lineEnd := String crlfString.
separator := $, .
fieldWriter := #writeQuotedField:
! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/17/2012 16:25'!
lineEndConvention: symbol
"Set the end of line convention to be used.
Either #cr, #lf or #crlf (the default)."
self assert: (#(cr lf crlf) includes: symbol).
lineEnd := String perform: symbol! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/14/2012 09:48'!
on: aWriteStream
"Initialize on aWriteStream, which should be a character stream that
implements #nextPut:, #nextPutAll:, #space and (optionally) #close."
writeStream := aWriteStream
! !
!NeoCSVWriter methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 6/17/2012 16:24'!
separator: character
"Set character to be used as separator"
self assert: character isCharacter.
separator := character ! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/15/2012 23:03'!
addFieldAccessor: block
fieldAccessors
ifNil: [
fieldAccessors := Array with: block ]
ifNotNil: [
fieldAccessors := fieldAccessors copyWith: block ]! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/15/2012 19:16'!
writeEndOfLine
writeStream nextPutAll: lineEnd ! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/15/2012 12:47'!
writeField: object
self perform: fieldWriter with: object! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/15/2012 22:37'!
writeFieldsUsingAccessors: anObject
| first |
first := true.
fieldAccessors do: [ :each | | fieldValue |
first ifTrue: [ first := false ] ifFalse: [ self writeSeparator ].
fieldValue := each value: anObject ]! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/15/2012 19:45'!
writeFieldsUsingDo: anObject
| first |
first := true.
anObject do: [ :each |
first ifTrue: [ first := false ] ifFalse: [ self writeSeparator ].
self writeField: each ]! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 5/13/2014 15:51'!
writeObjectField: object
object = emptyFieldValue
ifFalse: [ object printOn: writeStream ]! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 5/13/2014 15:53'!
writeQuotedField: object
object = emptyFieldValue
ifTrue: [ writeStream nextPut: $" ; nextPut: $" ]
ifFalse: [ | string |
string := object asString.
writeStream nextPut: $".
string do: [ :each |
each == $"
ifTrue: [ writeStream nextPut: $"; nextPut: $" ]
ifFalse: [ writeStream nextPut: each ] ].
writeStream nextPut: $" ]! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 9/18/2014 09:20'!
writeRawField: object
object = emptyFieldValue
ifFalse: [ writeStream nextPutAll: object asString ]! !
!NeoCSVWriter methodsFor: 'private' stamp: 'SvenVanCaekenberghe 6/14/2012 10:03'!
writeSeparator
writeStream nextPut: separator ! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/16/2012 13:34'!
addFields: accessors
accessors do: [ :each |
self addField: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/26/2012 20:31'!
addFieldsAt: keys
keys do: [ :each |
self addFieldAt: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/15/2012 22:38'!
addObjectFields: accessors
accessors do: [ :each |
self addObjectField: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/26/2012 20:31'!
addObjectFieldsAt: keys
keys do: [ :each |
self addObjectFieldAt: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/15/2012 22:39'!
addQuotedFields: accessors
accessors do: [ :each |
self addQuotedField: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/26/2012 20:32'!
addQuotedFieldsAt: keys
keys do: [ :each |
self addQuotedFieldAt: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/15/2012 22:38'!
addRawFields: accessors
accessors do: [ :each |
self addRawField: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 6/26/2012 20:32'!
addRawFieldsAt: keys
keys do: [ :each |
self addRawFieldAt: each ]! !
!NeoCSVWriter methodsFor: 'convenience' stamp: 'SvenVanCaekenberghe 5/10/2015 21:51'!
namedColumnsConfiguration: columns
"Configure the receiver to output the named columns as keyed properties.
The objects to be written should respond to #at: like a Dictionary.
Writes a header first. Uses the configured field writer."
self writeHeader: columns.
self addFieldsAt: columns! !
!NeoCSVWriter methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 6/17/2012 19:39'!
flush
writeStream flush! !
!NeoCSVWriter methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 6/17/2012 16:23'!
nextPut: anObject
"Write anObject as single record.
Depending on configuration fieldAccessors or a #do: enumeration will be used."
fieldAccessors
ifNil: [
self writeFieldsUsingDo: anObject ]
ifNotNil: [
self writeFieldsUsingAccessors: anObject ].
self writeEndOfLine ! !
!NeoCSVWriter methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 6/17/2012 16:23'!
nextPutAll: collection
"Write a collection of objects as records"
collection do: [ :each |
self nextPut: each ]! !
!NeoCSVWriter methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 12/11/2013 21:47'!
writeHeader: fieldNames
"Write the header, a collection of field names.
This should normally be called only at the beginning and only once."
fieldNames
do: [ :each | self writeQuotedField: each ]
separatedBy: [ self writeSeparator ].
self writeEndOfLine! !
!NeoCSVWriter class methodsFor: 'instance creation' stamp: 'SvenVanCaekenberghe 6/14/2012 09:47'!
on: writeStream
"Initialize on writeStream, which should be a character stream that
implements #nextPut:, #nextPutAll:, #space and (optionally) #close."
^ self new
on: writeStream;
yourself! !
!NeoNumberParser methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 12/2/2012 13:44'!
base: integer
self assert: (integer between: 2 and: 36) description: 'Number base must be between 2 and 36'.
base := integer! !
!NeoNumberParser methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 12/2/2012 13:49'!
initialize
super initialize.
self base: 10! !
!NeoNumberParser methodsFor: 'initialize-release' stamp: 'SvenVanCaekenberghe 12/2/2012 13:43'!
on: readStream
stream := readStream ! !
!NeoNumberParser methodsFor: 'parsing' stamp: 'SvenVanCaekenberghe 12/2/2012 17:30'!
consumeWhitespace
"Strip whitespaces from the input stream."
[ stream atEnd not and: [ stream peek isSeparator ] ]
whileTrue: [ stream next ]
! !
!NeoNumberParser methodsFor: 'parsing' stamp: 'SvenVanCaekenberghe 12/3/2012 11:00'!
parseNumber
| negated number |
negated := stream peekFor: $-.
number := self parseNumberInteger.
(stream peekFor: $.)
ifTrue: [ number := number + self parseNumberFraction ].
((stream peekFor: $e) or: [ stream peekFor: $E ])
ifTrue: [ number := number * self parseNumberExponent ].
negated
ifTrue: [ number := number negated ].
^ number! !
!NeoNumberParser methodsFor: 'parsing' stamp: 'SvenVanCaekenberghe 12/2/2012 20:06'!
parseNumberExponent
| number negated |
number := 0.
(negated := stream peekFor: $-)
ifFalse: [ stream peekFor: $+ ].
[ stream atEnd not and: [ stream peek digitValue between: 0 and: base - 1 ] ]
whileTrue: [ number := base * number + stream next digitValue ].
negated
ifTrue: [ number := number negated ].
^ base raisedTo: number! !
!NeoNumberParser methodsFor: 'parsing' stamp: 'SvenVanCaekenberghe 12/2/2012 20:06'!
parseNumberFraction
| number power |
number := 0.
power := 1.0.
[ stream atEnd not and: [ stream peek digitValue between: 0 and: base - 1 ] ]
whileTrue: [
number := base * number + stream next digitValue.
power := power * base ].
^ number / power! !
!NeoNumberParser methodsFor: 'parsing' stamp: 'SvenVanCaekenberghe 12/2/2012 20:06'!
parseNumberInteger
| number |
number := nil.
[ stream atEnd not and: [ stream peek digitValue between: 0 and: base - 1 ] ]
whileTrue: [ number := base * (number ifNil: [ 0 ]) + stream next digitValue ].
number ifNil: [ self error: 'Integer digit expected' ].
^ number! !
!NeoNumberParser methodsFor: 'accessing' stamp: 'SvenVanCaekenberghe 12/2/2012 13:31'!
next
^ self parseNumber! !
!NeoNumberParser class methodsFor: 'instance creation' stamp: 'SvenVanCaekenberghe 12/2/2012 13:31'!
on: readStream
^ self new
on: readStream;
yourself! !
!NeoNumberParser class methodsFor: 'queries' stamp: 'SvenVanCaekenberghe 12/2/2012 13:35'!
parse: stringOrStream
| stream |
stream := stringOrStream isString
ifTrue: [ stringOrStream readStream ]
ifFalse: [ stringOrStream ].
^ (self on: stream) next! !
!NeoNumberParser class methodsFor: 'queries' stamp: 'SvenVanCaekenberghe 12/2/2012 13:51'!
parse: stringOrStream base: base
| stream |
stream := stringOrStream isString
ifTrue: [ stringOrStream readStream ]
ifFalse: [ stringOrStream ].
^ (self on: stream)
base: base;
next! !
!NeoNumberParser class methodsFor: 'queries' stamp: 'SvenVanCaekenberghe 12/2/2012 17:43'!
parse: stringOrStream base: base ifFail: block
^ [ self parse: stringOrStream base: base ]
on: Error
do: block! !
!NeoNumberParser class methodsFor: 'queries' stamp: 'SvenVanCaekenberghe 12/2/2012 17:43'!
parse: stringOrStream ifFail: block
^ [ self parse: stringOrStream ]
on: Error