void stress_test_dll(int amt) { dll l1; dll l2; gendata x, y; int i; l1 = dll_create(); l2 = dll_create(); assert(dll_empty(l1)); assert(dll_empty(l2)); printf("Filling two dlls with 2 * %d items...\n", amt); for (i = 0; i < amt; ++i) { x.num = i; l1 = dll_prepend_head(l1, x); l2 = dll_prepend_head(l2, x); assert(!dll_empty(l1)); assert(!dll_empty(l2)); l1 = dll_append_head(l1, x); l2 = dll_append_head(l2, x); assert(!dll_empty(l1)); assert(!dll_empty(l2)); } /* Do some funky inserting at a `random' position. */ dll_append_head(dll_forward(l1, 1), x); assert(x.num == dll_get_data(dll_forward(l1, 1)).num); dll_remove_head(dll_forward(l1, 2), NULL); l1 = dll_append(l1, l2); assert(dll_count(l1) == (unsigned int)(4 * amt)); /* From now on, l2 is `invalid' */ printf("Removing 2 * 2 * %d items from the appended dll...\n", amt); for (i = 0; i < (2 * amt); ++i) { assert(!dll_empty(l1)); x = dll_get_data(l1); l1 = dll_remove_head(l1, NULL); assert(!dll_empty(l1)); y = dll_get_data(l1); l1 = dll_remove_head(l1, NULL); /* * We have to count backwards in this check, since we're * using the list like a stack, prepending all the time. */ assert(x.num == amt - (i % amt) - 1); assert(x.num == y.num); } assert(dll_empty(l1)); dll_destroy(l1, NULL); }
void readAndCreateStruct(const char * filename) { DlList_T myList=dll_create(); FILE* pFile = fopen(filename, "r"); if (!pFile) { perror("The following error occurred:"); } else { char* buf = (char*) malloc(sizeof(char) * MAX_LINE); size_t n=MAX_LINE; while(getline(&buf, &n, pFile)!=-1) { strtok(buf,"\n"); void* input=malloc(sizeof(char)*(strlen(buf))); printf("%s\n",buf ); memcpy(input,buf,strlen(buf)); dll_append(myList, input); } //printList(myList); free(buf); fclose(pFile); } dll_move_to(myList,dll_size(myList)); startLookingforInput(myList,filename); }
static PyObject* getNodesInNetwork(PyObject* self, PyObject* args) { PyObject *pylist = NULL; repa_sock_t sock; int i; struct dllist *list = NULL; struct dll_node *aux = NULL; if (PyArg_ParseTuple(args, "(ii)", &sock.sock_send, &sock.sock_recv)) { dll_create(list); // Create a linkedlist // Get the list of nodes in network in a linkedlist and parse to python's list if (repa_get_nodes_in_network(sock, list) >= 0) { // Create a list in python with the same size of linkedlist pylist = PyTuple_New(list->num_elements); // Put all the objects in the linkedlist into a python's list for (aux = list->head, i = 0; aux != NULL; aux = aux->next, i++) { PyTuple_SetItem(pylist, i, PyInt_FromLong((int)*((prefix_addr_t*)aux->data))); } } dll_destroy_all(list); // Destroy the linkedlist } else { PyErr_SetString(RepaError, "Error on read parameters"); return NULL; } return pylist; }
int main4(int argc, char **argv) { jlg_init(); JLG_DEBUG_ON(); JLG_LOG("starting"); double_linked_list_t list; dll_create(&list); dll_push(&list, "coucou"); dll_node_t *coucou_nodep = list.nodep; dll_push(&list, "hello"); dll_push(&list, "salut"); dll_node_t *nodep = list.nodep; while (nodep) { JLG_DEBUG("value = %s", (char *) nodep->valuep); nodep = nodep->nextp; } dll_remove(&list, coucou_nodep); void *buffer = NULL; while (dll_pop(&list, &buffer) != EODLL) { JLG_DEBUG("item = %s", (char *) buffer); } //cleanup: return JLG_RETURN_CODE; }
/** * repad_net_init - Initialize all structures used to receive packets from network * * @param id The id of thread which call this function * * @return <0 for errors */ static int repad_net_init(long id) { int result_raw = 0, result_udp = 0, result_overlay = 0; repad_attr.th_network_threads[TH_RAW_SOCKET] = 0; repad_attr.th_network_threads[TH_UDP_SOCKET] = 0; repad_attr.th_network_threads[TH_OVERLAY_NET] = 0; /* Create a new prod_cons for receive packets from network */ repad_attr.pc_receive_packets = pc_create(10, NULL, NULL, NULL, (void(*)(void*))skb_release); if (repad_attr.pc_receive_packets == NULL) { repad_printf(LOG_INFO, "Th%ld: ERROR: On create prod_cons for received packets\n"); return -1; } /* Create HashMap for memory message */ repad_attr.hm_memory_message = hmap_create(hmap_hash_mem_fn, hmap_eq_mem_fn, NULL, (void (*)(hmap_key_t))dll_destroy_all, 0, 0.0); if (repad_attr.hm_memory_message == NULL) { repad_printf(LOG_INFO, "Th%ld: ERROR: On create map for memory message\n"); return -1; } /* Create list of packet with no match */ repad_attr.list_pkt_without_match = dll_create(upwm_compare_packet, NULL); if (repad_attr.list_pkt_without_match == NULL) { repad_printf(LOG_INFO, "Th%ld: ERROR: On create list for packets without match\n"); return -1; } /* Load ifaces informations */ load_all_ifaces_info(&repad_attr.list_all_ifaces); repad_printf(LOG_INFO, "Th%ld: It was found %d interface(s)...\n", id, dll_get_num_elements(repad_attr.list_all_ifaces)); /* Init raw socket */ repad_printf(LOG_INFO, "Th%ld: raw_socket_enable=%s - udp_socket_enable=%s" " - overlay_enable=%s\n", id, strbool(repad_attr.bl_raw_socket_enable), strbool(repad_attr.bl_udp_socket_enable), strbool(repad_attr.bl_overlay_enable)); if (repad_attr.bl_raw_socket_enable) { result_raw = repad_net_init_raw_socket(id); } /* Init UDP socket */ if (repad_attr.bl_udp_socket_enable) { result_udp = repad_net_init_udp_socket(id); } /* Init overlay */ if (repad_attr.bl_overlay_enable) { result_overlay = repad_net_init_overlay(id); } return ((result_raw >= 0) ? 0 : (result_udp >= 0) ? 0 : (result_overlay >= 0) ? 0 : -1); }
int main(int argc, char const *argv[]) { // creating a new list dll_t *list = dll_create(); dll_registerCompareFn(list, compareFn); dll_registerFreeFn(list, freeFn); dll_registerPrintFn(list, printFn); // ask for the number of persons to enter puts("How many persons do you like to enter?"); char input[sizeof(stdin)]; fgets(input, sizeof(stdin), stdin); int x; sscanf(input, "%d", &x); // ask for person data for x times int i; for(i = 0; i < x; i++) { dll_pushTail(list, askPersonData() ); } // print data dll_print(list); puts(""); // reverse the list puts("reverse"); dll_reverse(list); // use dll iterator functions in a for loop to print the reversed list for(dll_head(list); dll_hasNext(list); dll_next(list)) { printFn(list->curr->data); } puts("sort"); dll_sort(list); // use dll iterator functions in a while loop to print the sorted list dll_head(list); while(dll_hasNext(list)) { printFn(list->curr->data); dll_next(list); } printf("List size: %ld\n", dll_size(list)); // empty the whole list dll_clear(list); return 0; }
int main(void) { dict_h lh ; dict_iter iter ; int i ; int *ip ; lh = dll_create( int_comp, int_kcomp, 0 ) ; for ( i = 0 ; i < N ; i++ ) { nums[ i ] = 10-i ; if ( dll_insert( lh, &nums[ i ] ) != DICT_OK ) { printf( "Failed at %d\n", i ) ; exit( 1 ) ; } } printf( "Successor test\n" ) ; for ( ip=INTP(dll_minimum( lh )) ; ip ; ip=INTP(dll_successor( lh, ip )) ) printf( "%d\n", *ip ) ; printf( "Predecessor test\n" ) ; for ( ip=INTP(dll_maximum( lh )) ; ip ; ip=INTP(dll_predecessor( lh, ip )) ) printf( "%d\n", *ip ) ; printf( "Search/delete test\n" ) ; i = 7 ; ip = INTP( dll_search( lh, &i ) ) ; if ( ip == NULL ) printf( "Search failed\n" ) ; else if ( dll_delete( lh, ip ) != DICT_OK ) { printf( "Delete failed\n" ) ; exit( 0 ) ; } printf( "Successor test 2\n" ) ; for ( ip=INTP(dll_minimum( lh )) ; ip ; ip=INTP(dll_successor( lh, ip )) ) printf( "%d\n", *ip ) ; printf( "Predecessor test 2\n" ) ; for ( ip=INTP(dll_maximum( lh )) ; ip ; ip=INTP(dll_predecessor( lh, ip )) ) printf( "%d\n", *ip ) ; printf( "Iteration test\n" ) ; iter = dll_iterate( lh, DICT_FROM_START ) ; while (( ip = INTP( dll_nextobj( lh, iter ) ) )) if ( *ip == 5 ) (void) dll_delete( lh, ip ) ; else printf( "%d\n", *ip ) ; exit( 0 ) ; }
hash_t *hash_create_ex(hash_config_t *cfg) { JLG_CREATE(hashp, hash_t); hashp->cfg = cfg; // allocate the array and setup all pointers to NULL hashp->array = (linked_list_t **) malloc(sizeof(linked_list_t *) * hashp->cfg->key_size); memset(hashp->array, 0, sizeof(linked_list_t *) * hashp->cfg->key_size); // dlistp (for hash_keys) hashp->dlistp = dll_create(); return hashp; }
int main(void) { DlList_T myList =dll_create(); void *one=5; void *two=6; void *three=9; dll_append(myList,one); dll_append(myList,two); dll_append(myList,three); dll_move_to(myList,2); showCursor(myList); dll_move_to(myList,1); showCursor(myList); dll_move_to(myList,3); showCursor(myList); dll_move_to(myList,257); showCursor(myList); dll_move_to(myList,1); showCursor(myList); printList(myList); printf("SIZE IS %d\n",dll_size(myList)); dll_clear(myList); dll_append(myList,one); dll_append(myList,two); dll_append(myList,three); dll_move_to(myList,2); showCursor(myList); dll_move_to(myList,1); showCursor(myList); dll_move_to(myList,3); showCursor(myList); dll_move_to(myList,257); showCursor(myList); dll_move_to(myList,1); showCursor(myList); printList(myList); printf("SIZE IS %d\n",dll_size(myList)); return 0; }
struct DLLHASHTABLE* dllhashtable_create( unsigned int nslots, HASHTABLE_COMPFUNC compfunc, HASHTABLE_HASHFUNC hashfunc, HASHTABLE_DELVALUEFUNC hashtable_delvaluefunc) { struct DLLHASHTABLE* dllhashtable_ptr; struct DLL* dll_ptr; struct HASHTABLE* hashtable_ptr; dllhashtable_ptr = (struct DLLHASHTABLE*)malloc( sizeof(struct DLLHASHTABLE)); dll_ptr = dll_create(voidfunc); hashtable_ptr = hashtable_create(nslots, compfunc, hashfunc, dllhashtable_hashtable_delvaluefunc); dllhashtable_ptr->dll_ptr = dll_ptr; dllhashtable_ptr->hashtable_ptr = hashtable_ptr; // dllhashtable_ptr->dll_delvaluefunc= dll_delvaluefunc; dllhashtable_ptr->hashtable_delvaluefunc = hashtable_delvaluefunc; return dllhashtable_ptr; }
int main(void) { doublylinkedlist *sample = dll_create(); doublylinkedlistnode node; char input[15][30] = {0, }; int position = 0; int element = 0; printf("ListCommader\n"); printf("Set list name: "); //scanf("%s", &input[14]); input[14][0] = 's'; input[14][1] = 'a'; input[14][2] = 'm'; input[14][3] = 'p'; input[14][4] = 'l'; input[14][5] = 'e'; printf("%s\n", input[14]); printf("\nHint: h<enter>\n"); while(true) { printf("Select list type: "); scanf("%s", &input[15]); if(!strcmp("q", input[15])) break; else if(!strcmp("h", input[15])) { printf("--------------------------------\n"); printf("Available list types \n"); printf("dll Use DoublyLinkedList \n"); printf("--------------------------------\n"); printf("Available commands \n"); printf("q Quit ListCommander \n"); printf("--------------------------------\n"); } else if(!strcmp("dll", input[15])) { printf("\nYou are in [%s:doublylinkedlist].\n", input[14]); while(true) { printf("Enter command: "); scanf("%s", &input[0]); if(!strcmp("h", input[0])) { printf("--------------------------------\n"); printf("Available commands \n"); printf("a Add element \n"); printf("r Remove element \n"); printf("c Clear all elements \n"); printf("l Show length \n"); printf("s Show all elements \n"); printf("q Close and delete list \n"); printf("--------------------------------\n"); } else if(!strcmp("a", input[0])) { printf("Position: "); scanf("%s", &input[1]); printf("Value of the element: (int)"); scanf("%s", &input[2]); position = strtol(input[1], NULL, 10); element = strtol(input[2], NULL, 10); node.element = element; dll_add_element(sample, position, node); printf("\"%d\" to %d added.\n", element, position); } else if(!strcmp("r", input[0])) { printf("Position: "); scanf("%s", &input[1]); position = strtol(input[1], NULL, 10); dll_remove_element(sample, position); printf("Element of position %d has been removed.\n", position); } else if(!strcmp("c", input[0])) dll_clear(sample); else if(!strcmp("l", input[0])) printf("Size: %d\n", dll_get_length(sample)); else if(!strcmp("s", input[0])) { printf("--------------------------------\n"); dll_interate(sample); printf("--------------------------------\n"); } else if(!strcmp("q", input[0])) break; else printf("%s is not a command\n"); } dll_delete(sample); printf("[%s:doublylinkedlist] has been deleted.\n", input[14], input[15]); printf("Hint: q<enter> again to quit\n"); } else printf("%s is not a list type or a command\n", input[15]); } return EXIT_SUCCESS; }
int main ( void ) { DlList_T myList; myList = dll_create(); if( myList == 0 ) { fputs( "Cannot create list!\n", stderr ); return( 1 ); } printf( "Initial list is %s\n", dll_empty( myList ) ? "empty" : "not empty" ); char* one = (char*)malloc( 11 * sizeof(char) ); char* two = (char*)malloc( 12 * sizeof(char) ); char* three = (char*)malloc( 11 * sizeof(char) ); strcpy( one, "First Line" ); strcpy( two, "Second Line" ); strcpy( three, "Third Line" ); printf( "Checking cursor initialized null...\n"); if( dll_has_next( myList ) ) { printf( "Your possition is valid\n" ); } else { printf( "Your possition is NOT valid\n" ); } // Test append printf( "List size: %d\n", dll_size( myList ) ); printf( "Adding \"%s\"\n", one ); dll_append( myList, one ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Adding \"%s\"\n", two ); dll_append( myList, two ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Adding \"%s\"\n", three ); dll_append( myList, three ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Checking cursor fixed with appends...\n"); if( dll_has_next( myList ) ) { printf( "Your possition is valid\n" ); } else { printf( "Your possition is NOT valid\n" ); } printf( "Test cursor movement...\n" ); if( dll_move_to( myList, 3 ) ) { printf( "You moved to an index you shouldn't be able to\n" ); } else { printf( "You can't move the cursor to 3\n" ); } if( dll_move_to( myList, 2 ) ) { printf( "moved to the last index\n" ); } else { printf( "movement problem to index 2\n" ); } if( dll_move_to( myList, 0 ) ) { printf( "moved to the first index\n" ); } else { printf( "movement problem to index 0\n" ); } printf( "Checking cursor still valid...\n" ); if( dll_has_next( myList ) ) { printf( "Your possition is valid\n" ); } else { printf( "Your possition is NOT valid\n" ); } printf( "Print state and test dll_next:\n" ); void* data = dll_next( myList ); int index = 0; // Index 0 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); index++; // Index 1 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); index++; // Index 2 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); index++; // Index 3 (Should be the same as index 2 as it should not exist) printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_next( myList ); printf( "Lets work backwards:\n" ); data = dll_prev( myList ); index = dll_size( myList ) - 1; // Index 2 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); index--; // Index 1 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); index--; // Index 0 printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); index--; // Index -1 (Should be same as index 0 as it should not exist) printf( "[%d] \"%s\"\n", index, (char*)data ); data = dll_prev( myList ); char* four = (char*)malloc( 12 * sizeof(char) ); char* five = (char*)malloc( 11 * sizeof(char) ); char* six = (char*)malloc( 11 * sizeof(char) ); char* seven = (char*)malloc( 13 * sizeof(char) ); char* eight = (char*)malloc( 12 * sizeof(char) ); strcpy( four, "Fourth Line" ); strcpy( five, "Fifth Line" ); strcpy( six, "Sixth Line" ); strcpy( seven, "Seventh Line" ); strcpy( eight, "Eighth Line" ); printf( "Testing inserts\n" ); dll_insert_at( myList, 0, six ); printf( "List size: %d\n", dll_size( myList ) ); dll_insert_at( myList, 2, seven ); printf( "List size: %d\n", dll_size( myList ) ); dll_insert_at( myList, 4, eight ); printf( "List size: %d\n", dll_size( myList ) ); printf( "Test full print and check inserts\n" ); index = 0; data = dll_get( myList, index ); while( data != NULL ) { printf( "[%d] \"%s\"\n", index, (char*)data ); index++; data = dll_get( myList, index ); } printf( "Test Sets\n" ); data = dll_set( myList, 0, five ); printf( "Switched \"%s\" with \"%s\"\n", (char*)data, five ); free( data ); data = dll_set( myList, 2, four ); printf( "Switched \"%s\" with \"%s\"\n", (char*)data, four ); free( data ); printf( "Test full print and check sets\n" ); index = 0; data = dll_get( myList, index ); while( data != NULL ) { printf( "[%d] \"%s\"\n", index, (char*)data ); index++; data = dll_get( myList, index ); } printf( "Testing popping\n" ); data = dll_pop( myList, dll_size( myList ) -1 ); printf( "Last element is: \"%s\"\n", (char*)data ); free( data ); data = dll_pop( myList, 2 ); printf( "Third element is: \"%s\"\n", (char*)data ); free( data ); printf( "Poping the rest...\n"); index = 0; data = dll_pop( myList, 0 ); while( data != NULL ) { printf( "[%d] \"%s\"\n", index, (char*)data ); free( data ); index++; data = dll_pop( myList, 0 ); } printf( "Destroying\n" ); dll_destroy( myList ); }
void noExplictparam() { printf("no file supplied\n"); DlList_T lst=dll_create(); int hasChanged=0; int looping=1; char buff[MAX_LINE]; while(looping) { fgets(buff,MAX_LINE, stdin); strtok(buff,"\n"); if(strcmp(buff,"Q")==0) { dll_destroy(lst); break; } else if(strcmp(buff,".")==0) { showCursor(lst); } else if(strcmp(buff,"a")==0) { int currentlooping=1; while(currentlooping) { fgets(buff,MAX_LINE, stdin); strtok(buff,"\n"); if(strcmp(buff,".")==0) { break; } else { void* input=malloc(sizeof(char)*(strlen(buff))); memcpy(input,buff,strlen(buff)); dll_append(lst, input); dll_move_to(lst,dll_size(lst)); hasChanged=1; //printf("SIZE %d\n",dll_size(lst) ); //showCursor(lst); //printList(lst); } } } else if(strcmp(buff, "\n")==0 || strcmp(buff,"+")==0) { if(getNext(lst)!=NULL) { dll_move_to(lst,getCursorNumber(lst) +1); } else { printf("?\n" ); } } else if(strcmp(buff,"-")==0) { if(getPrevious(lst)!=NULL) { dll_move_to(lst,getCursorNumber(lst) -1); } } else if(strcmp(buff,"$")==0) { if(getHead(lst)==NULL) { printf("?\n"); } else { dll_move_to(lst,dll_size(lst)); showCursor(lst); } } //NEEDS WORKS else if(isdigit(buff)) { printf("GOT HERE\n"); int newIndex=atoi(buff); if(newIndex>=1 && newIndex<=dll_size(lst)) { dll_move_to(lst,newIndex); } } else if(strcmp(buff,".=")==0) { printf("%d\n",getCursorNumber(lst)); } else if(strcmp(buff,"$=")==0) { printf("%d\n",dll_size(lst)); } else if(strcmp(buff,"p")==0) { printListForward(lst); dll_move_to(lst,dll_size(lst)); } else if(strcmp(buff,"q")==0) { if(hasChanged) { printf("? buffer dirty\n"); } else { dll_destroy(lst); printf("\n"); printf("Bye\n"); break; } } else if(strcmp(buff,"w")==0) { printf("?\n"); } else if(strcmp(buff,"wq")==0) { printf("?\n"); } else if(strcmp(buff,"i")==0) { int looping=1; while(looping) { fgets(buff,MAX_LINE, stdin); printf("%d\n",strcmp(buff,".") ); if(strcmp(buff,".")==10) { break; } else { dll_insert_at(lst,getCursorNumber(lst),(void *) buff); dll_move_to(lst,getCursorNumber(lst)); } } } else if(strcmp(buff,"d")==0) { dll_pop(lst,getCursorNumber(lst)); } else { } } }
int main ( int argc, char* argv[] ) { DlList_T myList; // List buffer to keep track of inserting or appending DlList_T listBuff; myList = dll_create(); if( myList == 0 ) { fprintf( stderr, "Cannot create list!\n" ); return( 1 ); } listBuff = dll_create(); if( listBuff == 0 ) { fprintf( stderr, "Cannot create buffer!\n" ); return( 1 ); } // number of bytes for getline int nbytes = 80; // Set up a string to read user input and file char *str = NULL; // Set up name for saving files to char *saveFile = NULL; // Read in a file // Check to make sure there's arguments of the file to open if( argc == 2 ) { FILE* pFile = fopen(argv[1], "r"); if( !pFile ) { perror("open failed"); fprintf( stderr, "could not read file '%s'\n", argv[1] ); return( 1 ); } else { // The file has been opened. Go through // and fill the doubly linked list while( getline(&str, (size_t *) &nbytes, pFile) ) { // Kill the new line killNL(str); // Set up space char* toAdd = (char*)malloc( ( strlen(str) + 1 ) * sizeof(char) ); // Add the previous read in line to that space strcpy( toAdd, str ); // Add it to the list dll_append( myList, toAdd ); //Free 'str' free( str ); str = NULL; } fclose( pFile ); } } else { printf( "no file supplied\n" ); } // Keep track if we should stop or not bool running = true; // Keep track of changes bool buffChange = false; // Keeps track if we're appending or inserting bool grabText = false; char grabMode = 'a'; // Keeps track of the cursor movement void* lastData = NULL; while( running ) { // Read in the next line getline(&str, (size_t *) &nbytes, stdin); // Kill new line killNL(str); // We're either appending or inserting if( grabText ) { if( strlen( str ) == 1 && str[0] == '.' ) { // A single period was entered to stop the adding // Set grabText to false grabText = false; // If the mode was set to append if( grabMode == 'a' ) { // We go through and pop all the elements // off the buffer and append them to the list int index = 0; void* data = dll_pop( listBuff, 0 ); while( data != NULL ) { dll_append( myList, data ); index++; data = dll_pop( listBuff, 0 ); } } else if ( grabMode == 'i' ) { // We go through and pop all the elements // off the buffer and insert them to the list int index = 0; // Set the insert index as the current // pointer's index int insertIndex = getCursorIndex( myList ); void* data = dll_pop( listBuff, 0 ); while( data != NULL ) { dll_insert_at( myList, insertIndex, data ); index++; insertIndex++; data = dll_pop( listBuff, 0 ); } } } else { // Set up space char* toAdd = (char*)malloc( ( strlen(str) + 1 ) * sizeof(char) ); // Add the previous read in line to that space strcpy( toAdd, str ); // Add it to the list dll_append( listBuff, toAdd ); } } else if( str[0] == 'a' ) { grabText = true; grabMode = 'a'; buffChange = true; // Current line } else if( str[0] == '.' ) { // Print the index of the current line if( str[1] == '=' ) { printf( "%d\n", getCursorIndex( myList ) ); // Print the current line } else { printf( "%s\n", (char*)getCursorData( myList ) ); } // Advance cursor to the next line // String length of 1 means they hit enter } else if( strlen(str) == 0 || str[0] == '+' ) { // Try to advance the cursor void* data = dll_next( myList ); // If the cursor didn't advance, notify the user if( data == lastData ){ printf("?\n"); // Otherwise the cursor advanced, print the line } else { lastData = data; printf( "%s\n", (char*)data ); } // Advance the cursor to the previous line } else if( str[0] == '-' ) { // Try to advance the cursor void* data = dll_prev( myList ); // If the cursor didn't advance, notify the user if( data == lastData ){ printf("?\n"); // Otherwise the cursor advanced, print the line } else { lastData = data; printf( "%s\n", (char*)data ); } // Delete line } else if( str[0] == 'd' ) { if( dll_has_next( myList ) ){ int index = getCursorIndex( myList ); void* data = dll_pop( myList, index ); free( data ); buffChange = true; } } else if( str[0] == 'i' ) { grabText = true; grabMode = 'i'; buffChange = true; // Last element } else if( str[0] == '$' ) { // For both "$=" and "$" it prints a "?" upon // an empty list int size = dll_size( myList ); if( size == 0 ) { printf( "?\n" ); } else { // If the command was "$=" just print // the possition of the last element if( str[1] == '=' ) { printf( "%d\n", size ); // Otherwise move to the last position // and print the last element } else { dll_move_to( myList, size - 1 ); void* data = dll_get( myList, size - 1 ); printf( "%s\n", (char*)data ); } } // Print } else if( str[0] == 'p' ) { printList( myList ); // Save } else if( str[0] == 'w' ) { char grab[(strlen(str) + 1)]; // Check if a file name has beens pecified sscanf(str, "%*s %s", grab); // If the length is greater than '1' we set // it as the last acceptable saveFile if( strlen(grab) > 0 ) { if( saveFile != NULL ) { free( saveFile ); } saveFile = (char*)malloc( ( strlen(str) + 1 ) * sizeof(char) ); strcpy( saveFile, grab ); } // Check if a quit flag has been given bool quit = (str[1] == 'q'); if( saveFile == NULL || strlen(saveFile) <= 0 ) { // We don't have an acceptable file name to check continue; } // Open file to be saved FILE* pFile = fopen(saveFile, "w"); if( !pFile ) { perror("open failed: "); } else { printf( "file name: '%s'\n", saveFile ); // Go through each node of the doulby linked list int index = 0; void* data = dll_get( myList, index ); while( data != NULL ) { // Write to file fputs((char*)data, pFile); fputs("\n", pFile); index++; data = dll_get( myList, index ); } // Close fclose( pFile ); // If we want to quit afterwards if( quit ) { running = false; printf("\nBye\n"); } // reset the buffer flag buffChange = false; } // Soft quit } else if ( str[0] == 'q' ) { // If the buffer hasn't changed stop the program if( !buffChange ) { running = false; printf( "\nBye\n" ); // Otherwise warn that there's been changes } else { printf( "? buffer dirty.\n" ); } // Hard quit } else if ( str[0] == 'Q' ) { running = false; printf( "\nBye\n" ); } else { // Check if a number char* end; int checkNum = strtol( str, &end, 10 ); // If the conversion did not encounter a string // We know the number is good if( !*end ) { // If we can move to the requested index if( dll_move_to( myList, checkNum - 1 ) ) { // Print that index printf( "%s\n", (char*)getCursorData( myList ) ); } else { printf( "?\n" ); } } } // Free 'str' free( str ); // Set 'str' to NULL str = NULL; } // Destroy our list dll_destroy( myList ); // Destroy the buffer dll_destroy( listBuff ); }