/* main encoder */ void Encode (void) { unsigned int dictpos, deleteflag, sectorlen; unsigned long bytescompressed; InitEncode(); dictpos = deleteflag = 0; bytescompressed = 0; while (1) { /* delete old data from dictionary */ if (deleteflag) DeleteData(dictpos); /* grab more data to compress */ if ((sectorlen = LoadDict(dictpos)) == 0) break; /* hash the data */ HashData(dictpos, sectorlen); /* find dictionary matches */ DictSearch(dictpos, sectorlen); bytescompressed += sectorlen; printf("\r%ld", bytescompressed); dictpos += SECTORLEN; /* wrap back to beginning of dictionary when its full */ if (dictpos == DICTSIZE) { dictpos = 0; deleteflag = 1; /* ok to delete now */ } } /* Send EOF flag */ SendMatch(MAXMATCH + 1, 0); /* Flush bit buffer */ if (bitsin) SendBits(0, 8 - bitsin); return; }
int msg_in_history(char* id, int msg ) { int to_ret = 0; char *history = DictSearch( msg_history, id ); char* old_msg; char id_s[20]; sprintf(id_s,"%d",id); while( (old_msg = strsep(&history,",") != NULL && old_msg != '\0')) { if(strcmp(id_s,old_msg) == 0) { to_ret = 1; break; } } return to_ret; }
void get_next(char* client, int socket) { int last_msg = DictSearch( clients, client ); int new_msg = last_msg + 1; char to_ret[MSG_SIZE]; printf(" Client: %s Last_Msg: %d New_Msg: %d \n", client, last_msg, new_msg); if( msg_in_history(client, last_msg) == 0 ) { if( new_msg < num_msg) { sprintf(to_ret,"%s", DictSearchS(messages, new_msg)); DictDelete(clients, client); DictInsert(clients, client, new_msg); } else { sprintf(to_ret,"%s","No new messages "); } } write(socket, to_ret, sizeof(to_ret)); }//end get_next
//чтение данных, присланных пользователем int handlerRequest(int epollfd, int connectionfd, struct UserParams userParams, pthread_mutex_t mutex){ int buffCount = 1024; int i = 3; char *buffer = (char*) malloc(buffCount * sizeof(char)); char conBuf[4]; ssize_t count = read(connectionfd, buffer, sizeof buffer); char *ret; //динамическое выделение памяти для буфера char bufferN[buffCount]; ssize_t countN = read(connectionfd, bufferN, sizeof bufferN); while(countN > 0){ ret = (char*)realloc(buffer,i*buffCount*sizeof(char)); if(!ret){ perror("realloc"); exit(1); } strcat(buffer, bufferN); count+=countN; i++; countN = read(connectionfd, bufferN, sizeof bufferN); } switch(count){ case -1: if (errno != EAGAIN) perror ("failed to read data"); break; case 0: printf("client closed the connection"); break; default: snprintf(conBuf, 4,"%d",connectionfd); //ввод логина if(!strcmp(DictSearch(userParams.login, conBuf),"")){ if (count > 1){ char *tmpLogin = (char*) malloc(count+1); strncpy(tmpLogin, buffer, count); tmpLogin[count] = '\0'; DictInsert(userParams.login,conBuf,tmpLogin); printf("%s\n", DictSearch(userParams.login, conBuf)); dprintf(connectionfd, "Enter password: "******"Enter login: "******"0")){ char *tmpPass=(char*) malloc(count+1); strncpy(tmpPass, buffer, count); tmpPass[count] = '\0'; //вызов проверки pthread_mutex_lock(&mutex); int check = checkLogin(DictSearch(userParams.login, conBuf), tmpPass); pthread_mutex_unlock(&mutex); if(check == 1){ DictDelete(userParams.fLogin,conBuf); DictInsert(userParams.fLogin,conBuf,"1"); DictInsert(userParams.location,conBuf,"cd user_root/;"); dprintf(connectionfd, "!!Enter command: "); } else{ DictDelete(userParams.login,conBuf); DictInsert(userParams.login,conBuf,""); dprintf(connectionfd,"Enter login: "******"logout",6)) close(connectionfd); else{ int c; FILE *pp; extern FILE *popen(); char* tempCd = (char*) malloc(1024*sizeof(char)); strcpy(tempCd, DictSearch(userParams.location, conBuf)); //strcat(tempCd, ";"); printf("----->%s\n",tempCd); if ( !(pp=popen(strcat(tempCd, buffer), "r")) ){ free(tempCd); return 1; } while ( (c=fgetc(pp)) != EOF ) { putc(c, stdout); dprintf(connectionfd,"%c",c); fflush(pp); } pclose(pp); free(tempCd); printf("%d user command: %.*s", connectionfd, count, buffer); dprintf(connectionfd, "Enter command: "); } } } } free(buffer); }
int search(struct position source, struct position target, int (*blocked)(struct position)) { struct position current; current = source; Dict d; d = DictCreate(); PQ* q; q = initQueue(); heapNode hn; hn.distFromSource = 0; hn.value = abs(current.x - target.x) + abs(current.y - target.y); hn.pos = current; enqueue(hn,q); DictInsert(d,current.x,current.y,hn.distFromSource); while(q->size) //stops when priority queue is empty { int i; hn = dequeue(q); current = hn.pos; if(foundTarget(current,target)) //hooray! target found!! { DictDestroy(d); destroyQueue(q); return hn.distFromSource; } for(i = 0; i < 4; i++) //for loop explores all the four neighbors defined as 0...3 { struct position neighbor = createNeighbor(current,i); int dictSearchDist = DictSearch(d,neighbor.x,neighbor.y); if(!blocked(neighbor) && dictSearchDist < 0) //add the neighbor to PQ { DictInsert(d,neighbor.x,neighbor.y,hn.distFromSource + 1); heapNode node; int distToTarget = abs(neighbor.x - target.x) + abs(neighbor.y - target.y); //manhattan distance node.value = (hn.distFromSource + 1) + distToTarget; node.pos = neighbor; node.distFromSource = hn.distFromSource + 1; enqueue(node,q); } else if(dictSearchDist >= 0) { if(dictSearchDist > hn.distFromSource + 1) { DictInsert(d,neighbor.x,neighbor.y,hn.distFromSource + 1); heapNode node; int distToTarget = abs(neighbor.x - target.x) + abs(neighbor.y - target.y); //manhattan distance node.value = (hn.distFromSource + 1) + distToTarget; node.pos = neighbor; node.distFromSource = hn.distFromSource + 1; enqueue(node,q); } } } } DictDestroy(d); destroyQueue(q); return NO_PATH; }