forked from PolMine/RcppCWB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes.c
More file actions
1453 lines (1236 loc) · 43.1 KB
/
Copy pathattributes.c
File metadata and controls
1453 lines (1236 loc) · 43.1 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
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
/*
* IMS Open Corpus Workbench (CWB)
* Copyright (C) 1993-2006 by IMS, University of Stuttgart
* Copyright (C) 2007- by the respective contributers (see file AUTHORS)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any later
* version.
*
* This program 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 General
* Public License for more details (in the file "COPYING", or available via
* WWW at http://www.gnu.org/copyleft/gpl.html).
*/
void Rprintf(const char *, ...);
#include <ctype.h>
#include <sys/types.h>
#include "globals.h"
#include "endian2.h"
#include "corpus.h"
#include "fileutils.h"
#include "attributes.h"
#include "makecomps.h"
#ifdef __MINGW__
#undef SUBDIR_SEPARATOR
#undef SUBDIR_SEP_STRING
#define SUBDIR_SEPARATOR '/'
#define SUBDIR_SEP_STRING "/"
#endif
/*
*******************************************************************
* CONSTANTS controlling how ensure_component() behaves.
*******************************************************************
*/
/**
* if CL_ENSURE_COMPONENT_EXITS is defined, ensure_component will exit
* when the component can't be created or loaded.
*
* @see ensure_component
*/
#if 0
#define CL_ENSURE_COMPONENT_EXITS
#endif
/**
* if CL_ENSURE_COMPONENT_ALLOW_CREATION is defined, components may be created
* on the fly by ensure_component.
*
* @see ensure_component
*/
#if 0
#define CL_ENSURE_COMPONENT_ALLOW_CREATION
#endif
/**
* if KEEP_SILENT is defined, ensure_component won't complain about
* non-accessible data.
*
* @see ensure_component
*/
#define CL_ENSURE_COMPONENT_KEEP_SILENT
/*******************************************************************/
/**
* The component_field_spec data type.
*
* @see Component_Field_Specs
*/
typedef struct component_field_spec {
ComponentID id; /**< the specifier for what kind of blob of info this component is; also used
as the index for this component in its Attribute's component array. */
char *name; /**< String used as the label for this component (abbreviation of the
relevant label from in the ComponentID enumeration). */
int using_atts; /**< The attribute type of the Attributes that use this component */
char *default_path; /**< The default location of the file corresponding to this component;
can contain variables ($DIR=directory, $ANAME=attribute name) */
} component_field_spec;
/**
* Global object in the "attributes" module, giving specifications
* for each component in the array of components that each Attribute
* object contains.
*/
static struct component_field_spec Component_Field_Specs[] =
{
{ CompDirectory, "DIR", ATT_ALL, "$APATH"},
{ CompCorpus, "CORPUS", ATT_POS, "$DIR" SUBDIR_SEP_STRING "$ANAME.corpus"},
{ CompRevCorpus, "REVCORP", ATT_POS, "$CORPUS.rev"},
{ CompRevCorpusIdx, "REVCIDX", ATT_POS, "$CORPUS.rdx"},
{ CompCorpusFreqs, "FREQS", ATT_POS, "$CORPUS.cnt"},
{ CompLexicon, "LEXICON", ATT_POS, "$DIR" SUBDIR_SEP_STRING "$ANAME.lexicon"},
{ CompLexiconIdx, "LEXIDX", ATT_POS, "$LEXICON.idx"},
{ CompLexiconSrt, "LEXSRT", ATT_POS, "$LEXICON.srt"},
{ CompAlignData, "ALIGN", ATT_ALIGN, "$DIR" SUBDIR_SEP_STRING "$ANAME.alg"},
{ CompXAlignData, "XALIGN", ATT_ALIGN, "$DIR" SUBDIR_SEP_STRING "$ANAME.alx"},
{ CompStrucData, "STRUC", ATT_STRUC, "$DIR" SUBDIR_SEP_STRING "$ANAME.rng"},
{ CompStrucAVS, "STRAVS", ATT_STRUC, "$DIR" SUBDIR_SEP_STRING "$ANAME.avs"},
{ CompStrucAVX, "STRAVX", ATT_STRUC, "$DIR" SUBDIR_SEP_STRING "$ANAME.avx"},
{ CompHuffSeq, "CIS", ATT_POS, "$DIR" SUBDIR_SEP_STRING "$ANAME.huf"},
{ CompHuffCodes, "CISCODE", ATT_POS, "$DIR" SUBDIR_SEP_STRING "$ANAME.hcd"},
{ CompHuffSync, "CISSYNC", ATT_POS, "$CIS.syn"},
{ CompCompRF, "CRC", ATT_POS, "$DIR" SUBDIR_SEP_STRING "$ANAME.crc"},
{ CompCompRFX, "CRCIDX", ATT_POS, "$DIR" SUBDIR_SEP_STRING "$ANAME.crx"},
{ CompLast, "INVALID", 0, "INVALID"}
};
/* ---------------------------------------------------------------------- */
static ComponentState work_out_component_state(Component *component);
static int comp_drop_component(Component *component);
/* ---------------------------------------------------------------------- */
/**
* Gets the specification for the named component field.
*
* This function returns a pointer to an element of the global, static
* Component_Field_Specs array.
*
* @param name A string that identifies the component field to be looked up.
* @return Pointer to the desired specification, or NULL if not found.
* @see Component_Field_Specs
*/
struct component_field_spec *
find_cid_name(char *name)
{
int i;
for (i = 0; i < CompLast; i++) {
if (cl_streq(Component_Field_Specs[i].name, name))
return &Component_Field_Specs[i];
}
return NULL;
}
/**
* Gets the specification for the identified component field.
*
* This function returns a pointer to an element of the global, static
* Component_Field_Specs array.
*
* @param id The ComponentID for the component field to be looked up.
* @return Pointer to the desired specification, or NULL if not found.
* @see Component_Field_Specs
*/
struct component_field_spec *
find_cid_id(ComponentID id)
{
if (id < CompLast)
return &Component_Field_Specs[id];
else
return NULL;
}
/**
* Gets a string containing the name of the attribute component
* with the specified ID-code.
*/
char *
cid_name(ComponentID id)
{
component_field_spec *spec = find_cid_id(id);
return (spec == NULL ? "((NULL))" : spec->name);
}
/**
* Gets the identifier of the attribute component with the specified name.
*/
ComponentID
component_id(char *name)
{
component_field_spec *spec = find_cid_name(name);
return (spec == NULL ? CompLast : spec->id);
}
/**
* Checks whether a particular Attribute type can possess
* the specified component field.
*
* this function does not appear to be used anywhere ??
*
* @return Boolean.
*/
int
MayHaveComponent(int attr_type, ComponentID cid)
{
component_field_spec *spec = find_cid_id(cid);
if (spec && spec->id != CompLast)
return (spec->using_atts & attr_type) ? 1 : 0;
return 0;
}
/**
* Gets a string containing a description of the specified attribute type.
*
* Non-exported function.
*
* @param att_type The attribute-type whose name is required.
* (Should be one of the values of the constants
* defined in cl.h.)
* @return String (pointer to internal constant string,
* do not change or free).
*/
static char *
aid_name(int att_type)
{
switch (att_type) {
case ATT_NONE: return "NONE (ILLEGAL)"; break;
case ATT_POS: return "Positional Attribute"; break;
case ATT_STRUC: return "Structural Attribute"; break;
case ATT_ALIGN: return "Alignment Attribute"; break;
case ATT_DYN: return "Dynamic Attribute"; break;
default: return "ILLEGAL ATTRIBUTE TYPE"; break;
}
/* NOTREACHED */
return NULL;
}
/**
* Gets a string containing a description of the specified dynamic attribute argument type.
*
* @param argtype The argument-type whose name is required.
* (Should be one of the values of the constants defined in cl.h.)
* @return String (pointer to internal constant string, do not change or free).
*/
static char *
argid_name(int argtype)
{
switch (argtype) {
case ATTAT_NONE: return "NONE(ILLEGAL)"; break;
case ATTAT_POS: return "CorpusPosition"; break;
case ATTAT_STRING: return "String"; break;
case ATTAT_VAR: return "Variable[StringList]"; break;
case ATTAT_INT: return "Integer"; break;
case ATTAT_FLOAT: return "Float"; break;
case ATTAT_PAREF: return "PARef"; break;
default: return "ILLEGAL*ARGUMENT*TYPE"; break;
}
/* NOTREACHED */
return NULL;
}
/**
* Creates a DynArg object.
*
* The object created is a dynamic argument of the type specified by the argument type_id,
* with its "next" pointer set to NULL.
*
* @see DynArg
* @param type_id String specifying the type of argument required; choose from:
* STRING, POS, INT, VARARG, FLOAT
* @return Pointer to the new DynArg object, or NULL in case of an invalid
* type_id.
*/
DynArg *
makearg(char *type_id)
{
DynArg *arg = (DynArg *)cl_malloc(sizeof(DynArg));
arg->next = NULL;
if (cl_str_is(type_id, "STRING"))
arg->type = ATTAT_STRING;
else if (cl_str_is(type_id, "POS"))
arg->type = ATTAT_POS;
else if (cl_str_is(type_id, "INT"))
arg->type = ATTAT_INT;
else if (cl_str_is(type_id, "VARARG"))
arg->type = ATTAT_VAR;
else if (cl_str_is(type_id, "FLOAT"))
arg->type = ATTAT_FLOAT;
else
cl_free(arg);
return arg;
}
/* ---------------------------------------------------------------------- */
/**
* Sets up a corpus attribute.
*
* NEVER CALL THIS!! ONLY USED WHILE PARSING A REGISTRY ENTRY!!!!
*
* @param corpus The corpus this attribute belongs to.
* @param attribute_name The name of the attribute (i.e. the handle it has in the registry file).
* @param type Type of attribute to be created.
* @param data Unused. It can just be NULL.
*/
Attribute *
setup_attribute(Corpus *corpus, char *attribute_name, int type, char *data)
{
ComponentID cid;
Attribute *attr = NULL;
Attribute *prev;
/* count of attributes that the corpus possesses already, including the default;
* used to calculate this attribute's attr_number value. */
int a_num;
if (NULL != cl_new_attribute(corpus, attribute_name, type)) {
Rprintf("attributes:setup_attribute(): Warning: \n"
" Attribute %s of type %s already defined in corpus %s\n",
attribute_name, aid_name(type), corpus->id);
return NULL;
}
attr = (Attribute *)cl_malloc(sizeof(Attribute));
attr->type = type;
attr->any.mother = corpus;
attr->any.name = attribute_name;
for (cid = CompDirectory; cid < CompLast; cid++)
attr->any.components[cid] = NULL;
/* if we're setting up "word", the attr_number will be 0; otherwise, start counting at 1 */
a_num = (cl_str_is(attribute_name, CWB_DEFAULT_ATT_NAME) && type == ATTAT_POS) ? 0 : 1;
/* insert at end of attribute list */
attr->any.next = NULL;
if (corpus->attributes == NULL)
corpus->attributes = attr;
else {
for (prev = corpus->attributes ; prev->any.next ; prev = prev->any.next)
a_num++;
prev->any.next = attr;
}
attr->any.attr_number = a_num;
attr->any.path = NULL;
/* ======================================== type specific initialization */
switch (attr->type) {
case ATT_POS:
attr->pos.hc = NULL;
attr->pos.this_block_nr = -1;
break;
case ATT_STRUC:
attr->struc.has_attribute_values = -1; /* not yet known */
break;
default:
break;
}
return attr;
}
/**
* Finds an attribute that matches the specified parameters, if one exists,
* for the given corpus.
*
* Note that although this is a cl_new_* function, and it is the canonical way
* that we get an Attribute to call Attribute-functions on, it doesn't actually
* create any kind of object. The Attribute exists already as one of the dependents
* of the Corpus object; this function simply locates it and returns a pointer
* to it.
*
* @see cl_new_attribute_oldstyle
*
* @param corpus The corpus in which to search for the attribute.
* @param attribute_name The name of the attribute (i.e. the handle it has in the registry file).
* @param type Type of attribute to be searched for.
*
* @return Pointer to Attribute object, or NULL if not found.
*/
Attribute *
cl_new_attribute(Corpus *corpus, const char *attribute_name, int type)
{
Attribute *attr = NULL;
if (!corpus)
Rprintf("attributes:cl_new_attribute(): called with NULL corpus\n");
else
for (attr = corpus->attributes ; attr ; attr = attr->any.next)
if (type == attr->type && cl_streq(attr->any.name, attribute_name))
break;
return attr;
}
/**
* Deletes the specified Attribute object.
*
* The function also appropriately amends the Corpus object of which this
* Attribute is a dependent. This means you can call it repreatedly on the first
* element of a Corpus's Attribute list (as the linked list is automatically
* adjusted).
*
* @return Boolean: true for all OK, false for a problem.
*/
int
cl_delete_attribute(Attribute *attribute)
{
Attribute *prev = NULL;
DynArg *arg;
Corpus *corpus;
ComponentID cid;
if (!attribute)
return 0;
corpus = attribute->any.mother;
assert("NULL corpus in attribute" && (corpus != NULL));
/* remove attribute from corpus attribute list */
if (attribute == corpus->attributes)
corpus->attributes = attribute->any.next;
else {
for (prev = corpus->attributes ; prev ; prev = prev->any.next)
if (prev->any.next == attribute)
break;
if (prev == NULL)
Rprintf("attributes:cl_delete_attribute():\n Warning: Attribute %s not in list of corpus attributes\n", attribute->any.name);
else {
assert("Error in attribute chain" && prev->any.next == attribute);
prev->any.next = attribute->any.next;
}
}
/* get rid of components */
for (cid = CompDirectory; cid < CompLast; cid++)
if (attribute->any.components[cid])
comp_drop_component(attribute->any.components[cid]);
cl_free(attribute->any.name);
cl_free(attribute->any.path);
/* get rid of special fields */
switch (attribute->type) {
case ATT_POS:
cl_free(attribute->pos.hc);
break;
case ATT_DYN:
cl_free(attribute->dyn.call);
while (attribute->dyn.arglist != NULL) {
arg = attribute->dyn.arglist;
attribute->dyn.arglist = arg->next;
cl_free(arg);
}
break;
default:
break;
}
cl_free(attribute);
return 1;
}
/**
* Accessor function to get the mother corpus of the attribute.
*/
Corpus *
cl_attribute_mother_corpus(Attribute *attribute)
{
return attribute->any.mother;
}
/**
* Sets up a component for the given attribute.
*
* If the component of the specified ComponentID does not already exist,
* a new Component object is created, set up, and assigned to the attribute's
* component array. Finally, the component path is initialised using the
* path argument.
*
* @see component_full_name
* @param attribute The Attribute for which to create this component.
* @param cid The ID of the component to create.
* @param path Path to be passed to component_full_name. Can be NULL.
* @return The new component if all is OK. If a component with the
* specified ID already exists, it is returned and no new
* component is created (and a warning message is printed
* to STDERR). If the attribute is NULL, return is NULL
* (and a warning is printed).
*/
Component *
declare_component(Attribute *attribute, ComponentID cid, char *path)
{
Component *component;
if (attribute == NULL) {
Rprintf("attributes:declare_component(): \n NULL attribute passed in declaration of %s component\n", cid_name(cid));
return NULL;
}
if (!(component = attribute->any.components[cid])) {
component = (Component *)cl_malloc(sizeof(Component));
component->id = cid;
component->corpus = attribute->any.mother;
component->attribute = attribute;
component->path = NULL;
init_mblob(&(component->data));
attribute->any.components[cid] = component;
/* we can then initialize the component path within the attribute */
component_full_name(attribute, cid, path);
}
else
Rprintf("attributes:declare_component(): Warning:\n Component %s of %s declared twice\n", cid_name(cid), attribute->any.name);
return component;
}
/**
* Sets up a default set of components on the given attribute.
*
* Note that in each case, a call is made to declare_component
* with the path as NULL.
*
* @see declare_component
*/
void
declare_default_components(Attribute *attribute)
{
int i;
if (attribute == NULL)
Rprintf("attributes:declare_default_components(): \n NULL attribute passed -- can't create defaults\n");
else
for (i = CompDirectory; i < CompLast; i++)
if (0 != (Component_Field_Specs[i].using_atts & attribute->type) && NULL == attribute->any.components[i])
declare_component(attribute, i, NULL);
}
/**
* Works out and returns the state of the component.
* For public interface see component_state().
*/
static ComponentState
work_out_component_state(Component *comp)
{
assert(comp);
if (comp->data.data != NULL)
return ComponentLoaded;
else if (comp->id == CompDirectory)
return ComponentDefined;
else if (comp->path == NULL)
return ComponentUndefined;
else if (file_length(comp->path) < 0) /* access error == EOF -> assume file doesn't exist */
return ComponentDefined;
else
return ComponentUnloaded;
}
/**
* Gets the state of a specified component on the given attribute.
*
* @param attribute The attribute to look at.
* @param cid The component whose state to get.
* @return The return value in case the component is not
* found is ComponentUndefined. Otherwise, some
* other value of ComponentState.
*/
ComponentState
component_state(Attribute *attribute, ComponentID cid)
{
if (attribute && cid < CompLast) {
Component *comp = attribute->any.components[cid];
if (comp == NULL)
return ComponentUndefined;
else
return work_out_component_state(comp);
}
else
return ComponentUndefined;
}
/**
* Initializes the path of an attribute Component.
*
* This function starts with the path it is passed, and then evaluates variables
* in the form $UPPERCASE. The resulting path is assigned to the specified
* entry in the component array for the given Attribute.
*
* Note that if it is called for a Component that does not yet exist, this function
* creates the component by calling declare_component().
*
* @see declare_component
* @see Component_Field_Specs
* @param attribute The Attribute object to work with.
* @param cid The identifier of the Component to which the path is to
* be added.
* @param path The path to assign to the component. Can be NULL,
* in which case, the default path from Component_Field_Specs
* is used.
* @return Pointer to this function's static buffer for creating the
* path (NB: NOT to the path in the actual component! which is a copy).
* If a path already exists, a pointer to that path.
* Either way, don't muck about with the buffer content.
* NULL in case of error in Component_Field_Specs.
*/
char *
component_full_name(Attribute *attribute, ComponentID cid, char *path)
{
component_field_spec *compspec;
Component *component;
static char buf[CL_MAX_LINE_LENGTH] = { '\0' };
char rname[CL_MAX_LINE_LENGTH] = { '\0' };
char *reference;
char c;
/* index into strings "path", "buf", "reference"/"rname" */
int ppos = 0, bpos = 0, rpos = 0;
/* did we do the job before? */
if ((component = attribute->any.components[cid]) != NULL && component->path != NULL)
return component->path;
/* component is so far undeclared. So try to guess the name: */
compspec = NULL;
if (path == NULL) {
if (!(compspec = find_cid_id(cid))) {
Rprintf("attributes:component_full_name(): Warning:\n"
" can't find component table entry for Component #%d\n", cid);
return NULL;
}
path = compspec->default_path;
}
while ((c = path[ppos]) != '\0') {
if (c == '$') {
/* the $ is a reference to the name of another component. */
rpos = 0;
c = path[++ppos]; /* first skip the '$' */
while (isupper(c)) {
rname[rpos++] = c;
c = path[++ppos]; /* now, move over the reference while copying its name */
}
rname[rpos] = '\0';
/* ppos now points to the first character after the reference;
* rname holds the UPPERCASE name of the referenced component */
reference = NULL;
if (cl_str_is(rname, "HOME"))
reference = getenv(rname);
else if (cl_str_is(rname, "APATH"))
reference = (attribute->any.path ? attribute->any.path : attribute->any.mother->path);
else if (cl_str_is(rname, "ANAME"))
reference = attribute->any.name;
else if ((compspec = find_cid_name(rname)) != NULL)
reference = component_full_name(attribute, compspec->id, NULL);
if (!reference) {
Rprintf("attributes:component_full_name(): Warning:\n Can't reference to the value of %s -- copying\n", rname);
reference = rname;
}
/* the reference is copied into buf */
for (rpos = 0; reference[rpos] != '\0'; rpos++, bpos++)
buf[bpos] = reference[rpos];
}
else {
/* just copy the character to the buffer, and advance */
buf[bpos] = c;
bpos++;
ppos++;
}
}
buf[bpos] = '\0';
if (component != NULL)
component->path = cl_strdup(buf);
else
declare_component(attribute, cid, buf);
/* and return it */
return &buf[0];
/* ?? why is buf returned instead of component->path, as earlier in the function? -- AH 16/9/09 */
}
/**
* Loads the specified component for this attribute.
*
* "Loading" means that the file specified by the component's "path" member
* is read into the "data" member.
*
* If the component is CompHuffCodes, part of the data is also copied to the
* attribute's pos.hc member (that is, the beginning of the file).
*
* Note that the action of this function is dependent on the component's state.
* If the component's state is ComponentUnloaded, the component is loaded.
* If the component's state is ComponentDefined, the size is set to 0 and
* nothing else is done.
*
* @param attribute The Attribute object to work with.
* @param cid The identifier of the Component to load.
* @return Pointer to the component. This will be NULL if the
* component has not been declared (i.e. created).
*/
Component *
load_component(Attribute *attribute, ComponentID cid)
{
Component *comp;
ComponentState state;
assert(attribute != NULL && "Null attribute passed to load_component");
if (NULL == (comp = attribute->any.components[cid])) {
Rprintf("attributes:load_component(): Warning:\n Component %s is not declared for %s attribute\n", cid_name(cid), aid_name(attribute->type));
return NULL;
}
state = work_out_component_state(comp);
if (ComponentUnloaded == state) {
assert(comp->path != NULL);
if (cid == CompHuffCodes) {
if (cl_sequence_compressed(attribute)) {
if (read_file_into_blob(comp->path, CL_MEMBLOB_MMAPPED, sizeof(int), &(comp->data)) == 0)
Rprintf("attributes:load_component(): Warning:\n Data of %s component of attribute %s can't be loaded\n", cid_name(cid), attribute->any.name);
else {
int i;
if (attribute->pos.hc != NULL)
Rprintf("attributes:load_component: WARNING:\n\tHCD block already loaded, overwritten.\n");
attribute->pos.hc = (HCD *)cl_malloc(sizeof(HCD));
memcpy(attribute->pos.hc, comp->data.data, sizeof(HCD));
/* convert network byte order to native integers */
attribute->pos.hc->size = ntohl(attribute->pos.hc->size);
attribute->pos.hc->length = ntohl(attribute->pos.hc->length);
attribute->pos.hc->min_codelen = ntohl(attribute->pos.hc->min_codelen);
attribute->pos.hc->max_codelen = ntohl(attribute->pos.hc->max_codelen);
for (i = 0; i < MAXCODELEN; i++) {
attribute->pos.hc->lcount[i] = ntohl(attribute->pos.hc->lcount[i]);
attribute->pos.hc->symindex[i] = ntohl(attribute->pos.hc->symindex[i]);
attribute->pos.hc->min_code[i] = ntohl(attribute->pos.hc->min_code[i]);
}
attribute->pos.hc->symbols = comp->data.data + (4+3*MAXCODELEN);
comp->size = attribute->pos.hc->length;
assert(work_out_component_state(comp) == ComponentLoaded);
}
}
else
Rprintf("attributes/load_component: missing files of compressed PA,\n\tcomponent CompHuffCodes not loaded\n");
}
else if (cid > CompDirectory && cid < CompLast) {
/* i.e. any ComponentID value except CompDirectory / CompLast and CompHuffCodes */
if (!read_file_into_blob(comp->path, CL_MEMBLOB_MMAPPED, sizeof(int), &(comp->data)))
Rprintf("attributes:load_component(): Warning:\n Data of %s component of attribute %s can't be loaded\n", cid_name(cid), attribute->any.name);
else {
comp->size = comp->data.nr_items;
assert(work_out_component_state(comp) == ComponentLoaded);
}
}
}
else if (ComponentDefined == state)
comp->size = 0;
return comp;
}
/**
* Creates the specified component for the given Attribute.
*
* This function only works for the following components:
* CompRevCorpus, CompRevCorpusIdx, CompLexiconSrt, CompCorpusFreqs.
* Also, it only works if the state of the component is
* ComponentDefined.
*
* "Create" here means create the CWB data files. This is accomplished by
* calling one of the "creat_*" functions, of which there is one for each
* of the four available component types. These are defined in makecomps.c.
*
* Each of these functions reads in the data it needs, processes it, and then
* writes a new file.
*
* @param attribute The Attribute object to work with.
* @param cid The identifier of the Component to create.
*
* @return Pointer to the component created, or NULL in case of
* error (e.g. if an invalid/undefined component was requested).
*
*/
Component *
create_component(Attribute *attribute, ComponentID cid)
{
Component *comp = attribute->any.components[cid];
if (cl_debug)
Rprintf("Creating %s\n", cid_name(cid));
if (component_state(attribute, cid) != ComponentDefined)
return NULL;
else {
assert(comp != NULL);
assert(comp->data.data == NULL);
assert(comp->path != NULL);
switch (cid) {
case CompLast:
case CompDirectory:
/* cannot create these */
break;
case CompCorpus:
case CompLexicon:
case CompLexiconIdx:
Rprintf("attributes:create_component(): Warning:\n"
" Can't create the '%s' component. Use 'cwb-encode' to create it out of a text file\n", cid_name(cid));
return NULL;
case CompHuffSeq:
case CompHuffCodes:
case CompHuffSync:
Rprintf("attributes:create_component(): Warning:\n"
" Can't create the '%s' component. Use 'cwb-huffcode' to create it out of an item sequence file\n", cid_name(cid));
return NULL;
case CompCompRF:
case CompCompRFX:
Rprintf("attributes:create_component(): Warning:\n"
" Can't create the '%s' component. Use 'cwb-compress-rdx' to create it out of the reversed file index\n", cid_name(cid));
return NULL;
case CompRevCorpus:
creat_rev_corpus(comp);
break;
case CompRevCorpusIdx:
creat_rev_corpus_idx(comp);
break;
case CompLexiconSrt:
creat_sort_lexicon(comp);
break;
case CompCorpusFreqs:
creat_freqs(comp);
break;
case CompAlignData:
case CompXAlignData:
case CompStrucData:
case CompStrucAVS:
case CompStrucAVX:
Rprintf("attributes:create_component(): Warning:\n"
" Can't create the '%s' component of %s attribute %s.\nUse the appropriate external tool to create it.\n",
cid_name(cid), aid_name(attribute->type), attribute->any.name);
return NULL;
default:
comp = NULL;
Rprintf("attributes:create_component(): Unknown cid: %d\n", cid);
assert(0);
break;
}
return comp;
}
}
/**
* Ensures that a component is loaded and ready.
*
* The state of the component specified should be ComponentLoaded
* once this function has run (assuming all is well). If the
* component is unloaded, the function will try to load it. If the
* component is defined, the function MAY try to create it. If the
* component is undefined, nothing will be done.
*
* There are flags in attributes.c that control the behaviour of
* this function (e.g. if failure to ensure causes the program
* to abort).
*
* @see CL_ENSURE_COMPONENT_KEEP_SILENT
* @see CL_ENSURE_COMPONENT_EXITS
* @see CL_ENSURE_COMPONENT_ALLOW_CREATION
*
* @param attribute The Attribute object to work with.
* @param cid The identifier of the Component to "ensure".
* @param try_creation Boolean. True = attempt to create a
* component that does not exist. False = don't.
* This behaviour only applies when
* CL_ENSURE_COMPONENT_ALLOW_CREATION is defined;
* otherwise component creation will never be attempted.
* @return A pointer to the specified component (or NULL
* if the component cannot be "ensured").
*/
Component *
ensure_component(Attribute *attribute, ComponentID cid, int try_creation)
{
Component *comp = NULL;
if (!(comp = attribute->any.components[cid])) {
/* component is undeclared */
Rprintf("attributes:ensure_component(): Warning:\n Undeclared component: %s\n", cid_name(cid));
#ifdef CL_ENSURE_COMPONENT_EXITS
exit(1);
#endif
return NULL;
}
else {
/* component IS declared, so let's see if we can ensure it. */
switch (work_out_component_state(comp)) {
case ComponentLoaded:
/* already here, so do nothing */
break;
case ComponentUnloaded:
/* try to load the component */
load_component(attribute, cid);
if (work_out_component_state(comp) != ComponentLoaded) {
#ifndef CL_ENSURE_COMPONENT_KEEP_SILENT
Rprintf("attributes:ensure_component(): Warning:\n Can't load %s component of %s\n", cid_name(cid), attribute->any.name);
#endif
#ifdef CL_ENSURE_COMPONENT_EXITS
exit(1);
#endif
return NULL;
}
break;
case ComponentDefined:
/* doesn't exist; try to create if the #defines are set up to allow that and if the caller wants */
if (try_creation) {
#ifdef CL_ENSURE_COMPONENT_ALLOW_CREATION
/* try to create the component */
create_component(attribute, cid);
if (work_out_component_state(comp) != ComponentLoaded) {
# ifndef CL_ENSURE_COMPONENT_KEEP_SILENT
Rprintf("attributes:ensure_component(): Warning:\n Can't load or create %s component of %s\n", cid_name(cid), attribute->any.name);
# endif
# ifdef CL_ENSURE_COMPONENT_EXITS
exit(1);
# endif
return NULL;
}
#else
/* this is the only alert-message NOT subject to the KEEP_SILENT definition */
Rprintf("Sorry, but this program is not set up to allow the creation of corpus components.\n"
"Please refer to the manuals or use the ''cwb-makeall'' tool.\n");
# ifdef CL_ENSURE_COMPONENT_EXITS
exit(1);
# endif
return NULL;
#endif