Esempio n. 1
0
int top_publish(char *name, char *message, RpcEndpoint *ep) {
    int ret = 0;
    Topic *st;

    if (tshm_get(topicTable, name, (void **)&st)) {
        ret = 1;
        pthread_mutex_lock(&(st->lock));
        if (ll_size(st->regAUs) > 0L) {
            Iterator *iter;
            iter = ll_it_create(st->regAUs);
            if (iter) {
                Event *event;
                event = ev_create(name, message, ep, ll_size(st->regAUs));
                if (event) {
                    unsigned long id;
                    while (it_hasNext(iter)) {
                        (void) it_next(iter, (void **)&id);
                        au_publish(id, event);
                    }
                }
                it_destroy(iter);
            }
        }
        pthread_mutex_unlock(&(st->lock));
    }
    return ret;
}
Esempio n. 2
0
int main(int argc, char *argv[]) {
   LinkedList *ll = NULL;
   TreeSet *ts = NULL;
   char *sp;
   char pattern[4096];
   RegExp *reg;
   Iterator *it;

   if (argc < 2) {
      fprintf(stderr, "Usage: ./fileCrawler pattern [dir] ...\n");
      return -1;
   }
   /*
    * convert bash expression to regular expression and compile
    */
   cvtPattern(pattern, argv[1]);
   if ((reg = re_create()) == NULL) {
      fprintf(stderr, "Error creating Regular Expression Instance\n");
      return -1;
   }
   if (! re_compile(reg, pattern)) {
      char eb[4096];
      re_status(reg, eb, sizeof eb);
      fprintf(stderr, "Compile error - pattern: `%s', error message: `%s'\n",
              pattern, eb);
      re_destroy(reg);
      return -1;
   }
   /*
    * create linked list and treeset
    */
   if ((ll = ll_create()) == NULL) {
      fprintf(stderr, "Unable to create linked list\n");
      goto done;
   }
   if ((ts = ts_create(scmp)) == NULL) {
      fprintf(stderr, "Unable to create tree set\n");
      goto done;
   }
   /*
    * populate linked list
    */
   if (argc == 2) {
      if (! processDirectory(".", ll, 1))
         goto done;
   } else {
      int i;
      for (i = 2; i < argc; i++) {
         if (! processDirectory(argv[i], ll, 1))
            goto done;
      }
   }
   /*
    * for each directory in the linked list, apply regular expression
    */
   while (ll_removeFirst(ll, (void **)&sp)) {
      int stat = applyRe(sp, reg, ts);
      free(sp);
      if (! stat)
         break;
   }
   /*
    * create iterator to traverse files matching pattern in sorted order
    */
   if ((it = ts_it_create(ts)) == NULL) {
      fprintf(stderr, "Unable to create iterator over tree set\n");
      goto done;
   }
   while (it_hasNext(it)) {
      char *s;
      (void) it_next(it, (void **)&s);
      printf("%s\n", s);
   }
   it_destroy(it);
/*
 * cleanup after ourselves so there are no memory leaks
 */
done:
   if (ll != NULL)
      ll_destroy(ll, free);
   if (ts != NULL)
      ts_destroy(ts, free);
   re_destroy(reg);
   return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[]) {
    char buf[1024];
    char *p;
    LinkedList *ll;
    long i, n;
    FILE *fd;
    char **array;
    Iterator *it;

    if (argc != 2) {
        fprintf(stderr, "usage: ./lltest file\n");
        return -1;
    }
    if ((ll = ll_create()) == NULL) {
        fprintf(stderr, "Error creating linked list of strings\n");
        return -1;
    }
    if ((fd = fopen(argv[1], "r")) == NULL) {
        fprintf(stderr, "Unable to open %s to read\n", argv[1]);
        return -1;
    }
    /*
     * test of add()
     */
    printf("===== test of add\n");
    while (fgets(buf, 1024, fd) != NULL) {
        if ((p = strdup(buf)) == NULL) {
            fprintf(stderr, "Error duplicating string\n");
            return -1;
        }
        if (!ll_add(ll, p)) {
            fprintf(stderr, "Error adding string to linked list\n");
            return -1;
        }
    }
    fclose(fd);
    n = ll_size(ll);
    /*
     * test of get()
     */
    printf("===== test of get\n");
    for (i = 0; i < n; i++) {
        if (!ll_get(ll, i, (void **)&p)) {
            fprintf(stderr, "Error retrieving %ld'th element\n", i);
            return -1;
        }
        printf("%s", p);
    }
    /*
     * test of remove
     */
    printf("===== test of remove\n");
    for (i = n - 1; i >= 0; i--) {
        if (!ll_remove(ll, i, (void **)&p)) {
            fprintf(stderr, "Error removing string from linked list\n");
            return -1;
        }
        free(p);
    }
    /*
     * test of destroy with NULL userFunction
     */
    printf("===== test of destroy(NULL)\n");
    ll_destroy(ll, NULL);
    /*
     * test of insert
     */
    if ((ll = ll_create()) == NULL) {
        fprintf(stderr, "Error creating linked list of strings\n");
        return -1;
    }
    fd = fopen(argv[1], "r");		/* we know we can open it */
    printf("===== test of insert\n");
    while (fgets(buf, 1024, fd) != NULL) {
        if ((p = strdup(buf)) == NULL) {
            fprintf(stderr, "Error duplicating string\n");
            return -1;
        }
        if (!ll_insert(ll, 0, p)) {
            fprintf(stderr, "Error adding string to linked list\n");
            return -1;
        }
    }
    fclose(fd);
    for (i = 0; i < n; i++) {
        if (!ll_get(ll, i, (void **)&p)) {
            fprintf(stderr, "Error retrieving %ld'th element\n", i);
            return -1;
        }
        printf("%s", p);
    }
    /*
     * test of set
     */
    printf("===== test of set\n");
    for (i = 0; i < n; i++) {
        char bf[1024], *q;
        sprintf(bf, "line %ld\n", i);
        if ((p = strdup(bf)) == NULL) {
            fprintf(stderr, "Error duplicating string\n");
            return -1;
        }
        if (!ll_set(ll, i, p, (void **)&q)) {
            fprintf(stderr, "Error replacing %ld'th element\n", i);
            return -1;
        }
        free(q);
    }
    /*
     * test of toArray
     */
    printf("===== test of toArray\n");
    if ((array = (char **)ll_toArray(ll, &n)) == NULL) {
        fprintf(stderr, "Error in invoking ll_toArray()\n");
        return -1;
    }
    for (i = 0; i < n; i++) {
        printf("%s", array[i]);
    }
    free(array);
    /*
     * test of iterator
     */
    printf("===== test of iterator\n");
    if ((it = ll_it_create(ll)) == NULL) {
        fprintf(stderr, "Error in creating iterator\n");
        return -1;
    }
    while (it_hasNext(it)) {
        char *p;
        (void) it_next(it, (void **)&p);
        printf("%s", p);
    }
    it_destroy(it);
    /*
     * test of destroy with free() as userFunction
     */
    printf("===== test of destroy(free)\n");
    ll_destroy(ll, free);

    return 0;
}