Bsoft  2.1.0
Bernard's software package
rwstar.cpp
Go to the documentation of this file.
1 
9 #include "rwstar.h"
10 #include "linked_list.h"
11 #include "file_util.h"
12 #include "utilities.h"
13 #include <iostream>
14 #include <stdarg.h>
15 
16 using namespace std;
17 
18 // Declaration of global variables
19 extern int verbose; // Level of output to the screen
20 
21 // Internal function prototypes
22 
23 char* clean_line(char* string)
24 {
25  unsigned int i, j, start(0), last = strlen(string);
26 
27  if ( last < 1 ) return string;
28 
29  while ( isspace(string[start]) && start < strlen(string) ) start++; // Find first non-whitespace
30 
31  char* aptr = string;
32 
33  j = 0;
34  for ( i=start; i<strlen(string); i++ ) {
35  if ( string[i] != '\n' && string[i] != '\r' ) { // Ignore newlines
36  if ( string[i] < '\r' ) aptr[j] = ' '; // All control characters become spaces
37  else aptr[j] = string[i];
38  if ( aptr[j] != ' ' ) last = j;
39  j++;
40  }
41  }
42 
43  last++;
44  aptr[last] = 0;
45 
46  return aptr;
47 }
48 
49 string clean_line(string s)
50 {
51  size_t i, j(0), start(0), last(s.length());
52 
53  if ( last < 1 ) return s;
54 
55 // while ( isspace(s[start]) && start < s.length() ) start++; // Find first non-whitespace
56  s.erase(0, s.find_first_not_of(" \t"));
57 
58  for ( i=start; i<s.length(); i++ ) {
59  if ( s[i] != '\n' && s[i] != '\r' ) { // Ignore newlines
60  if ( s[i] < '\r' ) s[j] = ' '; // All control characters become spaces
61  else s[j] = s[i];
62  if ( s[j] != ' ' ) last = j;
63  j++;
64  }
65  }
66 
67  last++;
68  s[last] = 0;
69 
70  return s;
71 }
72 
74 {
75  Bstar_block* block;
76  Bstar_item* item;
77 
78  if ( verbose & VERB_FULL )
79  cout << "Removing square brackets from tags" << endl;
80 
81  for ( block = star->block; block; block = block->next ) {
82  for ( item = block->item; item; item = item->next ) {
83  item->tag = item->tag.replace('[', '_');
84  item->tag = item->tag.remove(']');
85  }
86  }
87 
88  return 0;
89 }
90 
91 bool tag_compare(Bstring item_tag, Bstring tag)
92 {
93  if ( item_tag == tag ) return true;
94 
95  tag.replace('.', '_');
96 
97  if ( item_tag == tag ) return true;
98 
99  return false;
100 }
101 
112 {
113  if ( verbose & VERB_DEBUG_STAR )
114  cout << "DEBUG init_star: Initializing" << endl;
115 
116  // The main database structure and pointers to the data blocks
117  Bstar* star = new Bstar;
118  memset(star, 0, sizeof(Bstar));
119  star->line_length = LINELENGTH; // Length of line for output
120 
121  return star;
122 }
123 
132 int kill_star(Bstar* star)
133 {
134  if ( star == NULL ) return 0;
135 
136  if ( verbose & VERB_DEBUG_STAR )
137  cout << "DEBUG kill_star: Deleting STAR data base" << endl;
138 
139  Bstar_block* block, *block2;
140 
141  for ( block=star->block; block; ) {
142  block2 = block->next;
143  kill_block(block);
144  block = block2;
145  }
146 
147  star->comment = 0;
148 
149  delete star;
150 
151  return 0;
152 }
153 
164 {
165  if ( block == NULL ) return 0;
166 
167  Bstar_item* item, *item2;
168 
169  if ( verbose & VERB_DEBUG_STAR )
170  cout << "DEBUG kill_block: Deleting block " << block->tag << endl;
171 
172  for ( item = block->item; item; ) {
173  item2 = item->next;
174  kill_item(item);
175  item = item2;
176  }
177 
178  block->tag = 0;
179 
180  block->comment = 0;
181 
182  block->filename = 0;
183 
184  delete[] (char*) block;
185 
186  return 0;
187 }
188 
198 {
199  if ( item == NULL ) return 0;
200 
201  if ( verbose & VERB_DEBUG_STAR )
202  cout << "DEBUG kill_item: Deleting item " << item->tag << endl;
203 
204  string_kill(item->data);
205 
206  item->tag = 0;
207  item->comment = 0;
208 
209  delete[] (char*) item;
210 
211  return 0;
212 }
213 
224 int read_star(const char* filename, Bstar* star)
225 {
226  int err(0);
227 
228  // Get the list of filenames
229  Bstring files(filename);
230  Bstring* file_list = files.split(",");
231 
232  err = read_star(file_list, star);
233 
234  string_kill(file_list);
235 
236  return err;
237 }
238 
239 int read_star(Bstring& filename, Bstar* star)
240 {
241  Bstring onefile(filename);
242  return read_star(&onefile, star);
243 }
244 
245 int read_star(Bstring* file_list, Bstar* star)
246 {
247  int err(0);
248 
249  if ( !file_list ) {
250  error_show("No file names found!", __FILE__, __LINE__);
251  return -1;
252  }
253 
254  // Get the list of filenames
255  Bstring* thisfile = NULL;
256 
257  if ( verbose & VERB_DEBUG_STAR ) {
258  cout << "DEBUG read_star: STAR filenames: " << endl;
259  for ( thisfile = file_list; thisfile; thisfile = thisfile->next )
260  cout << " " << *thisfile;
261  cout << endl;
262  }
263 
264  // Parse every file
265  ifstream* fstar = new ifstream;
266  char* aline = new char[MAXLINELEN+4];
267  string sline;
268  memset(aline, 0, MAXLINELEN+4);
269  char *aptr;
270  Bstring comment;
271  int comment_done(0), comment_add(1);
272  Bstar_block* block;
273 
274  for ( thisfile = file_list; thisfile; thisfile = thisfile->next ) {
275  if ( detect_and_fix_carriage_return(thisfile->c_str()) ) {
276  cerr << "Warning: Carriage returns not fixed in " << *thisfile << endl;
277 // error_show(thisfile->c_str(), __FILE__, __LINE__);
278 // return -1;
279  }
280 
281  if ( verbose & VERB_DEBUG )
282  cout << "Opening " << *thisfile << endl;
283  fstar->open(thisfile->c_str());
284  if ( fstar->fail() ) {
285  error_show(thisfile->c_str(), __FILE__, __LINE__);
286  return -1;
287  }
288 
289  if ( verbose & VERB_PROCESS )
290  cout << "# Reading STAR file: " << *thisfile << endl;
291 
292  comment_done = 0;
293  while ( !comment_done ) {
294  getline(*fstar, sline);
295 // cout << "-" << sline << "-" << endl;
296 // aptr = (char *) sline.c_str();
297  if ( fstar->eof() ) comment_done = 1;
298  else {
299 // while ( isspace(aptr[0]) ) aptr++;
300 // if ( strncmp(aptr, "data_", 5) == 0 ) comment_done = 1;
301 // else if ( comment_add ) comment = comment + aptr + "\n";
302  sline.erase(0, sline.find_first_not_of(" \t"));
303  if ( sline.compare(0, 5, "data_", 5) == 0 ) comment_done = 1;
304  else if ( comment_add ) comment = comment + sline + "\n";
305  }
306  }
307 
308  comment_add = 0;
309 
310  strncpy(aline, sline.c_str(), MAXLINELEN);
311  aptr = clean_line(aline); // Points to either a data block or nothing
312 
313  if ( strncmp(aptr, "data_", 5) == 0 ) {
314  if ( !star->block ) star->block = read_block(fstar, aptr, *thisfile);
315  else {
316  for ( block = star->block; block->next; block = block->next ) ;
317  block->next = read_block(fstar, aptr, *thisfile);
318  }
319  } else
320  cerr << "Error: No data blocks in file " << *thisfile << endl;
321 
322  fstar->close();
323  }
324 
325  delete fstar;
326  delete[] aline;
327 
328  for ( block = star->block; block->next; block = block->next )
329  block->next->number = block->number + 1;
330 
331  if ( comment.length() ) star->comment = comment;
332  else star->comment = "\n";
333 
334  if ( verbose & VERB_DEBUG_STAR )
335  cout << "DEBUG read_star: Transferring comments:" << star->comment << endl << endl;
336 
337  no_square_brackets(star);
338 
339  if ( verbose & VERB_DEBUG_STAR )
340  cout << "DEBUG read_star: Done!" << endl;
341 
342  return err;
343 }
344 
356 Bstar_block* read_block(ifstream* fstar, char* aptr, Bstring& filename)
357 {
358  Bstar_block* block = NULL;
359  add_item(reinterpret_cast<char **>(&block), sizeof(Bstar_block));
360 
361  block->filename = filename;
362 
363  Bstar_item* item = NULL;
364  Bstar_item* new_item = NULL;
365 
366  int loop_counter(0), loop = -1;
367 
368  // Copy the block tag
369  aptr += 5;
370  if ( strlen(aptr) > 0 )
371  block->tag = aptr;
372  else
373  block->tag = "";
374 
375  if ( verbose & VERB_DEBUG_STAR )
376  cout << "# Reading block: " << block->tag << endl;
377 
378  char* aline = new char[MAXLINELEN+4];
379  memset(aline, 0, MAXLINELEN+4);
380 
381  while ( fstar->getline(aline, MAXLINELEN) ) {
382  aptr = clean_line(aline);
383  if ( strncmp(aptr, "data_", 5) == 0 ) {
384  block->next = read_block(fstar, aptr, filename);
385  } else {
386  if ( aptr[0] == '_' || aptr[0] == '#' || aptr[0] == ';' ) { // Single item read
387  new_item = read_single_item(fstar, aptr);
388  loop = -1;
389  } else if ( strncmp(aptr, "loop_", 5) == 0 ) { // A loop block
390  new_item = read_loop_items(fstar);
391  loop = ++loop_counter;
392  }
393  if ( new_item ) {
394  if ( item ) item->next = new_item;
395  else {
396  block->item = item = new_item;
397  item->loop = loop;
398  }
399  while ( item->next ) {
400  item = item->next;
401  item->loop = loop;
402  }
403  new_item = NULL;
404  }
405  }
406  }
407 
408  delete[] aline;
409 
410  return block;
411 }
412 
427 Bstar_item* read_single_item(ifstream* fstar, char* aline)
428 {
429  Bstar_item* item = NULL;
430  add_item(reinterpret_cast<char **>(&item), sizeof(Bstar_item));
431  item->type = StringItem;
432 
433  int k, digit(0), alpha(0);
434  Bstring svalue;
435  char* aptr = aline;
436  char temp[100];
437 
438  if ( aptr[0] == '#' && strlen(aptr) < 2 ) return 0;
439  if ( aptr[0] == '_' && strlen(aptr) < 4 ) return 0;
440 
441  if ( verbose & VERB_DEBUG_STAR )
442  cout << "DEBUG single item: Starting" << endl;
443 
444  item->loop = -1;
445 
446  if ( aptr[0] == '#' || aptr[0] == ';' ) { // A comment
447  item->tag = "comment";
448  } else {
449  sscanf(aptr, "%s%n", temp, &k); // Read tag
450  item->tag = &temp[1]; // Omit the initial underscore
451  aptr = clean_line(aline + k); // Start of data item
452  if ( strlen(aptr) < 1 ) { // Next line
453  fstar->getline(aline, MAXLINELEN);
454  aptr = clean_line(aline);
455  }
456  }
457 
458  if ( aptr[0] == '#' ) { // Comment
459  aptr++;
460  svalue = aptr;
461  } else if ( aptr[0] == '\'' ) { // String delimiters
462  aptr++;
463  k = strcspn(aptr, "\'");
464  aptr[k] = 0;
465  svalue = aptr;
466  } else if ( aptr[0] == '\"' ) {
467  while ( aptr[0] == '\"' ) aptr++; // Take care of multiple quotes
468  k = strcspn(aptr, "\"");
469  aptr[k] = 0;
470  svalue = aptr;
471  } else if ( aptr[0] == ';' ) {
472  aptr++;
473  k = strcspn(aptr, "\n");
474  aptr[k] = 0;
475  if ( k > 0 ) svalue = aptr;
476  while ( fstar->getline(aline, MAXLINELEN) && aline[0] != ';' ) {
477  k = strcspn(aline, "\n");
478  aline[k] = 0;
479  if ( k > 0 ) svalue += aline;
480  }
481  } else {
482  k = strcspn(aptr, "#");
483  aptr[k] = 0;
484  svalue = aptr;
485  }
486 
487  if ( svalue.length() < 1 ) {
488  cerr << "Warning: Tag " << item->tag << " value not read!" << endl;
489  kill_item(item);
490  item = NULL;
491  }
492 
493  svalue = svalue.remove('\"');
494 
495  string_add(&item->data, svalue);
496  item->maxlen = item->data->length();
497  for ( k=0; k<svalue.length(); k++ ) { // Check if string or number
498  if ( isalpha(svalue[k]) ) alpha++;
499  else if ( isdigit(svalue[k]) ) digit++;
500  }
501  if ( alpha < 1 && digit > 0 ) item->type = NumberItem;
502 
503  if ( verbose & VERB_DEBUG_STAR ) {
504  cout << "DEBUG single item: Tag: " << item->tag << ", maxlen=" << item->maxlen << endl;
505  cout << "DEBUG single item: Data: " << *(item->data) << endl;
506  }
507 
508  return item;
509 }
510 
528 Bstar_item* read_loop_items(ifstream* fstar)
529 {
530  Bstar_item* item_list = NULL;
531  Bstar_item* item = NULL;
532 
533  char* aline = new char[MAXLINELEN+4];
534  memset(aline, 0, MAXLINELEN+4);
535  char* aval = new char[MAXLINELEN+4];
536  memset(aval, 0, MAXLINELEN+4);
537  char* aptr = aline;
538  int i, j, k, loopdone(0), number(0);
539  int digit, alpha;
540  int nitems(0);
541 
542  if ( verbose & VERB_DEBUG_STAR )
543  cout << "DEBUG loop: Starting" << endl;
544 
545  while ( fstar->getline(aline, MAXLINELEN) &&
546  ( aptr = clean_line(aline) ) &&
547  aptr[0] == '_' ) { // Find all the tags
548  item = reinterpret_cast<Bstar_item *> (add_item(reinterpret_cast<char **>(&item_list), sizeof(Bstar_item)));
549  sscanf(aptr, "%s%n", aval, &k); // Read tag
550  item->tag = &aval[1]; // Omit the initial underscore
551  item->type = NumberItem; // Default is NumberItem --> check later if it should be StringItem
552  nitems++;
553  }
554 
555  if ( verbose & VERB_DEBUG_STAR )
556  cout << "DEBUG loop: number of tags=" << nitems << endl;
557 
558  if ( nitems < 1 ) {
559  cerr << "Error: No tags in the loop!" << endl;
560  return NULL;
561  }
562 
563  // Strings to hold the values temporarily
564  Bstring* sval = new Bstring[nitems];
565  Bstring** data = new Bstring*[nitems];
566  Bstring** datalist = new Bstring*[nitems];
567  memset(sval, 0, nitems*sizeof(Bstring));
568  memset(data, 0, nitems*sizeof(Bstring*));
569  memset(datalist, 0, nitems*sizeof(Bstring*));
570 
571  loopdone = number = 0;
572  if ( strlen(aptr) < 1 || aptr[0] == '\n' || aptr[0] == '#' ) // Check for blank line
573  loopdone = 1;
574  while ( !loopdone ) { // Get the number of rows in the loop
575  if ( verbose & VERB_DEBUG_STAR )
576  cout << "DEBUG loop: starting " << number << " " << aline << endl;
577 // I cannot take out commas - it's not generally only used as delimiter
578 // for ( j=0; j<strlen(aptr); j++ ) if ( aptr[j] == ',' ) aptr[j] = ' ';
579  for ( i=0; i<nitems && !loopdone; i++ ) {
580  aptr = clean_line(aptr);
581  if ( strlen(aptr) < 1 ) { // Loop columns extend over multiple lines
582  if ( fstar->getline(aline, MAXLINELEN) ) // Check for end-of-file
583  aptr = clean_line(aline);
584  else loopdone = 1;
585  }
586  if ( aline[0] == ';' ) { // Multi-line string items
587  aptr = clean_line(aline + 1);
588  j = strcspn(aptr, "\n");
589  aptr[j] = 0;
590  sval[i] = aptr;
591  while ( fstar->getline(aline, MAXLINELEN) && aline[0] != ';' ) {
592  j = strcspn(aline, "\n");
593  aline[j] = 0;
594  sval[i] = sval[i] + aline;
595  }
596  } else {
597  if ( sscanf(aptr, "%s", aval) < 1 ) { // Short items in loop
598  if ( fstar->getline(aline, MAXLINELEN) )
599  aptr = clean_line(aline);
600  if ( sscanf(aptr, "%s", aval) < 1 ) // Check for end-of-loop
601  loopdone = 1;
602  else sval[i] = aval;
603  } else sval[i] = aval;
604  }
605  if ( ( aptr = strchr(aptr, ' ') ) == NULL )
606  aptr = aline + strlen(aline);
607  }
608  if ( verbose & VERB_DEBUG_STAR ) {
609  cout << "DEBUG loop: finishing " << number << endl;
610  for ( i=0; i<nitems; i++ ) cout << tab << sval[i] << endl;
611  cout << endl;
612  }
613  if ( i == nitems && !loopdone ) {
614  number++;
615  for ( j=0, item=item_list; j<nitems; j++, item=item->next ) {
616  sval[j] = sval[j].remove('\"');
617  if ( item->maxlen < (int)sval[j].length() ) item->maxlen = sval[j].length();
618  data[j] = string_add(&data[j], sval[j]);
619  if ( !datalist[j] ) datalist[j] = data[j];
620  digit = alpha = 0;
621  for ( k=0; k<(int)sval[j].length(); k++ ) { // Check if it is a string
622  if ( isalpha(sval[j][k]) ) alpha++;
623  else if ( isdigit(sval[j][k]) ) digit++;
624  }
625  if ( alpha > 0 ) item->type = StringItem;
626  }
627  if ( fstar->getline(aline, MAXLINELEN) ) // Check for end-of-file
628  aptr = clean_line(aline);
629  else loopdone = 1;
630  } else loopdone = 1; // Check for end-of-loop
631  if ( strlen(aptr) < 1 || aptr[0] == '\n' || aptr[0] == '#' ) // Check for blank line
632  loopdone = 1;
633  }
634 
635  for ( i=0, item=item_list; i<nitems && item; i++, item=item->next ) // Assign the items
636  item->data = datalist[i];
637 
638  if ( verbose & VERB_DEBUG_STAR )
639  cout << "DEBUG loop: Columns=" << nitems << " Rows=" << number << endl;
640 
641  delete[] aline;
642  delete[] aval;
643  delete[] sval;
644  delete[] data;
645  delete[] datalist;
646 
647  if ( verbose & VERB_DEBUG_STAR )
648  cout << "DEBUG loop: memory freed" << endl;
649 
650  return item_list;
651 }
652 
666 int write_star(const char* filename, Bstar* star)
667 {
668  Bstring thefile(filename);
669  return write_star(thefile, star);
670 }
671 
672 int write_star(Bstring& filename, Bstar* star)
673 {
674  int err(0);
675 
676  if ( star->split < 9 )
677  if ( filename.length() < 1 ) return -1;
678 
679  ofstream* fstar = new ofstream;
680  Bstring default_comment = "# Written by Bsoft";
681 
682  if ( star->comment.length() < 1 ) star->comment = default_comment;
683 
684  if ( star->split == 0 ) {
685  fstar->open(filename.c_str());
686  if ( fstar->fail() ) {
687  cerr << "Error: Not able to write " << filename << endl;
688  return -1;
689  }
690 
691  if ( verbose & VERB_PROCESS )
692  cout << "# Writing file: " << filename << endl;
693 
694  *fstar << star->comment << endl;
695  } else {
696  if ( verbose & VERB_PROCESS )
697  cout << "Splitting STAR file (" << star->split << "):" << endl;
698  }
699 
700  int i;
701  Bstar_block* block;
702  Bstring blockname;
703  char format[32];
704 
705  snprintf(format, 32, "_%%0%dd.", star->split);
706 
707  for ( i=0, block = star->block; block; block = block->next, i++ ) if ( block->item ) {
708  if ( star->split ) {
709  if ( star->split == 9 ) {
710  if ( block->tag.length() > 0 ) {
711  blockname = block->tag;
712  blockname = blockname.replace(' ', '_');
713  blockname += ".star";
714  } else {
715  blockname = Bstring(i+1, "%09d.star");
716  }
717  } else {
718  blockname = filename.pre_rev('.') + Bstring(i+1, format) + filename.post_rev('.');
719  }
720  fstar->open(blockname.c_str());
721  if ( fstar->fail() ) {
722  cerr << "Error: Not able to write " << blockname << endl;
723  return -1;
724  }
725  if ( verbose & VERB_PROCESS )
726  cout << "# Writing file: " << blockname << endl;
727  *fstar << star->comment << endl;
728  }
729  err = write_block(fstar, block, star->line_length);
730  if ( star->split ) fstar->close();
731  }
732 
733  if ( star->split == 0 ) fstar->close();
734 
735  delete fstar;
736 
737  return err;
738 }
739 
750 int write_block(ofstream* fstar, Bstar_block* block, int linelength)
751 {
752  int err(0);
753  long j, m, size, nitems, pos;
754  Bstring tag, tstr;
755  Bstar_item* item, *item2;
756  Bstring** dptr;
757 
758  if ( block->tag.empty() ) block->tag = "";
759  *fstar << endl << "data_" << block->tag << endl << endl;
760 
761 // cout << block->item->tag << endl;
762 
763  for ( item = block->item; item; item = item->next ) {
764  if ( item->tag.contains("comment") ) {
765  tag = "\n# ";
766  } else if ( item->tag.length() > 0 ) {
767  if ( item->tag[0] == '_' ) tag = item->tag;
768  else tag = "_" + item->tag;
769  }
770  if ( item->loop < 0 ) { // Write single value items
771  if ( item->data->length() > linelength - item->tag.length() - 8 ) { // Multi-line items
772  if ( tag.length() > 0 )
773  *fstar << tag << endl;
774  *fstar << ";" << endl;
775  for ( j=0; j<item->data->length(); j += linelength ) {
776  size = item->data->length() - j;
777  if ( size > linelength ) size = linelength;
778  if ( size ) {
779  tstr = item->data->substr(j, size);
780  *fstar << tstr << endl;
781  }
782  }
783  *fstar << ";" << endl;
784  } else { // Single items fitting on one or two lines
785  if ( tag.length() > 0 ) {
786  if ( item->type == StringItem ) {
787  if ( item->data->length() > linelength - tag.length() - 8 ) {
788  *fstar << tag << endl << "\"" << item->data << "\"" << endl;
789  } else if ( strchr(item->data->c_str(), ' ') ) {
790  *fstar << setw(40) << tag << " \"" << *(item->data) << "\"" << endl;
791  } else
792  *fstar << setw(40) << tag << " " << *(item->data) << endl;
793  } else {
794  *fstar << setw(40) << tag << " " << *(item->data) << endl;
795  }
796  } else {
797  *fstar << *(item->data) << endl; // No-tag data
798  }
799  }
800  } else if ( item->data ) { // Write a loop block
801  *fstar << endl << "loop_" << endl;
802  for ( nitems=0, item2=item; item2 && item2->loop == item->loop; item2=item2->next, nitems++ ) {
803  if ( item2->tag[0] == '_' ) tag = item2->tag;
804  else tag = "_" + item2->tag;
805  *fstar << tag << endl;
806  }
807  dptr = new Bstring*[nitems];
808  for ( j=0, item2=item; j<nitems; item2=item2->next, j++ )
809  dptr[j] = item2->data;
810  while ( dptr[0] ) {
811  pos = 0;
812  tstr = 0;
813  for ( j=0, item2=item; j<nitems; item2=item2->next, j++ ) {
814  if ( item2->maxlen > linelength ) { // Multi-line items
815  tstr += ";\n";
816 /* for ( m=0; m<item2->maxlen; m += linelength ) {
817  size = item2->maxlen - m;
818  if ( size > linelength )
819  size = linelength;
820  if ( size > dptr[j]->length() - m )
821  size = dptr[j]->length() - m;
822  if ( size ) {
823  tstr += dptr[j]->substr(m, size) + "\n";
824  }
825  }*/
826  for ( m=0; m<dptr[j]->length(); m += linelength ) {
827  size = dptr[j]->length() - m;
828  if ( size > linelength )
829  size = linelength;
830  if ( size ) {
831  tstr += dptr[j]->substr(m, size) + "\n";
832  }
833  }
834  if ( j == nitems - 1 )
835  tstr += ";";
836  else
837  tstr += ";\n";
838  pos = 0;
839  } else {
840  if ( item2->type == NumberItem ) { // Right justify
841  tstr += " " + dptr[j]->right(item2->maxlen);
842  } else { // Write strings as is
843 // tstr += *dptr[j] + " ";
844  if ( dptr[j]->contains("'") ) *dptr[j] = "\"" + *dptr[j] + "\"";
845  if ( item2->maxlen < dptr[j]->length() ) item2->maxlen = dptr[j]->length();
846  tstr += " " + dptr[j]->left(item2->maxlen);
847 // pos += dptr[j]->length() + 1;
848  }
849  pos += item2->maxlen + 1;
850  }
851  if ( pos + item2->maxlen > linelength && j < nitems - 1 ) {
852  tstr += "\n";
853  pos = 0;
854  }
855  dptr[j] = dptr[j]->next;
856  }
857  *fstar << tstr << endl;
858  }
859  for ( j=1; item->next && j<nitems; j++ ) item = item->next;
860  delete[] dptr;
861  *fstar << endl;
862  }
863  }
864  *fstar << endl;
865 
866  return err;
867 }
868 
880 int star_update_comment(Bstar* star, int n, char** strings)
881 {
882  time_t ti = time(NULL);
883 
884  int i;
885  Bstring new_comment("# ");
886 
887  for ( i=0; i<n; i++ )
888  new_comment = new_comment + strings[i] + " ";
889 
890  new_comment = new_comment + "\n# " + asctime(localtime(&ti)) + "\n";
891 
892  if ( verbose & VERB_DEBUG )
893  cout << endl << new_comment << endl;
894 
895  star->comment += new_comment;
896 
897  return star->comment.length();
898 }
899 
907 long star_list_comments(Bstar* star, long len)
908 {
909  if ( len < 1 ) len = 10000000000;
910 
911  long n(0);
912  Bstring* s;
913  Bstring s1;
914 
915  Bstring* strarr = star->comment.split("\n");
916 
917  for ( s = strarr; s; s = s->next ) {
918  if ( (*s)[0] == '#' ) {
919  s1 = s->substr(2,len);
920  cout << s1 << endl;
921  n++;
922  }
923  }
924 
925  string_kill(strarr);
926 
927  return n;
928 }
929 
939 {
940  Bstar_block* block;
941  Bstar_item* item;
942  Bstring* data;
943 
944  for ( block = star->block; block; block = block->next ) {
945  for ( item = block->item; item; item = item->next ) {
946  for ( data = item->data; data; data = data->next )
947  if ( item->maxlen < data->length() )
948  item->maxlen = data->length();
949  }
950  }
951 
952  return 0;
953 }
954 
966 int item_change_tag(Bstar* star, const char* tag, const char* newtag)
967 {
968  Bstar_block* block;
969  Bstar_item* item;
970 
971  if ( verbose & VERB_FULL )
972  cout << "Changing all tags \"" << tag << "\" to \"" << newtag << "\"" << endl;
973 
974  for ( block = star->block; block; block = block->next ) {
975  for ( item = block->item; item; item = item->next ) {
976  if ( item->tag == tag ) {
977  item_delete_from_block(block, newtag); // First make sure any other newtag is deleted
978  item->tag = newtag;
979  }
980  }
981  }
982 
983  return 0;
984 }
985 
991 int show_tags(Bstar* star)
992 {
993  int ntags(0);
994  Bstring tag;
995  Bstar_block* block;
996  Bstar_item* item;
997 
998  for ( block = star->block; block; block = block->next ) {
999  cout << endl << "data_" << block->tag << endl;
1000  cout << endl << "loop_" << endl;
1001  cout << "_star_tag" << endl;
1002  cout << "_star_type" << endl;
1003  for ( item = block->item; item; item = item->next ) {
1004  tag = "\"_" + item->tag + "\"";
1005  cout << setw(40) << item->tag;
1006  if ( item->type == StringItem )
1007  cout << "String" << endl;
1008  else
1009  cout << "Number" << endl;
1010  ntags++;
1011  }
1012  cout << endl;
1013  }
1014  cout << endl;
1015 
1016  return ntags;
1017 }
1018 
1029 int item_index(Bstar_block* block, const char* tag)
1030 {
1031  int i;
1032  Bstar_item* item = NULL;
1033 
1034  for ( i=0, item = block->item; item && !tag_compare(item->tag, tag); item = item->next ) i++;
1035 
1036  if ( !item ) i = -1;
1037 
1038  if ( verbose & VERB_DEBUG_STAR )
1039  cout << "DEBUG item_index: " << tag << " " << i << endl;
1040 
1041  return i;
1042 }
1043 
1050 Bstar_block* block_find_with_tag(Bstar* star, const char* tag)
1051 {
1052  int found(0);
1053  Bstar_block* block = NULL;
1054  Bstar_item* item = NULL;
1055 
1056  for ( block = star->block; block; block = block->next ) {
1057  for ( item = block->item; item; item = item->next ) {
1058  if ( item->tag == tag ) {
1059  found = 1;
1060  break;
1061  }
1062  }
1063  if ( found ) break;
1064  }
1065 
1066  if ( verbose & VERB_DEBUG_STAR )
1067  cout << "DEBUG item_get_first_block: " << tag << " " << &block << endl;
1068 
1069  return block;
1070 }
1071 
1072 
1083 Bstar_item* item_find(Bstar_block* block, const char* tag)
1084 {
1085  Bstar_item* item = NULL;
1086 
1087  for ( item = block->item; item && !tag_compare(item->tag, tag); item = item->next ) ;
1088 
1089  if ( verbose & VERB_DEBUG_STAR )
1090  cout << "DEBUG item_find: " << tag << " " << &item << endl;
1091 
1092  return item;
1093 }
1094 
1105 Bstar_item* item_find_or_make(Bstar_block* block, const char* tag)
1106 {
1107  Bstar_item* item = item_find(block, tag);
1108 
1109 // if ( item ) {
1110 // string_kill(item->data);
1111 // item->data = NULL;
1112 // } else {
1113  if ( !item ) {
1114  item = reinterpret_cast<Bstar_item *> (add_item(reinterpret_cast<char **>(&block->item), sizeof(Bstar_item)));
1115  item->tag = tag;
1116  }
1117 
1118  if ( verbose & VERB_DEBUG_STAR )
1119  cout << "DEBUG item_find_or_make: " << tag << " " << &item << endl;
1120 
1121  return item;
1122 }
1123 
1134 long item_get_number(Bstar* star, const char* tag)
1135 {
1136  long number(0);
1137  Bstar_block* block;
1138  Bstar_item* item;
1139 
1140  for ( block = star->block; block; block = block->next ) {
1141  for ( item = block->item; item; item = item->next ) {
1142  if ( item->tag == tag )
1143  number += count_list((char *)item->data);
1144  }
1145  }
1146 
1147  if ( verbose & VERB_DEBUG_STAR )
1148  cout << "DEBUG item_get_number: " << tag << " " << &number << endl;
1149 
1150  return number;
1151 }
1152 
1163 long item_get_number_for_block(Bstar_block* block, const char* tag)
1164 {
1165  long number(0);
1166  Bstar_item* item;
1167 
1168  for ( item = block->item; item; item = item->next ) {
1169  if ( item->tag == tag )
1170  number += count_list((char *)item->data);
1171  }
1172 
1173  if ( verbose & VERB_DEBUG_STAR )
1174  cout << "DEBUG item_get_number_for_block: " << tag << " " << &number << endl;
1175 
1176  return number;
1177 }
1178 
1189 char* item_get_string(Bstar_block* block, const char* tag)
1190 {
1191  int i;
1192  Bstar_item* item;
1193  char* string = NULL;
1194 
1195  for ( item = block->item; item && !string; item = item->next ) {
1196  if ( item->tag == tag ) {
1197  string = new char[item->data->length()+1];
1198  for ( i=0; i<item->data->length(); i++ ) string[i] = (*item->data)[i];
1199  string[item->data->length()] = 0;
1200  }
1201  }
1202 
1203  if ( verbose & VERB_DEBUG_STAR )
1204  cout << "DEBUG item_get_string: " << tag << " " << string << endl;
1205 
1206  return string;
1207 }
1208 
1220 int item_copy_string(char* string, Bstar_block* block, const char* tag)
1221 {
1222  Bstar_item* item;
1223 
1224  for ( item = block->item; item; item = item->next ) {
1225  if ( item->tag == tag )
1226  strcpy(string, item->data->c_str());
1227  }
1228 
1229  if ( verbose & VERB_DEBUG_STAR )
1230  cout << "DEBUG item_copy_string: " << tag << " " << string << endl;
1231 
1232  return 0;
1233 }
1234 
1245 int item_get_integer(Bstar_block* block, const char* tag)
1246 {
1247  Bstar_item* item;
1248  int value(0);
1249 
1250  for ( item = block->item; item; item = item->next ) {
1251  if ( item->tag == tag ) {
1252  if ( *(item->data) == "?" || *(item->data) == "." ) value = 0;
1253  else value = item->data->integer();
1254  }
1255  }
1256 
1257  if ( verbose & VERB_DEBUG_STAR )
1258  cout << "DEBUG item_get_integer: " << tag << " " << value << endl;
1259 
1260  return value;
1261 }
1262 
1273 float item_get_float(Bstar_block* block, const char* tag)
1274 {
1275  Bstar_item* item;
1276  float value(0);
1277 
1278  for ( item = block->item; item; item = item->next ) {
1279  if ( item->tag == tag ) {
1280  if ( *(item->data) == "?" || *(item->data) == "." )
1281  value = strtod ("NAN()",NULL);
1282  else value = item->data->real();
1283  }
1284  }
1285 
1286  if ( verbose & VERB_DEBUG_STAR )
1287  cout << "DEBUG item_get_float: " << tag << " " << value << endl;
1288 
1289  return value;
1290 }
1291 
1300 int item_put_string(Bstar_block* block, const char* tag, char* string)
1301 {
1302  Bstring this_string(string);
1303  return item_put_string(block, tag, this_string);
1304 }
1305 
1306 int item_put_string(Bstar_block* block, const char* tag, Bstring& string)
1307 {
1308  if ( !block ) return -1;
1309 
1310  long len = string.length();
1311 
1312  if ( len < 1 ) return 0;
1313 
1314  Bstar_item* item = item_find_or_make(block, tag);
1315 
1316  item->type = StringItem;
1317  item->loop = -1;
1318 
1319  string_add(&item->data, string);
1320 
1321  if ( item->maxlen < len ) item->maxlen = len;
1322 
1323  if ( verbose & VERB_DEBUG_STAR )
1324  cout << "DEBUG item_put_string: " << tag << " " << *(item->data) << endl;
1325 
1326  return 0;
1327 }
1328 
1337 int item_put_string_list(Bstar_block* block, const char* tag, Bstring* list)
1338 {
1339  if ( !block ) return -1;
1340 
1341  Bstar_item* item = item_find_or_make(block, tag);
1342  Bstring* data = NULL;
1343  Bstring* string = NULL;
1344 
1345  item->type = StringItem;
1346 
1347  for ( string = list; string; string = string->next ) {
1348  data = string_add(&data, *string);
1349  if ( !item->data ) item->data = data;
1350  if ( item->maxlen < string->length() ) item->maxlen = string->length();
1351  }
1352 
1353  if ( verbose & VERB_DEBUG_STAR )
1354  cout << "DEBUG item_put_string_list: " << tag << " " << *(item->data) << endl;
1355 
1356  return 0;
1357 }
1358 
1368 int item_put_integer(Bstar_block* block, const char* tag, int value, const char* format)
1369 {
1370  if ( !block ) return -1;
1371 
1372  char string[32] = "";
1373  snprintf(string, 32, format, value);
1374 
1375  long len = strlen(string);
1376 
1377  Bstar_item* item = item_find_or_make(block, tag);
1378 
1379  item->type = NumberItem;
1380  item->loop = -1;
1381 
1382  string_add(&item->data, string);
1383 
1384  if ( item->maxlen < len ) item->maxlen = len;
1385 
1386  if ( verbose & VERB_DEBUG_STAR )
1387  cout << "DEBUG item_put_integer: " << tag << " " << *(item->data) << endl;
1388 
1389  return 0;
1390 }
1391 
1401 int item_put_float(Bstar_block* block, const char* tag, float value, const char* format)
1402 {
1403  if ( !block ) return -1;
1404 
1405  char string[32] = "";
1406  snprintf(string, 32, format, value);
1407  if ( isnan(value) ) snprintf(string, 32, ".");
1408 
1409  long len = strlen(string);
1410 
1411  Bstar_item* item = item_find_or_make(block, tag);
1412 
1413  item->type = NumberItem;
1414  item->loop = -1;
1415 
1416  string_add(&item->data, string);
1417 
1418  if ( item->maxlen < len ) item->maxlen = len;
1419 
1420  if ( verbose & VERB_DEBUG_STAR )
1421  cout << "DEBUG item_put_float: " << tag << " " << *(item->data) << endl;
1422 
1423  return 0;
1424 }
1425 
1436 int item_put_list(Bstar_block* block, const char* tag, char* list,
1437  size_t offset, const char* format)
1438 {
1439  if ( !block ) return -1;
1440 
1441  Bstar_item* item = item_find_or_make(block, tag);
1442  Bstring* data = item->data;
1443  if ( data ) while ( data->next ) data = data->next;
1444 
1445  item->type = NumberItem;
1446  if ( strstr(format, "s") ) item->type = StringItem;
1447 
1448  char string[32] = "";
1449  char** it;
1450 
1451  for ( it = (char **) list; it; it = (char **) *it ) {
1452  if ( strstr(format, "s") )
1453  snprintf(string, 32, format, (char *)it + offset);
1454  else if ( strstr(format, "d") )
1455  snprintf(string, 32, format, *((int*)((char *)it + offset)));
1456  else if ( strstr(format, "lf") )
1457  snprintf(string, 32, format, *((double*)((char *)it + offset)));
1458  else
1459  snprintf(string, 32, format, *((float*)((char *)it + offset)));
1460  if ( strstr(string, "nan") ) snprintf(string, 32, ".");
1461  data = string_add(&data, string);
1462  if ( !item->data ) item->data = data;
1463  if ( item->maxlen < strlen(string) ) item->maxlen = strlen(string);
1464  }
1465 
1466  if ( verbose & VERB_DEBUG_STAR )
1467  cout << "DEBUG item_put_list: " << tag << " (" << offset << ") " << *(item->data) << endl;
1468 
1469  return 0;
1470 }
1471 
1486 int item_put_float_array(Bstar_block* block, const char* tag, int number,
1487  float* value, const char* format)
1488 {
1489  if ( !block || number < 1 || !value ) return -1;
1490 
1491  Bstar_item* item = item_find_or_make(block, tag);
1492  Bstring* data = item->data;
1493  if ( data ) while ( data->next ) data = data->next;
1494 
1495  item->type = NumberItem;
1496 
1497  char string[32] = "";
1498  int i;
1499 
1500  for ( i=0; i<number; i++ ) {
1501  snprintf(string, 32, format, value[i]);
1502  if ( strstr(string, "nan") ) snprintf(string, 32, ".");
1503  data = string_add(&data, string);
1504  if ( !item->data ) item->data = data;
1505  if ( item->maxlen < strlen(string) ) item->maxlen = strlen(string);
1506  }
1507 
1508  if ( verbose & VERB_DEBUG_STAR )
1509  cout << "DEBUG item_put_float_array: " << tag << " " << *(item->data) << endl;
1510 
1511  return 0;
1512 }
1513 
1528 int item_put_angle_list(Bstar_block* block, const char* tag, char* list,
1529  size_t offset, const char* format)
1530 {
1531  if ( !block ) return -1;
1532 
1533  Bstar_item* item = item_find_or_make(block, tag);
1534  Bstring* data = item->data;
1535  if ( data ) while ( data->next ) data = data->next;
1536 
1537  item->type = NumberItem;
1538 
1539  double angle;
1540  char string[32] = "";
1541  char** it;
1542 
1543  for ( it = (char **) list; it; it = (char **) *it ) {
1544  if ( strstr(format, "lf") )
1545  angle = *((double*)((char *)it + offset));
1546  else
1547  angle = *((float*)((char *)it + offset));
1548  snprintf(string, 32, format, angle*180.0/M_PI);
1549  if ( strstr(string, "nan") ) snprintf(string, 32, ".");
1550  data = string_add(&data, string);
1551  if ( !item->data ) item->data = data;
1552  if ( item->maxlen < strlen(string) ) item->maxlen = strlen(string);
1553  }
1554 
1555  if ( verbose & VERB_DEBUG_STAR )
1556  cout << "DEBUG item_put_float_list: " << tag << " " << *(item->data) << endl;
1557 
1558  return 0;
1559 }
1560 
1571 int item_list(Bstar* star, Bstring& tag)
1572 {
1573  Bstar_block* block;
1574  Bstar_item* item;
1575  Bstring* data;
1576 
1577  if ( verbose & VERB_LABEL )
1578  cout << "Listing all items associated with tag \"" << tag << "\"" << endl;
1579 
1580  for ( block = star->block; block; block = block->next ) {
1581  for ( item = block->item; item; item = item->next ) {
1582  if ( item->tag == tag ) {
1583  for ( data = item->data; data; data = data->next )
1584  cout << " " << *data;
1585  }
1586  }
1587  }
1588  cout << endl;
1589 
1590  return 0;
1591 }
1592 
1603 int item_delete_all(Bstar* star, const char* tag)
1604 {
1605  Bstar_block* block;
1606 
1607  if ( verbose & VERB_FULL )
1608  cout << "Deleting all items associated with tag \"" << tag << "\"" << endl;
1609 
1610  for ( block = star->block; block; block = block->next )
1611  item_delete_from_block(block, tag);
1612 
1613  return 0;
1614 }
1615 
1626 int item_delete_from_block(Bstar_block* block, const char* tag)
1627 {
1628  Bstar_item* item, *item2;
1629 
1630  if ( verbose & VERB_FULL )
1631  cout << "Deleting tag \"" << tag << "\" from block" << endl;
1632  for ( item = item2 = block->item; item; ) {
1633  if ( item->tag == tag ) {
1634  if ( item == block->item ) {
1635  block->item = item2 = item->next;
1636  kill_item(item);
1637  item = item2;
1638  } else {
1639  item2->next = item->next;
1640  kill_item(item);
1641  item = item2->next;
1642  }
1643  } else {
1644  item2 = item;
1645  item = item->next;
1646  }
1647  }
1648 
1649  return 0;
1650 }
1651 
1662 int block_delete(Bstar* star, Bstring& tag)
1663 {
1664  int del, ndel(0);
1665  Bstar_block* block, *block2;
1666  Bstar_item* item;
1667 
1668  if ( verbose & VERB_LABEL )
1669  cout << "Deleting all blocks associated with tag \"" << tag << "\"" << endl;
1670 
1671  for ( block = block2 = star->block; block; ) {
1672  for ( del = 0, item = block->item; item && del == 0; item = item->next )
1673  if ( item->tag == tag ) del = 1;
1674  if ( del ) {
1675  if ( block == star->block ) {
1676  star->block = block2 = block->next;
1677  kill_block(block);
1678  block = block2;
1679  } else {
1680  block2->next = block->next;
1681  kill_block(block);
1682  block = block2->next;
1683  }
1684  ndel++;
1685  } else {
1686  block2 = block;
1687  block = block->next;
1688  }
1689  }
1690 
1691  if ( verbose & VERB_LABEL )
1692  cout << "Blocks deleted: " << ndel << endl;
1693 
1694  return ndel;
1695 }
1696 
1709 int item_integer_scale_shift(Bstar* star, Bstring& tag, int iscale, int ishift)
1710 {
1711  int idata, total(0);
1712  char format[32];
1713  Bstar_block* block;
1714  Bstar_item* item;
1715  Bstring* data;
1716 
1717  if ( verbose & VERB_PROCESS ) {
1718  cout << "Modifying all integer items associated with tag \"" << tag << "\"" << endl;
1719  cout << "Scale and shift: " << iscale << " " << ishift << endl;
1720  } else if ( verbose & VERB_LABEL )
1721  cout << "Modifying all integer items associated with tag \"" << tag << "\"" << endl;
1722 
1723  for ( block = star->block; block; block = block->next ) {
1724  for ( item = block->item; item; item = item->next ) {
1725  if ( item->tag == tag ) {
1726  snprintf(format, 32, "%%%dd", item->maxlen);
1727  for ( data = item->data; data; data = data->next ) {
1728  idata = data->integer()*iscale + ishift;
1729  data[0] = Bstring(idata, format);
1730  total++;
1731  }
1732  }
1733  }
1734  }
1735 
1736  return total;
1737 }
1738 
1751 int item_float_scale_shift(Bstar* star, Bstring& tag, float scale, float shift)
1752 {
1753  int total(0), type, len;
1754  float fdata, maxpre;
1755  char format[32];
1756  Bstar_block* block;
1757  Bstar_item* item;
1758  Bstring* data;
1759 
1760  if ( verbose & VERB_PROCESS ) {
1761  cout << "Modifying all floating point items associated with tag \"" << tag << "\"" << endl;
1762  cout << "Scale and shift: " << scale << " " << shift << endl;
1763  } else if ( verbose & VERB_LABEL )
1764  cout << "Modifying all floating point items associated with tag \"" << tag << "\"" << endl;
1765 
1766 
1767  for ( block = star->block; block; block = block->next ) {
1768  for ( item = block->item; item; item = item->next ) {
1769  if ( item->tag == tag ) {
1770  type = item_get_format(item, format);
1771  if ( type < 2 ) snprintf(format, 32, "%%%d.0f", item->maxlen);
1772  maxpre = pow(10.0, 1.0*format[1])*scale + shift;
1773  if ( maxpre < 1 ) maxpre = 1;
1774  format[1] = (char) (log(maxpre)/log(10.0) + 1) + '0';
1775  if ( verbose & VERB_DEBUG )
1776  cout << "DEBUG item_float_scale_shift: format = " << format << endl;
1777  for ( data = item->data; data; data = data->next ) {
1778  fdata = data->real()*scale + shift;
1779  data[0] = Bstring(fdata, format);
1780  len = (*data).length();
1781  if ( item->maxlen < len ) item->maxlen = len;
1782  total++;
1783  }
1784  }
1785  }
1786  }
1787 
1788  return total;
1789 }
1790 
1803 int loop_set_identifier(Bstar_block* block, int loop, int n, ...)
1804 {
1805  va_list ap;
1806  char* tag_pattern;
1807  int i, nloop(0);
1808  Bstar_item* item;
1809 
1810  va_start(ap, n);
1811  for ( i=0; i<n; i++ ) {
1812  tag_pattern = va_arg(ap, char *);
1813  for ( item = block->item; item; item = item->next ) {
1814  if ( strstr(item->tag.c_str(), tag_pattern) == item->tag.c_str() ) {
1815  item->loop = loop;
1816  nloop++;
1817  }
1818  }
1819  }
1820  va_end(ap);
1821 
1822  if ( verbose & VERB_DEBUG_STAR )
1823  cout << "DEBUG loop_set_identifier: loop=" << loop << " number=" << nloop << endl;
1824 
1825  return i;
1826 }
1827 
1837 int item_get_format(Bstar_item* item, char* format)
1838 {
1839  int type(0), pre, post, maxpre(0), maxpost(0);
1840  Bstring* data;
1841 
1842  if ( item->type == StringItem )
1843  strcpy(format, "%s");
1844  else {
1845  type = 1;
1846  for ( data = item->data; data; data = data->next ) {
1847  if ( data->find(".") > -1 && data->length() > 1 ) {
1848  type = 2;
1849  pre = (data->pre('.')).length();
1850  post = (data->post('.')).length();
1851  if ( maxpre < pre ) maxpre = pre;
1852  if ( maxpost < post ) maxpost = post;
1853  }
1854  if ( item->maxlen < data->length() ) item->maxlen = data->length();
1855  }
1856  if ( type == 2 )
1857  snprintf(format, 32, "%%%d.%df", maxpre + maxpost + 1, maxpost);
1858  else
1859  snprintf(format, 32, "%%%dd", item->maxlen);
1860  }
1861 
1862  return type;
1863 }
1864 
Bstar_item::data
Bstring * data
Definition: rwstar.h:43
Bstar_item
Definition: rwstar.h:36
clean_line
char * clean_line(char *string)
Definition: rwstar.cpp:23
MAXLINELEN
#define MAXLINELEN
Definition: utilities.h:45
Bstar_block::number
int number
Definition: rwstar.h:57
Bstring::remove
Bstring remove(const char c)
Definition: Bstring.cpp:473
item_delete_from_block
int item_delete_from_block(Bstar_block *block, const char *tag)
Deletes an item associated with a given tag from a block in the STAR data base.
Definition: rwstar.cpp:1626
kill_block
int kill_block(Bstar_block *block)
Destroys a STAR data block.
Definition: rwstar.cpp:163
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:116
Bstar_item::comment
Bstring comment
Definition: rwstar.h:42
block_delete
int block_delete(Bstar *star, Bstring &tag)
Deletes all blocks with a given tag from the STAR data base.
Definition: rwstar.cpp:1662
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:768
file_util.h
Header file for file checking functions.
Bstring
Definition: Bstring.h:29
write_block
int write_block(ofstream *fstar, Bstar_block *block, int linelength)
Writes paramaters and data from a STAR data block into an open file.
Definition: rwstar.cpp:750
add_item
char * add_item(char **list, unsigned long size)
Adds an item to a linked list.
Definition: linked_list.cpp:26
tag_compare
bool tag_compare(Bstring item_tag, Bstring tag)
Definition: rwstar.cpp:91
read_loop_items
Bstar_item * read_loop_items(ifstream *fstar)
Reads a loop structure with multiple items into a STAR data block from an open file.
Definition: rwstar.cpp:528
verbose
int verbose
Bstring::post_rev
Bstring post_rev(char c)
Definition: Bstring.cpp:436
Bstring::integer
long integer()
Definition: Bstring.cpp:547
Bstring::replace
Bstring replace(char cold, char cnew)
Definition: Bstring.cpp:465
Bstar::comment
Bstring comment
Definition: rwstar.h:81
error_show
int error_show(string message, string file, int line)
Displays the error with file and line reference.
Definition: utilities.cpp:719
item_find_or_make
Bstar_item * item_find_or_make(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1105
Bstring::empty
bool empty()
Definition: Bstring.h:71
Bstring::real
double real()
Definition: Bstring.cpp:553
item_put_float
int item_put_float(Bstar_block *block, const char *tag, float value, const char *format)
Writes a floating point value into a data item associated with a specific data block and tag in a STA...
Definition: rwstar.cpp:1401
Bstring::post
Bstring post(char c)
Definition: Bstring.cpp:402
length
double length(vector< T > v)
Definition: Vector3.h:530
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
count_list
long count_list(char *list)
Counts the number of items in a linked list.
Definition: linked_list.cpp:235
star_update_comment
int star_update_comment(Bstar *star, int n, char **strings)
Puts a set of strings and time in the main comment of the STAR data base.
Definition: rwstar.cpp:880
read_star
int read_star(const char *filename, Bstar *star)
Reads paramaters and data into a STAR data base from a list of files.
Definition: rwstar.cpp:224
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
Bstar_item::maxlen
int maxlen
Definition: rwstar.h:41
item_put_angle_list
int item_put_angle_list(Bstar_block *block, const char *tag, char *list, size_t offset, const char *format)
Writes a list of angular values in degrees into a data item associated with a specific data block and...
Definition: rwstar.cpp:1528
kill_star
int kill_star(Bstar *star)
Destroys a STAR data base.
Definition: rwstar.cpp:132
Bstar_item::loop
int loop
Definition: rwstar.h:40
item_find
Bstar_item * item_find(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1083
item_put_string
int item_put_string(Bstar_block *block, const char *tag, char *string)
Writes a string into a data item associated with a specific data block and tag in a STAR data base.
Definition: rwstar.cpp:1300
star_list_comments
long star_list_comments(Bstar *star, long len)
Lists the command lines in the STAR header.
Definition: rwstar.cpp:907
Bstar_block::tag
Bstring tag
Definition: rwstar.h:58
Bstar_block::item
Bstar_item * item
Definition: rwstar.h:61
Bstring::pre
Bstring pre(char c)
Definition: Bstring.cpp:389
StringItem
@ StringItem
Definition: rwstar.h:25
Bstring::find
long find(const Bstring &s)
Definition: Bstring.h:104
item_delete_all
int item_delete_all(Bstar *star, const char *tag)
Deletes all items associated with a given tag from the STAR data base.
Definition: rwstar.cpp:1603
Bstring::pre_rev
Bstring pre_rev(char c)
Definition: Bstring.cpp:421
read_single_item
Bstar_item * read_single_item(ifstream *fstar, char *aline)
Reads a single-valued item or comment line into a STAR data block from an open file.
Definition: rwstar.cpp:427
Bstar_item::tag
Bstring tag
Definition: rwstar.h:38
Bstring::right
Bstring right(long len)
Definition: Bstring.cpp:375
detect_and_fix_carriage_return
int detect_and_fix_carriage_return(const char *filename)
Detects carriage returns in text files and converts them to new-lines.
Definition: file_util.cpp:377
item_put_string_list
int item_put_string_list(Bstar_block *block, const char *tag, Bstring *list)
Writes a list of strings into a data item associated with a specific data block and tag in a STAR dat...
Definition: rwstar.cpp:1337
Bstar_item::next
Bstar_item * next
Definition: rwstar.h:37
LINELENGTH
#define LINELENGTH
Definition: rwstar.h:12
item_index
int item_index(Bstar_block *block, const char *tag)
Gets the STAR item associated with a tag in a STAR data block.
Definition: rwstar.cpp:1029
VERB_DEBUG_STAR
#define VERB_DEBUG_STAR
Definition: utilities.h:117
read_block
Bstar_block * read_block(ifstream *fstar, char *aptr, Bstring &filename)
Reads paramaters and data into a STAR data block from an open file.
Definition: rwstar.cpp:356
no_square_brackets
int no_square_brackets(Bstar *star)
Definition: rwstar.cpp:73
show_tags
int show_tags(Bstar *star)
Prints the list of tags in the STAR data base.
Definition: rwstar.cpp:991
NumberItem
@ NumberItem
Definition: rwstar.h:26
Bstar_block::comment
Bstring comment
Definition: rwstar.h:59
block_find_with_tag
Bstar_block * block_find_with_tag(Bstar *star, const char *tag)
Gets the first block associated with a tag in a STAR data base.
Definition: rwstar.cpp:1050
M_PI
#define M_PI
Definition: utilities.h:63
utilities.h
Header file for general utilities.
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:22
Bstring::substr
Bstring substr(long start, long len)
Definition: Bstring.cpp:332
Bstring::length
long length() const
Definition: Bstring.cpp:263
star_set_string_lengths
int star_set_string_lengths(Bstar *star)
Sets the maximum string lengths for each item in each block.
Definition: rwstar.cpp:938
item_list
int item_list(Bstar *star, Bstring &tag)
Lists all items associated with a given tag from the STAR data base.
Definition: rwstar.cpp:1571
item_get_string
char * item_get_string(Bstar_block *block, const char *tag)
Gets a string value associated with a tag in a STAR data block.
Definition: rwstar.cpp:1189
loop_set_identifier
int loop_set_identifier(Bstar_block *block, int loop, int n,...)
Assigns a loop identification number to items.
Definition: rwstar.cpp:1803
item_put_list
int item_put_list(Bstar_block *block, const char *tag, char *list, size_t offset, const char *format)
Writes a list of string or numeric values into a data item associated with a specific data block and ...
Definition: rwstar.cpp:1436
Bstring::left
Bstring left(long len)
Definition: Bstring.cpp:366
Bstring::next
Bstring * next
Definition: Bstring.h:31
Bstar::line_length
int line_length
Definition: rwstar.h:80
item_integer_scale_shift
int item_integer_scale_shift(Bstar *star, Bstring &tag, int iscale, int ishift)
Scales and shifts all items associated with a given tag from the STAR data base.
Definition: rwstar.cpp:1709
linked_list.h
Header file for generalized linked list functions.
item_get_integer
int item_get_integer(Bstar_block *block, const char *tag)
Gets an integer value associated with a tag in a STAR data block.
Definition: rwstar.cpp:1245
item_put_float_array
int item_put_float_array(Bstar_block *block, const char *tag, int number, float *value, const char *format)
Writes a list of floating point numbers into a data item associated with a specific data block and ta...
Definition: rwstar.cpp:1486
init_star
Bstar * init_star()
Creates a STAR data base.
Definition: rwstar.cpp:111
item_get_number
long item_get_number(Bstar *star, const char *tag)
Gets the number of values associated with a tag in a STAR data base.
Definition: rwstar.cpp:1134
item_get_float
float item_get_float(Bstar_block *block, const char *tag)
Gets a floating point value associated with a tag in a STAR data block.
Definition: rwstar.cpp:1273
Bstring::split
Bstring * split()
Definition: Bstring.cpp:605
VERB_FULL
#define VERB_FULL
Definition: utilities.h:113
item_copy_string
int item_copy_string(char *string, Bstar_block *block, const char *tag)
Copies a string value associated with a tag in a STAR data block.
Definition: rwstar.cpp:1220
write_star
int write_star(const char *filename, Bstar *star)
Writes a STAR data base to one or more STAR format files.
Definition: rwstar.cpp:666
rwstar.h
A header file for STAR format files.
Bstar::block
Bstar_block * block
Definition: rwstar.h:82
kill_item
int kill_item(Bstar_item *item)
Destroys an item in a data block in a STAR data base.
Definition: rwstar.cpp:197
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
item_get_number_for_block
long item_get_number_for_block(Bstar_block *block, const char *tag)
Gets the number of values associated with a tag in a STAR data block.
Definition: rwstar.cpp:1163
Bstar_block::filename
Bstring filename
Definition: rwstar.h:60
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:110
Bstar_block
Definition: rwstar.h:55
Bstar_block::next
Bstar_block * next
Definition: rwstar.h:56
Bstar::split
int split
Definition: rwstar.h:79
item_change_tag
int item_change_tag(Bstar *star, const char *tag, const char *newtag)
Replaces one tag with another in the STAR data base.
Definition: rwstar.cpp:966
string_add
Bstring * string_add(Bstring **list, const char *string)
Adds a string to a linked list.
Definition: Bstring.cpp:713
item_put_integer
int item_put_integer(Bstar_block *block, const char *tag, int value, const char *format)
Writes a integer into a data item associated with a specific data block and tag in a STAR data base.
Definition: rwstar.cpp:1368
Bstar_item::type
StarType type
Definition: rwstar.h:39
Bstar
Definition: rwstar.h:78
item_get_format
int item_get_format(Bstar_item *item, char *format)
Gets the format from the item.
Definition: rwstar.cpp:1837
item_float_scale_shift
int item_float_scale_shift(Bstar *star, Bstring &tag, float scale, float shift)
Scales and shifts all items associated with a given tag from the STAR data base.
Definition: rwstar.cpp:1751