📄 utils.cc
字号:
int slisten(char *servicename){ struct sockaddr_in inaddr; int s, i=1; int protonum; if ((protonum = protonumber ("tcp")) < 0) return -1; if ((s = socket (PF_INET, SOCK_STREAM, protonum)) < 0) return -1; /* Put the REUSEADDR and KEEPALIVE sockopts in */ i=1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *) &i, sizeof(int)) != 0) { return -1; } i=0; if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (const char *) &i, sizeof(int)) != 0) { return -1; } if (make_inetaddr ((char *) 0, servicename, &inaddr) < 0) return -1; if (bind (s, (struct sockaddr *) &inaddr, sizeof (inaddr)) < 0) return -1; if (listen (s, 4) < 0) return -1; return s;}int sportnum(int s){ struct sockaddr sockname; int len; len = sizeof (sockname); if (getsockname (s, &sockname, &len) < 0) return -1; if (sockname.sa_family != AF_INET) return -1; return ((struct sockaddr_in *) (&sockname))->sin_port;}int make_inetaddr(char *hostname, char *servicename, struct sockaddr_in *inaddr){ struct hostent *host; struct servent *service; inaddr->sin_family = AF_INET; if (hostname == 0) inaddr->sin_addr.s_addr = 0; else if (isdigit (*hostname)) inaddr->sin_addr.s_addr = inet_addr(hostname); else { if ((host = gethostbyname(hostname)) == 0) { /*fprintf(stderr, "Unknown host in make_inetaddr..\n");*/ return -1; } if (host->h_addrtype != AF_INET) { fprintf(stderr, "Unknown af in make_inetaddr\n"); return -1; } memcpy(&inaddr->sin_addr.s_addr, host->h_addr, host->h_length); } if (servicename == 0) inaddr->sin_port = 0; else if (isdigit (*servicename)) inaddr->sin_port = htons(atoi (servicename)); else { if ((service = getservbyname (servicename, "tcp")) == 0) { fprintf(stderr, "Unknown service in make_inetaddr.\n"); return -1; } inaddr->sin_port = service->s_port; } return 0;}int protonumber(char *protoname){ struct protoent *proto; if ((proto = getprotobyname (protoname)) == 0) return -1; return proto->p_proto;}int test_readable(int s){ fd_set fdset; static struct timeval timeout = {0, 0}; /* For a poll */ int val; FD_ZERO(&fdset); FD_SET(s, &fdset); val = select(FD_SETSIZE, (fd_set *) &fdset, NULL, NULL, &timeout); if (val > 0) return 1; return val;}int test_writable(int s){ fd_set fdset; static struct timeval timeout = {0, 0}; /* For a poll */ int val; FD_ZERO(&fdset); FD_SET(s, &fdset); val = select(FD_SETSIZE, NULL, (fd_set *) &fdset, NULL, &timeout); if (val > 0) return 1; return val;}/********* Linked-list utilities ***********/#define LL_HEAPSIZE 50static ll heap_o_ll = NULL; /* Heap of free nodes for alloc and dealloc *//* Some private functions */ll Allocate_ll(void);void Free_ll(ll freeMe);int ll_add_to_end(ll *addToMe, void *data){ ll temp; temp = *addToMe; if (temp == NULL) { *addToMe = temp = Allocate_ll(); temp->data = data; temp->prev = temp->next = NULL; return 1; } while (temp->next != NULL) temp = temp->next; temp->next = Allocate_ll(); (temp->next)->prev = temp; (temp->next)->next = NULL; (temp->next)->data = data; return 1;}int ll_add_to_start(ll *addToMe, void *data){ ll temp; temp = *addToMe; if (temp == NULL) { *addToMe = temp = Allocate_ll(); temp->data = data; temp->prev = temp->next = NULL; return 1; } temp->prev = Allocate_ll(); (temp->prev)->next = temp; (temp->prev)->prev = NULL; (temp->prev)->data = data; *addToMe = temp->prev; return 1;}ll ll_find(ll *findInMe, void *data, int (*compare)(void *d1, void *d2)){ ll temp = *findInMe; while (temp != NULL) { if (compare(data, temp->data) == 0) /* ie the same */ return temp; temp = temp->next; } return temp;}int ll_sorted_insert(ll *insertme, void *data, int (*compare)(void *d1, void *d2)){ ll temp = *insertme, addEl; while (temp != NULL) { if (compare(data, temp->data) <= 0) {/* ie. the same or smaller */ /* insert before temp */ addEl = Allocate_ll(); addEl->data = data; addEl->prev = temp->prev; addEl->next = temp; temp->prev = addEl; if (addEl->prev == NULL) *insertme = addEl; else (addEl->prev)->next = addEl; return 1; } temp = temp->next; } /* We hit the end, so add to the end */ return ll_add_to_end(insertme, data);}int ll_del(ll *delFromMe, void *data, int (*compare)(void *d1, void *d2), void (*nukeElement)(void *nukeMe)){ ll temp = *delFromMe; while (temp != NULL) { if (compare(data, temp->data) == 0) /* ie the same */ { if (temp->prev != NULL) (temp->prev)->next = temp->next; if (temp->next != NULL) (temp->next)->prev = temp->prev; if (temp == *delFromMe) *delFromMe = temp->next; if (nukeElement != NULL) nukeElement(temp->data); Free_ll(temp); return 1; } temp = temp->next; } return 0;}int ll_delfirst(ll *delFromMe, void (*nukeElement)(void *nukeMe)){ ll temp = *delFromMe; if (temp == NULL) return 0; if (temp->prev != NULL) (temp->prev)->next = temp->next; if (temp->next != NULL) (temp->next)->prev = temp->prev; if (temp == *delFromMe) *delFromMe = temp->next; if (nukeElement != NULL) nukeElement(temp->data); Free_ll(temp); return 1;}int ll_destroy(ll *destroyMe, void (*nukeElement)(void *nukeMe)){ ll temp = *destroyMe, next; while (temp != NULL) { next = temp->next; if (nukeElement != NULL) nukeElement(temp->data); Free_ll(temp); temp = next; } *destroyMe = NULL; return 1;}ll Allocate_ll(void){ int counter; ll temp_pointer; /* if heap is null, am out of ll elements and must allocate more */ if (heap_o_ll == NULL) { heap_o_ll = (ll_node *) malloc(LL_HEAPSIZE * sizeof(ll_node)); if (heap_o_ll == NULL) { fprintf(stderr, "Out of memory in Allocate_ll!\n"); exit(1); } for (counter=0; counter < LL_HEAPSIZE - 1; counter++) { (heap_o_ll + counter)->data = NULL; (heap_o_ll + counter)->prev = NULL; (heap_o_ll + counter)->next = (heap_o_ll + counter + 1); } (heap_o_ll + LL_HEAPSIZE - 1)->next = NULL; } /* Have a workable heap. Splice off top and return pointer. */ temp_pointer = heap_o_ll; heap_o_ll = heap_o_ll->next; temp_pointer->next = NULL; return temp_pointer;}void Free_ll(ll freeMe){ freeMe->next = heap_o_ll; heap_o_ll = freeMe;}int ll_build(ll *buildMe, char *buildFromMe){ /* Takes string of form [a, b, c, d, e] and builds linked list with elements from the string. Malloc's space for new linked list element strings. */ char *end, *start, *data, *temp; data = (char *) malloc(sizeof(char) * (strlen(buildFromMe)+1)); if (data == NULL) { fprintf(stderr, "Out of memory in ll_build!\n"); return 0; } strcpy(data, buildFromMe); start = end = data + 1; while ((*end != ']') && (*end != '\0')) { while (*start == ' ') { start++; end++; } if (*end == ',') { *end = '\0'; temp = (char *) (malloc(sizeof(char) * (strlen(start)+1))); if (temp == NULL) { fprintf(stderr, "Out of memory in ll_build!\n"); return 0; } strcpy(temp, start); ll_add_to_end(buildMe, (void *) temp); start = end + 1; } end++; } *end = '\0'; temp = (char *) (malloc(sizeof(char) * (strlen(start)+1))); if (temp == NULL) { fprintf(stderr, "Out of memory in ll_build!\n"); return 0; } strcpy(temp, start); ll_add_to_end(buildMe, (void *) temp); free(data); return 1;}/****************** Hash table routines *****************/int chained_hash_create(int num_slots, hash_table *rt){ hash_table retval; hash_el *sArray; int i; /* Creating a chained hash table is as simple as allocating room for all of the slots. */ sArray = retval.slot_array = (hash_el *) malloc(num_slots*sizeof(hash_el)); if (retval.slot_array == NULL) return -1; retval.num_elements = num_slots; *rt = retval; /* Initialize all slots to have a null entry */ for (i=0; i<num_slots; i++) sArray[i] = NULL; return 0;}int chained_hash_destroy(hash_table ht, deletor d){ int i, numEl; hash_el *sArray; /* Destroying a hash table first implies destroying all of the lists in the table, then freeing the table itself. */ for (i=0, numEl=ht.num_elements, sArray=ht.slot_array; i<numEl; i++) { if (sArray[i] != NULL) { /* We must destroy the list in this slot */ ll_destroy(&(sArray[i]), d);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -