void exampleList() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_list(16); List* L = newList(); list_add(L, "a"); list_add(L, "b"); list_add(L, "c"); list_add(L, "d"); list_add(L, "e"); list_add(L, "f"); // Display the current list displayStrings(L); //ABCDEF // Remove first item list_removeFirst(L); displayStrings(L); //BCDEF // Add first item back list_addFirst(L, "a"); displayStrings(L); list_clear(L); if (list_isEmpty(L)) printf("List was cleared.\n"); // Add some strings and remove all that begin with - int nums[] = {1, 2, 3, 4, 6, 7, 8}; int x; for (x = 0; x < 7; x++) list_add(L, &nums[x]); displayIntegers(L); list_start(L); while (list_hasNext(L)) { // get does not move the current node x = *((int*)list_peek(L)); if (x % 2 == 0) // remove will remove the node from the list altogether and // return the data removed. list_remove(L); else // next will just goto the next node and return the data. list_next(L); } // Print out the odd numbers displayIntegers(L); // Try removing all while traversing list_start(L); while (list_hasNext(L)) list_remove(L); displayIntegers(L); if (L->first == NULL && L->last == NULL && L->size == 0) printf("All cleaned up!\n"); // Traverse through an array of strings and find any that start // with . and after it add 0 and add one before it that is - list_add(L, ".1"); list_add(L, " two "); list_add(L, ".3"); list_add(L, " four "); list_add(L, ".5"); displayStrings(L); list_start(L); char* c; while (list_hasNext(L)) { c = (char*)list_peek(L); if (c[0] == '.') { list_insertBefore(L, "-"); list_insertAfter(L, "0"); } list_next(L); } displayStrings(L); char* first = (char*)L->first->data; char* last = (char*)L->last->data; if (first[0] == '-' && last[0] == '+') printf("Insertions correct.\n"); // This will clear the list of any nodes and pool them and then free // the list itself from memory list_free(L); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_list(); }
int main(int argc, char *argv[]) { argv0 = argv[0]; shrc rc; // htmllabel stree:initcall // initialize connection to server SH_DO(Shore::init(argc, argv, 0, getenv("DOC_INDEX_RC"))); // get command-line options int c; while ((c = getopt(argc,argv,"aldpV")) != EOF) switch(c) { case 'a': operation = OP_ADD; break; case 'l': operation = OP_LIST; break; case 'd': operation = OP_DEL; break; case 'p': operation = OP_POOL_LIST; break; case 'V': verbose++; break; default: usage(); } if (operation == OP_NONE) usage(); // Start a transaction for initialization SH_BEGIN_TRANSACTION(rc); if (rc) rc.fatal(); // this terminates the program with extreme prejudice // Check that our demo directory exists rc = Shore::chdir(DEMO_DIR); if (rc != RCOK) { if (rc != RC(SH_NotFound)) SH_ABORT_TRANSACTION(rc); // Not found. Must be the first time through. // Create the directory SH_DO(Shore::mkdir(DEMO_DIR, 0755)); SH_DO(Shore::chdir(DEMO_DIR)); // htmllabel stree:createrepository // Make a new DocIndex object ... repository = new("repository", 0644) DocIndex; repository.update()->initialize(); // ... and a pool for allocating Nodes. SH_DO(nodes.create_pool("pool", 0644, nodes)); } else { // not first time // Get the repository root from the database ... SH_DO(Ref<DocIndex>::lookup("repository",repository)); // ... and the pool for creating nodes SH_DO(nodes.lookup("pool", nodes)); } SH_DO(SH_COMMIT_TRANSACTION); switch (operation) { case OP_ADD: add_files(argc-optind, argv+optind); break; case OP_LIST: if (optind != argc-1) usage(); list_files(argv[optind]); break; case OP_DEL: delete_files(argc-optind, argv+optind); break; case OP_POOL_LIST: pool_list(); break; default: break; } return 0; } // main