IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 2, 2005, 9:40:50 AM (21 years ago)
Author:
eugene
Message:

psched bugfixes, moved IOBuffers and queues

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.data/queues.c

    r4693 r4697  
    1 # include "opihi.h"
     1# include "data.h"
    22
    33/* These functions are somewhat fragile: we cannot do multiple CreateQueue
     
    5656}
    5757
     58void PushNamedQueue (char *name, char *line) {
     59
     60  Queue *queue;
     61 
     62  queue = FindQueue (name);
     63  if (queue == NULL) {
     64    queue = CreateQueue (name);
     65  }
     66  PushQueue (queue, line);
     67  return;
     68}
     69
     70/* push line onto queue.  return chars create new lines */
    5871void PushQueue (Queue *queue, char *line) {
    5972
    6073  int N;
    61 
     74  char *p, *q;
     75
     76  p = line;
     77  q = strchr (line, '\n');
    6278  N = queue[0].Nlines;
    63   queue[0].lines[N] = strcreate (line);
    64   queue[0].Nlines ++;
    65   if (queue[0].Nlines == queue[0].NLINES) {
    66     queue[0].NLINES += 10;
    67     REALLOCATE (queue[0].lines, char *, queue[0].NLINES);
    68   }   
     79  while (q != NULL) {
     80    queue[0].lines[N] = strncreate (p, q - p);
     81    N++;
     82    CHECK_REALLOCATE (queue[0].lines, char *, queue[0].NLINES, N, 16);
     83    p = q + 1;
     84    q = strchr (p, '\n');
     85  }   
     86  if (*p) {
     87    queue[0].lines[N] = strcreate (p);
     88    N++;
     89    CHECK_REALLOCATE (queue[0].lines, char *, queue[0].NLINES, N, 16);
     90  }
     91  queue[0].Nlines = N;
     92  return;
     93}
     94
     95char *ChooseKey (char *line, int Key) {
     96
     97  int i;
     98  char *key, *p;
     99
     100  key = line;
     101  if (Key != -1) {
     102    for (i = 0; (i < Key) && (key != NULL); i++) {
     103      p = nextword (key);
     104      key = p;
     105    }
     106    key = thisword (key);
     107  }
     108  return (key);
     109}
     110
     111/* push line onto queue, skipping existing matches (optionally by key) */
     112void PushQueueUnique (Queue *queue, char *line, int Key) {
     113
     114  int i, j, N, found;
     115  char *p, *q, *key1, *key2;
     116  Queue tmp;
     117
     118  /* init tmp queue */
     119  tmp.Nlines = 0;
     120  tmp.NLINES = 16;
     121  ALLOCATE (tmp.lines, char *, tmp.NLINES);
     122
     123  /* push entries on tmp queue */
     124  p = line;
     125  q = strchr (line, '\n');
     126  N = tmp.Nlines;
     127  while (q != NULL) {
     128    tmp.lines[N] = strncreate (p, q - p);
     129    N++;
     130    CHECK_REALLOCATE (tmp.lines, char *, tmp.NLINES, N, 16);
     131    p = q + 1;
     132    q = strchr (p, '\n');
     133  }   
     134  if (*p) {
     135    tmp.lines[N] = strcreate (p);
     136    N++;
     137    CHECK_REALLOCATE (tmp.lines, char *, tmp.NLINES, N, 16);
     138  }
     139  tmp.Nlines = N;
     140
     141  /* add unique entries in tmp to queue */
     142  for (i = 0; i < tmp.Nlines; i++) {
     143    key1 = ChooseKey (tmp.lines[i], Key);
     144    if (key1 == NULL) continue;
     145    found = FALSE;
     146    for (j = 0; !found && (j < queue[0].Nlines); j++) {
     147      key2 = ChooseKey (queue[0].lines[j], Key);
     148      if (key2 == NULL) continue;
     149      if (strcmp (key1, key2)) {
     150        if (Key != -1) free (key2);
     151        continue;
     152      }
     153      found = TRUE;
     154      if (Key != -1) free (key2);
     155    }     
     156    if (!found) PushQueue (queue, tmp.lines[i]);
     157    if (Key != -1) free (key1);
     158  }
     159  for (i = 0; i < tmp.Nlines; i++) {
     160    free (tmp.lines[i]);
     161  }
     162  free (tmp.lines);
     163  return;
     164}
     165
     166/* push line onto queue, replacing matches (optionally by Key) */
     167void PushQueueReplace (Queue *queue, char *line, int Key) {
     168
     169  int i, j, N, found;
     170  char *p, *q, *key1, *key2;
     171  Queue tmp;
     172
     173  /* init tmp queue */
     174  tmp.Nlines = 0;
     175  tmp.NLINES = 16;
     176  ALLOCATE (tmp.lines, char *, tmp.NLINES);
     177
     178  /* push entries on tmp queue */
     179  p = line;
     180  q = strchr (line, '\n');
     181  N = tmp.Nlines;
     182  while (q != NULL) {
     183    tmp.lines[N] = strncreate (p, q - p);
     184    N++;
     185    CHECK_REALLOCATE (tmp.lines, char *, tmp.NLINES, N, 16);
     186    p = q + 1;
     187    q = strchr (p, '\n');
     188  }   
     189  if (*p) {
     190    tmp.lines[N] = strcreate (p);
     191    N++;
     192    CHECK_REALLOCATE (tmp.lines, char *, tmp.NLINES, N, 16);
     193  }
     194  tmp.Nlines = N;
     195
     196  /* add unique entries in tmp to queue */
     197  for (i = 0; i < tmp.Nlines; i++) {
     198    key1 = ChooseKey (tmp.lines[i], Key);
     199    if (key1 == NULL) continue;
     200    found = FALSE;
     201    for (j = 0; !found && (j < queue[0].Nlines); j++) {
     202      key2 = ChooseKey (queue[0].lines[j], Key);
     203      if (key2 == NULL) continue;
     204      if (strcmp (key1, key2)) {
     205        if (Key != -1) free (key2);
     206        continue;
     207      }
     208      queue[0].lines[j] = strcreate (tmp.lines[i]);
     209      found = TRUE;
     210      if (Key != -1) free (key2);
     211    }     
     212    if (!found) PushQueue (queue, tmp.lines[i]);
     213    if (Key != -1) free (key1);
     214  }
     215  for (i = 0; i < tmp.Nlines; i++) {
     216    free (tmp.lines[i]);
     217  }
     218  free (tmp.lines);
    69219  return;
    70220}
Note: See TracChangeset for help on using the changeset viewer.