예제 #1
0
int testLinkedList(void) {
	Node *headAndTail[2];	
	headAndTail[0] = NULL; //head
	headAndTail[1] = NULL; //tail

	printf("Start Test of Linked List.\n");
	printf("Initial contents of the list:\n");
	printList(headAndTail[0]);

	printf("\naddToEnd() 2, 3 and 7 to an empty list\n");
	addToEnd(headAndTail, 2);
	printList(headAndTail[0]);
	addToEnd(headAndTail, 3);
	printList(headAndTail[0]);
	addToEnd(headAndTail, 7);
	printList(headAndTail[0]);

	printf("\nremoveFromEnd() three times to make the list empty again\n");
	removeFromEnd(headAndTail);
	printList(headAndTail[0]);
	removeFromEnd(headAndTail);
	printList(headAndTail[0]);
	removeFromEnd(headAndTail);
	printList(headAndTail[0]);

	printf("\naddToStart() 2, 3 and 7 to an empty list\n");
	addToStart(headAndTail, 2);
	printList(headAndTail[0]);
	addToStart(headAndTail, 3);
	printList(headAndTail[0]);
	addToStart(headAndTail, 7);
	printList(headAndTail[0]);

	printf("\nremoveFromStart() three times to make the list empty again\n");
	removeFromStart(headAndTail);
	printList(headAndTail[0]);
	removeFromStart(headAndTail);
	printList(headAndTail[0]);
	removeFromStart(headAndTail);
	printList(headAndTail[0]);

	printf("\n Test error handling: removeFromEnd() and removeFromStart() on empty list\n");
	removeFromEnd(headAndTail);
	removeFromStart(headAndTail);
	printf("\n Testing Completed - No Errors\n");

	return EXIT_SUCCESS;
}
예제 #2
0
void cacheCall(Cache *cache, int index, int *cacheIndex, bool *compute){
    CacheNode *requested = cache->cacheMap[index];
    CacheList *list = cache->list;
    if(cache->cacheMap[index] == NULL){// cache miss
        cache->misses++;
        *compute = true;
        if(cache->occupancy == cache->maxSize){
            //move first node to end
            requested = list->first;
            moveToEnd(list,requested);
            //rewrite map, overwriting the lru node and erasing it from the map
            cache->cacheMap[index] = requested;
            cache->cacheMap[requested->index] = NULL;
            int removed = requested->index;
            requested->index = index;
//            printf("Overwrote Node with index %d!\n",removed);
        }else{
            requested = createCacheNode(cache->occupancy,index);
            cache->cacheMap[index] = requested;
            addToEnd(list,requested);
            cache->occupancy++;
//            printf("Added node!\n");
        }
        *cacheIndex = requested->cacheLineNum;
    }else{// cache hit
        cache->hits++;
        //move to end
        moveToEnd(list, requested);
        *cacheIndex = requested->cacheLineNum;
        *compute = false;
    }
//    printf("Last node index: %d\n",cache->list->last->index);
}
예제 #3
0
파일: klists.cpp 프로젝트: OmerMor/scummvm
reg_t kAddToEnd(EngineState *s, int argc, reg_t *argv) {
	addToEnd(s, argv[0], argv[1]);

	if (argc == 3)
		s->_segMan->lookupNode(argv[1])->key = argv[2];

	return s->r_acc;
}
//See why pass **headRef in My_Tutorial/C/Mod_Caller_Ptr_in_Callee
void createList(struct ListNode **head, struct ListNode **tail)
{
    int num;

    printf("Enter data to create a list. (Enter -999 to end)\n");
    scanf("%d", &num);

    while (num != -999) {
        addToEnd(num, head, tail);
        scanf("%d", &num);
    }
}
예제 #5
0
파일: list.cpp 프로젝트: NeilSchlunegger/CS
void addToEnd(node*& head, int passedData)
{
	if(head == NULL)
	{
		head = new node;
		head->data = passedData;
		head->next = NULL;
		return;
	}	

	return addToEnd(head->next, passedData);
}
예제 #6
0
파일: klists.cpp 프로젝트: OmerMor/scummvm
reg_t kSort(EngineState *s, int argc, reg_t *argv) {
	SegManager *segMan = s->_segMan;
	reg_t source = argv[0];
	reg_t dest = argv[1];
	reg_t order_func = argv[2];

	int input_size = (int16)readSelectorValue(segMan, source, SELECTOR(size));
	reg_t input_data = readSelector(segMan, source, SELECTOR(elements));
	reg_t output_data = readSelector(segMan, dest, SELECTOR(elements));

	List *list;
	Node *node;

	if (!input_size)
		return s->r_acc;

	if (output_data.isNull()) {
		list = s->_segMan->allocateList(&output_data);
		list->first = list->last = NULL_REG;
		writeSelector(segMan, dest, SELECTOR(elements), output_data);
	}

	writeSelectorValue(segMan, dest, SELECTOR(size), input_size);

	list = s->_segMan->lookupList(input_data);
	node = s->_segMan->lookupNode(list->first);

	sort_temp_t *temp_array = (sort_temp_t *)malloc(sizeof(sort_temp_t) * input_size);

	int i = 0;
	while (node) {
		reg_t params[1] = { node->value };
		invokeSelector(s, order_func, SELECTOR(doit), argc, argv, 1, params);
		temp_array[i].key = node->key;
		temp_array[i].value = node->value;
		temp_array[i].order = s->r_acc;
		i++;
		node = s->_segMan->lookupNode(node->succ);
	}

	qsort(temp_array, input_size, sizeof(sort_temp_t), sort_temp_cmp);

	for (i = 0;i < input_size;i++) {
		reg_t lNode = s->_segMan->newNode(temp_array[i].value, temp_array[i].key);
		addToEnd(s, output_data, lNode);
	}

	free(temp_array);

	return s->r_acc;
}
예제 #7
0
void List::insert(char* str, unsigned int index, bool redraw)
{
	if (index >= stringCount)
		return;

	if (index == stringCount - 1)
	{
		addToEnd(str, redraw);
		return;
	}

	for (unsigned int i = stringCount; i > index + 1; i--)
		memcpy(strings + (i - 1) * (stringLength + 1), strings + (i - 2) * (stringLength + 1), stringLength + 1);

	add(str, index, redraw);
}
예제 #8
0
Row *List::addByName(History *history) {
	if (_sortMode != SortMode::Name) return nullptr;

	Row *row = addToEnd(history), *change = row;
	const QString &peerName(history->peer->name);
	while (change->_prev && change->_prev->history()->peer->name.compare(peerName, Qt::CaseInsensitive) > 0) {
		change = change->_prev;
	}
	if (!insertBefore(row, change)) {
		while (change->_next != _end && change->_next->history()->peer->name.compare(peerName, Qt::CaseInsensitive) < 0) {
			change = change->_next;
		}
		insertAfter(row, change);
	}
	return row;
}
예제 #9
0
int main(){
  int i=0;
  char name[50];
  int roll_no;
  node *first=NULL;


  for(i=0;i<5;i++){
    printf("Enter Name:");
    readline(name,50);
    printf("Enter Roll No:");
    scanf("%d",&roll_no);
    
    //    first=addToBegining(first,name,roll_no);
    first=addToEnd(first,name,roll_no);
  }
  printList(first);
}
예제 #10
0
void List::add(char* str, unsigned int index, bool redraw)
{
	if (index >= stringCount)
		return;

	if (index == addCount)
	{
		addToEnd(str, redraw);
		return;
	}

	if (static_cast<unsigned int>(strlen(str)) > stringLength)
		str[stringLength] = 0;

	memset(strings + index * (stringLength + 1), ' ', stringLength);
	strings[(index + 1) * stringLength] = 0;
	memcpy(strings + index * (stringLength + 1), str, strlen(str));

	if (addCount != stringCount)
		addCount++;

	if (redraw)
		draw();
}
예제 #11
0
static int interpretAddr(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList, int p_numLinks)
{
    struct ifaddrmsg *l_info = (struct ifaddrmsg *)NLMSG_DATA(p_hdr);
    struct ifaddrs *l_interface = findInterface(l_info->ifa_index, p_resultList, p_numLinks);

    if(l_info->ifa_family == AF_PACKET)
    {
        return 0;
    }

    size_t l_nameSize = 0;
    size_t l_addrSize = 0;

    int l_addedNetmask = 0;

    size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
    struct rtattr *l_rta;
    for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
    {
        size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);

        switch(l_rta->rta_type)
        {
        case IFA_ADDRESS:
        case IFA_LOCAL:
            if((l_info->ifa_family == AF_INET || l_info->ifa_family == AF_INET6) && !l_addedNetmask)
            {   // make room for netmask
                l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
                l_addedNetmask = 1;
            }
        case IFA_BROADCAST:
            l_addrSize += NLMSG_ALIGN(calcAddrLen(l_info->ifa_family, l_rtaDataSize));
            break;
        case IFA_LABEL:
            l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
            break;
        default:
            break;
        }
    }

    struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize);
    if (l_entry == NULL)
    {
        return -1;
    }
    memset(l_entry, 0, sizeof(struct ifaddrs));
    l_entry->ifa_name = (l_interface ? l_interface->ifa_name : "");

    char *l_name = ((char *)l_entry) + sizeof(struct ifaddrs);
    char *l_addr = l_name + l_nameSize;

    l_entry->ifa_flags = l_info->ifa_flags;
    if(l_interface)
    {
        l_entry->ifa_flags |= l_interface->ifa_flags;
    }

    l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifaddrmsg));
    for(l_rta = IFA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
    {
        void *l_rtaData = RTA_DATA(l_rta);
        size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
        switch(l_rta->rta_type)
        {
        case IFA_ADDRESS:
        case IFA_BROADCAST:
        case IFA_LOCAL:
        {
            size_t l_addrLen = calcAddrLen(l_info->ifa_family, l_rtaDataSize);
            makeSockaddr(l_info->ifa_family, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
            if(l_info->ifa_family == AF_INET6)
            {
                if(IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)l_rtaData) || IN6_IS_ADDR_MC_LINKLOCAL((struct in6_addr *)l_rtaData))
                {
                    ((struct sockaddr_in6 *)l_addr)->sin6_scope_id = l_info->ifa_index;
                }
            }

            if(l_rta->rta_type == IFA_ADDRESS)
            {   // apparently in a point-to-point network IFA_ADDRESS contains the dest address and IFA_LOCAL contains the local address
                if(l_entry->ifa_addr)
                {
                    l_entry->ifa_dstaddr = (struct sockaddr *)l_addr;
                }
                else
                {
                    l_entry->ifa_addr = (struct sockaddr *)l_addr;
                }
            }
            else if(l_rta->rta_type == IFA_LOCAL)
            {
                if(l_entry->ifa_addr)
                {
                    l_entry->ifa_dstaddr = l_entry->ifa_addr;
                }
                l_entry->ifa_addr = (struct sockaddr *)l_addr;
            }
            else
            {
                l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
            }
            l_addr += NLMSG_ALIGN(l_addrLen);
            break;
        }
        case IFA_LABEL:
            strncpy(l_name, l_rtaData, l_rtaDataSize);
            l_name[l_rtaDataSize] = '\0';
            l_entry->ifa_name = l_name;
            break;
        default:
            break;
        }
    }

    if(l_entry->ifa_addr && (l_entry->ifa_addr->sa_family == AF_INET || l_entry->ifa_addr->sa_family == AF_INET6))
    {
        unsigned l_maxPrefix = (l_entry->ifa_addr->sa_family == AF_INET ? 32 : 128);
        unsigned l_prefix = (l_info->ifa_prefixlen > l_maxPrefix ? l_maxPrefix : l_info->ifa_prefixlen);
        char l_mask[16] = {0};
        unsigned i;
        for(i=0; i<(l_prefix/8); ++i)
        {
            l_mask[i] = 0xff;
        }
        if(l_prefix % 8)
        {
            l_mask[i] = 0xff << (8 - (l_prefix % 8));
        }

        makeSockaddr(l_entry->ifa_addr->sa_family, (struct sockaddr *)l_addr, l_mask, l_maxPrefix / 8);
        l_entry->ifa_netmask = (struct sockaddr *)l_addr;
    }

    addToEnd(p_resultList, l_entry);
    return 0;
}
예제 #12
0
static int interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_resultList)
{
    struct ifinfomsg *l_info = (struct ifinfomsg *)NLMSG_DATA(p_hdr);

    size_t l_nameSize = 0;
    size_t l_addrSize = 0;
    size_t l_dataSize = 0;

    size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
    struct rtattr *l_rta;
    for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
    {
        size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
        switch(l_rta->rta_type)
        {
        case IFLA_ADDRESS:
        case IFLA_BROADCAST:
            l_addrSize += NLMSG_ALIGN(calcAddrLen(AF_PACKET, l_rtaDataSize));
            break;
        case IFLA_IFNAME:
            l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
            break;
        case IFLA_STATS:
            l_dataSize += NLMSG_ALIGN(l_rtaSize);
            break;
        default:
            break;
        }
    }

    struct ifaddrs *l_entry = malloc(sizeof(struct ifaddrs) + sizeof(int) + l_nameSize + l_addrSize + l_dataSize);
    if (l_entry == NULL)
    {
        return -1;
    }
    memset(l_entry, 0, sizeof(struct ifaddrs));
    l_entry->ifa_name = "";

    char *l_index = ((char *)l_entry) + sizeof(struct ifaddrs);
    char *l_name = l_index + sizeof(int);
    char *l_addr = l_name + l_nameSize;
    char *l_data = l_addr + l_addrSize;

    // save the interface index so we can look it up when handling the addresses.
    memcpy(l_index, &l_info->ifi_index, sizeof(int));

    l_entry->ifa_flags = l_info->ifi_flags;

    l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
    for(l_rta = IFLA_RTA(l_info); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
    {
        void *l_rtaData = RTA_DATA(l_rta);
        size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
        switch(l_rta->rta_type)
        {
        case IFLA_ADDRESS:
        case IFLA_BROADCAST:
        {
            size_t l_addrLen = calcAddrLen(AF_PACKET, l_rtaDataSize);
            makeSockaddr(AF_PACKET, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
            ((struct sockaddr_ll *)l_addr)->sll_ifindex = l_info->ifi_index;
            ((struct sockaddr_ll *)l_addr)->sll_hatype = l_info->ifi_type;
            if(l_rta->rta_type == IFLA_ADDRESS)
            {
                l_entry->ifa_addr = (struct sockaddr *)l_addr;
            }
            else
            {
                l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
            }
            l_addr += NLMSG_ALIGN(l_addrLen);
            break;
        }
        case IFLA_IFNAME:
            strncpy(l_name, l_rtaData, l_rtaDataSize);
            l_name[l_rtaDataSize] = '\0';
            l_entry->ifa_name = l_name;
            break;
        case IFLA_STATS:
            memcpy(l_data, l_rtaData, l_rtaDataSize);
            l_entry->ifa_data = l_data;
            break;
        default:
            break;
        }
    }

    addToEnd(p_resultList, l_entry);
    return 0;
}
예제 #13
0
static void interpretLink(struct nlmsghdr *p_hdr, struct ifaddrs **p_links, struct ifaddrs **p_resultList)
{
    struct ifinfomsg *l_info = (struct ifinfomsg *)NLMSG_DATA(p_hdr);

    size_t l_nameSize = 0;
    size_t l_addrSize = 0;
    size_t l_dataSize = 0;
    
    size_t l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
    struct rtattr *l_rta;
    for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifinfomsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
    {
        void *l_rtaData = RTA_DATA(l_rta);
        size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
        switch(l_rta->rta_type)
        {
            case IFLA_ADDRESS:
            case IFLA_BROADCAST:
                l_addrSize += NLMSG_ALIGN(calcAddrLen(AF_PACKET, l_rtaDataSize));
                break;
            case IFLA_IFNAME:
                l_nameSize += NLMSG_ALIGN(l_rtaSize + 1);
                break;
            case IFLA_STATS:
                l_dataSize += NLMSG_ALIGN(l_rtaSize);
                break;
            default:
                break;
        }
    }
    
    struct ifaddrs *l_entry;
    l_entry = (struct ifaddrs *)malloc(sizeof(struct ifaddrs) + l_nameSize + l_addrSize + l_dataSize);
    memset(l_entry, 0, sizeof(struct ifaddrs));
    l_entry->ifa_name = "";
    
    char *l_name = ((char *)l_entry) + sizeof(struct ifaddrs);
    char *l_addr = l_name + l_nameSize;
    char *l_data = l_addr + l_addrSize;
    
    l_entry->ifa_flags = l_info->ifi_flags;
    
    l_rtaSize = NLMSG_PAYLOAD(p_hdr, sizeof(struct ifinfomsg));
    for(l_rta = (struct rtattr *)(((char *)l_info) + NLMSG_ALIGN(sizeof(struct ifinfomsg))); RTA_OK(l_rta, l_rtaSize); l_rta = RTA_NEXT(l_rta, l_rtaSize))
    {
        void *l_rtaData = RTA_DATA(l_rta);
        size_t l_rtaDataSize = RTA_PAYLOAD(l_rta);
        switch(l_rta->rta_type)
        {
            case IFLA_ADDRESS:
            case IFLA_BROADCAST:
            {
                size_t l_addrLen = calcAddrLen(AF_PACKET, l_rtaDataSize);
                makeSockaddr(AF_PACKET, (struct sockaddr *)l_addr, l_rtaData, l_rtaDataSize);
                ((struct sockaddr_ll *)l_addr)->sll_ifindex = l_info->ifi_index;
                ((struct sockaddr_ll *)l_addr)->sll_hatype = l_info->ifi_type;
                if(l_rta->rta_type == IFLA_ADDRESS)
                {
                    l_entry->ifa_addr = (struct sockaddr *)l_addr;
                }
                else
                {
                    l_entry->ifa_broadaddr = (struct sockaddr *)l_addr;
                }
                l_addr += NLMSG_ALIGN(l_addrLen);
                break;
            }
            case IFLA_IFNAME:
                strncpy(l_name, (char *)l_rtaData, l_rtaDataSize);
                l_name[l_rtaDataSize] = '\0';
                l_entry->ifa_name = l_name;
                break;
            case IFLA_STATS:
                memcpy(l_data, l_rtaData, l_rtaDataSize);
                l_entry->ifa_data = l_data;
                break;
            default:
                break;
        }
    }
    
    addToEnd(p_resultList, l_entry);
    p_links[l_info->ifi_index - 1] = l_entry;
}
예제 #14
0
// Add Rectangle to scene
void Scene::addToScene(Rectangle* rect) {
	addToEnd(llShapes, rect);
}
예제 #15
0
void moveToEnd(CacheList *list, CacheNode* node){
    removeNode(list, node);
    addToEnd(list, node);
}
// main crawler function
int main(int argc, char* argv[]) {

    // local variables
    FILE *fp; // file pointer for html files
    char *nextURL; // pointer to the next URL found on the seed page
    char *newURL; // pointer to the next URL in the while loop

    // check command line arguments
    if (argc != 4) {
        printf("Incorrect number of arguments provided.");
        exit(1);
    }
    // check that the second argument is a directory
    stat(argv[2],&statbuffer);
    if S_ISDIR(statbuffer.st_mode) { }
    else {
        printf("Error, you did not supply a valid directory");
        exit(1);
    }

    // get arguments
    char *seedURL = argv[1];
    int filename_len = strlen(argv[2])+21;

    // get the directory
    char*filename = calloc(filename_len,sizeof(char));

    // check the maxDepth
    int value = is_numeric(argv[3]);
    if (value != 0) {
        sscanf(argv[3],"%i",&maxDepth);
    }
    else {
        printf("Error! maxDepth must be a number");
        exit(1);
    }

    // init curl
    curl_global_init(CURL_GLOBAL_ALL);

    // initialize data structures/variables

    // initialize hashtable
    HashTable *table = malloc(sizeof(HashTable));
    memset(table,0,MAX_HASH_SLOT);

    // initialize linked list
    List *WebPageList;
    WebPageList = createList();

    // setup seed page

    // get seed webpage
    // if it fails, report and exit
    if (NormalizeURL(seedURL) == 0) {
        printf("Error, bad URL");
        exit(1);
    }
    // write seed file

    // create WebPage object by allocating memory
    WebPage *seedPage = malloc(sizeof(WebPage));

    // assign values to each part of the struct
    seedPage->url = seedURL;
    seedPage->html = NULL;
    seedPage->html_len = 0;
    seedPage->depth = 0;

    // try to get the webpage up to MAX_TRY times
    if (!GetWebPage(seedPage)) {
        for (tries = 0; tries < MAX_TRY; tries++) {
            if (GetWebPage(seedPage)) {
                break;
            }
        }
    }

    // write html contents to a file "1" in the given directory
    sprintf(filename,"%s/%d",argv[2],1);
    fp = fopen(filename,"w");
    fputs(seedURL,fp);
    fputs("\n",fp);
    fprintf(fp,"%d\n",seedPage->depth);
    fputs(seedPage->html,fp);

    // close the file and wipe the filename
    fclose(fp);
    memset(filename,'\0',filename_len);

    // add seed page to hashtable
    add(table,seedURL);

    // extract urls from seed page

    // while there are still URLs in the seed page's html
    while ((pos = GetNextURL(seedPage->html,pos,seedPage->url,&nextURL)) > 0) {

        // only visiting them if it wouldn't exceed maxDepth
        if ((seedPage->depth+1) > maxDepth) {
            free(seedPage);
            exit(1);
        }

        // ensure it's a valid url
        if (NormalizeURL(nextURL) != 0) {

            // also check if its in the right domain
            if (strncmp(URL_PREFIX,nextURL,strlen(URL_PREFIX)) == 0) {

                // if it is added to the hashtable it is a unique URL that
                // hasn't been visited before, add it to the linked list
                // of URLs to visit
                if (add(table,nextURL)) {
                    // create a new webpage object
                    WebPage *pages = malloc(sizeof(WebPage));
                    pages->url = nextURL;
                    pages->html = NULL;
                    pages->html_len = 0;
                    pages->depth = 1;

                    // try to get the webpage up until the MAX_TRY
                    tries = 0;
                    if (!GetWebPage(pages)) {
                        for (tries = 0; tries < MAX_TRY; tries++) {
                            if (GetWebPage(pages)) {
                                break;
                            }
                        }
                    }

                    // add it to the linked list
                    addToEnd(WebPageList,pages);
                }
            }
        }
    }

    // while there are urls to crawl
    while (WebPageList->head != NULL) {
        // get next url from list
        WebPage *nextPage = malloc(sizeof(WebPage));
        nextPage = removeFromFront(WebPageList);

        // try to get the webpage up until the MAX_TRY
        tries = 0;
        if (!GetWebPage(nextPage)) {
            for (tries = 0; tries < MAX_TRY; tries++) {
                if (GetWebPage(nextPage)) {
                    break;
                }
            }
        }

        // write page file
        sprintf(filename,"%s/%d",argv[2],docNum);
        fp = fopen(filename,"w");
        fputs(nextPage->url,fp);
        fputs("\n",fp);
        fprintf(fp,"%d\n",nextPage->depth);
        fputs(nextPage->html,fp);

        // close the file and wipe the filename (to be used next time)
        fclose(fp);
        memset(filename,'\0',filename_len);

        // increment the doc num
        docNum++;

        // check if visiting the URLs on this page will exceed maxDepth
        if ((nextPage->depth+1) > maxDepth) {
            free(nextPage);
            continue;
        }
        pos = 0;
        // iterate through all the URLs on the page
        while ((pos = GetNextURL(nextPage->html,pos,nextPage->url,&newURL))>0) {
            // check to ensure that the URLs are the proper format
            if (NormalizeURL(newURL) != 0 ) {
                // check to ensure that they are in the right domain
                if (strncmp(URL_PREFIX,newURL,strlen(URL_PREFIX)) == 0) {
                    // making sure to only add new ones to the list
                    if (add(table,newURL) != 0) {
                        // create a new WebPage object
                        WebPage *page = malloc(sizeof(WebPage));
                        page->url = newURL;
                        page->html = NULL;
                        page->html_len = 0;
                        page->depth = nextPage->depth + 1;
                        GetWebPage(page);

                        // try to get the webpage up until the MAX_TRY
                        tries = 0;
                        if (!GetWebPage(page)) {
                            for (tries = 0; tries < MAX_TRY; tries++) {
                                if (GetWebPage(page)) {
                                    break;
                                }
                            }
                        }

                        // add the page to the linked list
                        addToEnd(WebPageList,page);
                    }
                }
            }
        }
        // Sleep for a bit to avoid annoying the target
        sleep(INTERVAL_PER_FETCH);

        // Free resources
        free(nextPage);

    }

    // cleanup curl
    curl_global_cleanup();

    // free resources
    // free hashtable
    hash = JenkinsHash(seedURL,MAX_HASH_SLOT);
    HashTableNode *freer = table->table[hash];
    HashTableNode *tempHash = NULL;
    while (freer != NULL) {
        tempHash = freer;
        freer = freer->next;
        free(tempHash);
    }
    free(table);

    // free linked list
    free(WebPageList);

    // free WebPage and filename pointer
    free(seedPage);
    free(filename);
    return 0;
}
예제 #17
0
main(){
	int d,sear;
	struct node *head;
	struct node *second;
	head= (struct node *) malloc( sizeof(struct node) );
	second= (struct node *) malloc( sizeof(struct node) );
	head->data = 5; 
	head->next = second;	
	second->data=6;
	second->next = NULL;

	do
	{
	
		printf("\nMENU\n");
		printf("1. Add a new node to the start of the list\n");
		printf("2. Add a new node to the end of the list \n");
		printf("3. Display all of the nodes in the list \n");
		printf("4. Display the length of the list \n");
		printf("5. Search the linked list for a data value\n");
		printf("6. delete a node to the start of the list\n");
		printf("7. delete a node to the end of the list \n");
		printf("8. sort the list \n");
		printf("9. exit\n");
		printf("enter the option you want to choose");
		scanf("%d",&d);
	
		switch(d){
		case 1:
			addToStart(&head);
			break;
		case 2:
			addToEnd(head);
			break;
		case 3:
			DisplayAllNodes(head);
			break;
		case 4:
			DisplayLengthAllNodes(head);
			break;
		case 5:
			printf("enter the value you want to search\n");
			scanf("%d",&sear);
			searchList(head, sear);
			break;
		case 6:
			deleteFromStart(&head);
			break;
		case 7:
			deleteFromEnd(head);
			break;
		case 8:
			bubbleSort(head);
			break;
		case 9:
			exit(0);
			break;
		default:
			break;
		}
	system("cls");
	}while(d!=9);
	system("pause");
}