/* since lists are stored in reverse order, need to count from end */ int count_node_from_end(node *n) { if (n->next == NULL) { if (n->key == KEY_COUNTER) return 0; #ifdef DEBUG_ON fprintf(stderr, "note %d: '%s'\n",1,n->str); #endif return 1; /* reserve 0 for not found */ } #ifdef DEBUG_ON fprintf(stderr, "note %d: '%s'\n",count_node_from_end(n->next) +1,n->str); #endif return (count_node_from_end(n->next) + 1); }
/* since lists are stored in reverse order, need to count from end */ int count_node_from_end(node *n) { if (n->next == NULL) { if (n->key == KEY_COUNTER) return 0; return 1; /* reserve 0 for not found */ } return (count_node_from_end(n->next) + 1); }
/* note_number_for_label -- given a label to match, determine number to be used*/ int note_number_for_label(char *text, scratch_pad *scratch) { node *n = NULL; char *clean; char *label; #ifdef DEBUG_ON fprintf(stderr, "find note number for: %s\n",text); #endif if ((text == NULL) || (strlen(text) == 0)) return 0; /* Nothing to find */ clean = clean_string(text); label = label_from_string(clean); /* have we used this note already? */ /* look for label string as is */ n = node_matching_label(clean, scratch->used_notes); /* if not, look in reserve queue */ if (n == NULL) { n = node_matching_label(clean, scratch->notes); if (n != NULL) { /* move to used queue */ move_note_to_used(n, scratch); } } /* Check label version */ if (n == NULL) n = node_matching_label(label, scratch->used_notes); if (n == NULL) { n = node_matching_label(label, scratch->notes); if (n != NULL) { /* move to used queue */ move_note_to_used(n, scratch); } } /* CAN recursively drill down to start counter at 0 and ++ */ /* if found, move to used queue and return the number */ free(label); free(clean); if (n != NULL) return count_node_from_end(n); else return 0; }
/* note_number_for_node -- given a note reference to match, determine number to be used*/ int note_number_for_node(node *ref, scratch_pad *scratch) { char *label = ref->str; node *n = NULL; int num = 0; num = note_number_for_label(label, scratch); if (num > 0) return num; /* None found, so treat as inline note */ n = ref->children; use_inline_footnote(ref, scratch); return count_node_from_end(n); }
/* node_for_count -- given a number, get that node */ node * node_for_count(node *n, int count) { if (n == NULL) return NULL; int total = count_node_from_end(n); if (count > total) return NULL; if (count == total) return n; while (total > count) { n = n->next; if (n == NULL) return NULL; total--; } return n; }