Пример #1
0
Файл: cache.c Проект: nikmash/C
int main(int argc, char *argv[]){
	if(argc == 1){
		printf("Please enter a index file \n");
		printf("Usage: <name of compiled program> <option> <Memory Size> <Index File> \n");
	}
	
	if(argc == 4){
		
		FILE *fp;
				
		/***** CONVERT INPUTTED STRING TO MEMORY SIZE *******/
		data *dataptr = (data *) malloc(sizeof(struct data_));

		char *rawmemory = argv[2];
		int num = 0;
		char *memory = rawmemory;
		int multiplier;

		while(isdigit(*rawmemory)){
			num++;
			rawmemory++;
		}

		if(*rawmemory == 'K'){
			multiplier = 1024;
		}

		if(*rawmemory == 'M'){
			multiplier = 1024 * 1024;
		}

		if(*rawmemory == 'G'){
			multiplier = 1024 * 1024 * 1024;
		}

		*rawmemory = '\0';
		dataptr->maxmemory = atoi(memory) * multiplier;
		dataptr->currmemory = 0;


		/**************************************************/

		char line[1000];
		TokenizerT *tokenizer;
		char *token;

		word *newword;
		int memusage = 0;

		dataptr->file = argv[3];

		fp = fopen(argv[3], "r");
		if(fp == NULL){
			fprintf(stderr, "file could not be opened \n");
		}

		while(fgets(line, 1000, fp) != NULL){
			tokenizer = TKCreate(line);
			token = TKGetNextToken(tokenizer);

			while (token != NULL){
				checktoken(token);
				

				if(start == 1){
					if (!(counter % 2)){
						/* THIS IS WHERE I GET THE FILENAME STRING OF EACH WORD*/
						LLInsert(newword->filelist, token);
						memusage = memusage + strlen(token);
								
					}

					counter++;
				}


				/*fprintf(fp2, "%s \n", token);*/
						
				token = TKGetNextToken(tokenizer);
						
				if(var == 1){
					newword = (word *)malloc(sizeof(struct words));
					newword->string = token;
					newword->filelist = LLCreate();
					memusage = memusage + strlen(token);

					var = 0;
					start = 1;
					counter++;
				}
				if(var2 == 1){
					/* AFTER I HAVE ADDED ALL THE FILENAME INDEXES TO THE WORD THEN I INSERT IT INTO listwords array and print it out onto the console */
					if(memusage + dataptr->currmemory > dataptr->maxmemory){
						free(newword);
						break;
					}
					else{
						listwords[lwsize] = newword;
						lwsize++;
						var2 = 0;
						dataptr->currmemory = dataptr->currmemory + memusage;
						newword->memory = memusage;
						memusage = 0;
					}
					
				}

			}

			if(memusage + dataptr->currmemory > dataptr->maxmemory)
				break;
		}

		var = 0;
		var2 = 0;
		start = 0;

		TKDestroy(tokenizer);
		fclose(fp);


		char str[100];
		int x;

		
		for(; ;){
			linkedListPtr searchlist = LLCreate();
			linkedListPtr foundlist = LLCreate();

			/* LOOPING USER INTERFACE */
			printf("search> ");
			fgets(str, 100, stdin);

			  

			x = strlen(str)-1;
			if(str[x] == '\n') 
			    str[x] = '\0';

			token = strtok(str, " ");
			
			LLInsert(searchlist, token);

			
			while ((token = (strtok(NULL, " "))) != NULL){
				LLInsert(searchlist, token);
				searchlist->size++;
			}
			
		
			if (strcmp(searchlist->head->filename, "q") == 0){
			  	break;
			}			

			if(strcmp(searchlist->head->filename, "so") == 0){
				searchlist->head = searchlist->head->next;
				searchor(foundlist, searchlist, dataptr);
			}
			else if(strcmp(searchlist->head->filename, "sa") == 0){
				searchlist->head = searchlist->head->next;
				searchand(foundlist, searchlist, dataptr);
			}
			LLDestroy(searchlist);
			LLDestroy(foundlist);

		}

		int i;

		for (i = 0; i < lwsize; i++){
			LLDestroy(listwords[i]->filelist);
			free(listwords[i]->string);
			free(listwords[i]);
		}


	}
	
				
}
Пример #2
0
Queue QCreate(){
  return LLCreate();
}
Пример #3
0
Файл: cache.c Проект: nikmash/C
int LRU(char *wordc, data *dataptr, linkedListPtr searchlist){
	FILE *fp;
	fp = fopen(dataptr->file, "r");


	char *token;
	TokenizerT *tokenizer;
	char line[1000];

	int memusage = 0;
	int counter = 0;

	word *newword;

	int on = 0;


	while(fgets(line, 1000, fp) != NULL){

			tokenizer = TKCreate(line);
			token = TKGetNextToken(tokenizer);


			while (token != NULL){
				
				if(on == 1){
					if(strcmp(token, "</list>") == 0){
						on = 2;
						break;
					}

					else if(counter % 2 == 0){
						LLInsert(newword->filelist, token);
						memusage = memusage + strlen(token);
					}
					counter++;
				}



				if(strcmp(token, wordc) == 0){
					

					newword = (word *)malloc(sizeof(struct words));
					newword->counter = 1;
					newword->string = token;
					newword->filelist = LLCreate();
					counter = 2;
					memusage = memusage + strlen(token);
					on = 1;


				}

				token = TKGetNextToken(tokenizer);		
			}
			if(on == 2){
				break;
			}
			
	}


	if(counter == 0){
		printf("WORD %s DOES NOT EXIST \n", wordc);
		return -1;
	}

	newword->memory = memusage;

	llnode *currsearch = searchlist->head;
	llnode *prevsearch = NULL;


	//int count = 0;
	int lowest = INT_MAX;
	int lowestindex;
	int i;
	int retval;

	on = 0;


	fclose(fp);

	if(dataptr->maxmemory-dataptr->currmemory > memusage){
		listwords[lwsize] = newword;
		dataptr->currmemory = dataptr->currmemory + memusage;
		lwsize++;
	}

	else{
		while(dataptr->maxmemory-dataptr->currmemory < memusage){
			for(i = 0; i<lwsize; i++){
				while(currsearch != NULL){

					if(strcmp(currsearch->filename, listwords[i]->string)==0){
						on = 1;
						break;
					}
						

					prevsearch = currsearch;
					currsearch = currsearch->next;

				}


				if(on == 0){

					if(listwords[i]->counter < lowest){
						lowest = listwords[i]->counter;
						lowestindex = i;
					}

				}
			}


			dataptr->currmemory = dataptr->currmemory - listwords[lowestindex]->memory;
			free(listwords[lowestindex]);


			if(dataptr->maxmemory-dataptr->currmemory > memusage){
				listwords[lowestindex] = newword;
				dataptr->currmemory = dataptr->currmemory + memusage;
				retval = lowestindex;
				break;
			}

			else{
				if(lowestindex != lwsize-1){
					for(i = lowestindex; i<lwsize-1; i++){
						listwords[lowestindex] = listwords[lowestindex + 1];
						listwords[lowestindex+1] = NULL;
					}
					lwsize--;
				}
				
			}

		/*	if(count == 0){
				printf("WHAT IS THIS %d \n", lowestindex);
				printf("whats in 0 %s \n", listwords[lowestindex]->string);

				dataptr->currmemory = dataptr->currmemory - listwords[lowestindex]->memory;

				free(listwords[lowestindex]);
				printf("HERE1 \n");

				listwords[lowestindex] = newword;
				dataptr->currmemory = dataptr->currmemory + memusage;
				retval = lowestindex;
				count++;

			}
			else{
				printf("WHAT IS THIS 2 %d \n", lowestindex);

				dataptr->currmemory = dataptr->currmemory - listwords[lowestindex]->memory;

				free(listwords[lowestindex]);
				if(lowestindex != lwsize-1){
					for(i = lowestindex; i<lwsize; i++){
						listwords[lowestindex] = listwords[lowestindex + 1];
						listwords[lowestindex + 1] = NULL;
					}
					lwsize--;
				}
			}

			*/
		}
	}

	

	return retval;
}
Пример #4
0
int main(int argc, char **argv){
 
  List list = LLCreate();
  Stack stack = SCreate();
  Queue queue = QCreate();
  User *usr;

  setupUsers();
  MENU_CHOICE m_choice = MAIN_MENU;
  int choice, count;
  menu();

  choice = getChoice();
  
  while(choice){
    switch(m_choice){
    case MAIN_MENU:
      switch(choice){
      case 1:
	m_choice = LL_MENU; 
	llmenu();
	break;
      case 2:
	m_choice = QUEUE_MENU;
	qmenu();
	break;
      case 3:
	m_choice = STACK_MENU;
	smenu();
	break;
      case 4:
	showUsers();
        menu();
	break;
      case 0:
	exit(0);
	break;
      default:
	m_choice = MAIN_MENU;
	menu();
      }
      break;
    case LL_MENU:
      switch(choice){
      case 1:
	printf("\n%s\n", "adding head, choose a user");
        usr = chooseUser();
        printf("%s %s\n", "You chose ", usr -> name);
        LLAddHead(usr, list);
        llmenu();
	break;
      case 2:
	printf("\n%s\n", "adding tail, choose a user");
        usr = chooseUser();
        printf("%s %s\n", "You chose", usr -> name);
        LLAddTail(usr, list);
        llmenu();
	break;
      case 3:
	printf("\n%s\n", "removing head");
        LLRemoveHead(list);
        llmenu();
	break;
      case 4:
	printf("\n%s\n", "removing tail");
        LLRemoveTail(list);
        llmenu();
	break;
      case 5:
        print(list);
        llmenu();
	break;
      case 6:
        m_choice = MAIN_MENU;
	menu();
	break;
      case 0:
	exit(0);
	break;
      default:
	m_choice = LL_MENU;
      }
      break;
    case QUEUE_MENU:
      switch(choice){
      case 1:
        printf("\n%s\n", "enqueueing, choose a user");
        usr = chooseUser();
        printf("%s %s\n", "You chose ", usr -> name);
        Qenqueue(usr, queue);
        qmenu();
	break;
      case 2:
	printf("\n%s\n", "dequeueing");
        Qdequeue(queue);
        qmenu();
	break;
      case 3:
	print(queue);
        qmenu();
	break;
      case 4:
        m_choice = MAIN_MENU;
	menu();
	break;
      case 0:
	exit(0);
	break;
      default:
	m_choice = QUEUE_MENU;
      }
      break;
    case STACK_MENU:
      switch(choice){
      case 1:
	printf("%s\n", "push");
        smenu();
	break;
      case 2:
	printf("%s\n", "pop");
        smenu();
	break;
      case 3:
	printf("%s\n", "top");
        smenu();
	break;
      case 4:
	printf("%s\n", "print");
        smenu();
	break;
      case 5:
        m_choice = MAIN_MENU;
	menu();
	break;
      case 0:
	exit(0);
	break;
      default:
	m_choice = STACK_MENU;
      }
      break;
    } 
    choice = getChoice();
  }

  return 0;   
}