Example #1
0
void findWords(SymbolTable *table) {
	char *request, *rest, *token;
	char *ptr;
	Jval respond;

	if((request = myMalloc(sizeof(char), TEMPSIZE)) == NULL) {
		return;
	}

	ptr = request;
	printf("Please enter the string: \n");
	fgets(request, SIZE, stdin);
	request[strlen(request) - 1] = '\0';

	while((token = strtok_r(ptr, " ", &rest)) != NULL) {
		ptr = rest;
		respond = getEntry(new_jval_s(token), table);
		if(jval_s(respond) == jval_s(JNULL)) {
			printf("The word \"%s\" is not in the dictionary\n", token);
			continue;
		}
		printf("The word \"%s\" is in the dictionary\n", token);
	}

	free(request);
}
Example #2
0
main(int argc, char **argv)
{
  IS is;
  int n;
  Dllist l;
  Dllist tmp;

  if (argc != 2) {
    fprintf(stderr, "usage: dlltail n\n");
    exit(1);
  }
  n = atoi(argv[1]);
  if (n < 0) {
    fprintf(stderr, "usage: dlltail n  -- n must be >= 0\n");
    exit(1);
  }

  is = new_inputstruct(NULL);
  l = new_dllist();

  while (get_line(is) >= 0) {
    dll_append(l, new_jval_s(strdup(is->text1)));
    if (is->line > n) {
      tmp = dll_first(l);
      free(jval_s(dll_val(tmp)));
      dll_delete_node(tmp);
    }
  }

  dll_traverse(tmp, l) printf("%s", jval_s(tmp->val));
}
Example #3
0
int solve(char *fileName) {
	JRB tree;
	int choice;
	int case1Flag = 0;

	JRB anotherTree;

  	while(1) {
      choice = menuSpellChecking();

      switch(choice) {
        case 1:
        printf("You choose option %d\n", choice);
        if(case1Flag) {
        	printf("You already choose this option\n");
        	continue;
        }
        if((tree = buildTree(fileName)) == NULL) {
			return 1;
		}
		printf("Build tree successfully.\n");

		jrb_traverse(anotherTree, tree) {
		    printf("%s - %s\n", jval_s(anotherTree->key), jval_s(anotherTree->val));
		}
        case1Flag = 1;
        continue;
        case 2:
        printf("You choose option %d\n", choice);
        if(!case1Flag) {
        	printf("Please enter the first option first!\n");
        	continue;
        }
		checkingSpell(tree);
        continue;
        case 3:
        printf("You choose option %d\n", choice);
        printf("Bye bye\n");
        if(case1Flag) {
			jrb_free_tree(tree);
        }
        break;
        default:
        printf("It is not an option\n");
        continue;
      }
      break;
    }
void BFS_Visit(Graph_Struct Graph,char start[])
{
	Graph_Symbol_Table *Table;
	char *u,*v;
	JRB node_rbt,tree,ele;
	Dllist node_queue,queue = new_dllist();
	printf("%s ",start);
	
	u = malloc(MAX_LENGTH_STRING);
	v = malloc(MAX_LENGTH_STRING);
	
	Table = Search_On_Table(Graph,start);
	Table->colour = 'g';
	
	dll_append(queue, new_jval_s(start));
	while(!dll_empty(queue))
	{
		node_queue = dll_first(queue);
		u = jval_s(node_queue->val);
		dll_delete_node(node_queue);
		node_rbt = jrb_find_str(Graph.Edges,u);
		if(node_rbt != NULL)
		{
			tree = (JRB)jval_v(node_rbt->val);
			jrb_traverse(ele,tree)
			{
				v = jval_s(ele->key);
				Table = Search_On_Table(Graph,v);
				if(Table->colour == 'w')
				{
					printf("%s ",v);
					Table->colour = 'g';
					if(Table->previous == NULL)
					{
						Table->previous = malloc(MAX_LENGTH_STRING);
					}
					strcpy(Table->previous,u);
					dll_append(queue, new_jval_s(v));
				}
			}
		}
Example #5
0
main()
{
  JRB b;
  JRB bn;
  IS is;

  is = new_inputstruct(NULL);
  b = make_jrb();

  while (get_line(is) >= 0) {
    (void) jrb_insert_int(b, atoi(is->text1), new_jval_s(strdup(is->text1)));
  }

  jrb_traverse(bn, b) {
    printf("%s", jval_s(bn->val));
  }
Example #6
0
void compress(int argc, char **argv, int verb) {
	int i, size;
	IS is;
	Dllist dir_list, DLLnode;
	JRB inode, JRBnode, paths, links;
	void *buf;
	tar *t;
	
	links = make_jrb();
	dir_list = new_dllist();
	inode = make_jrb();
	JRBnode = make_jrb();
	paths = make_jrb();

	for(i = 2; i < argc; i++){
		process_files(dir_list, inode, paths, argv[i], links);
	}

	dll_traverse(DLLnode, dir_list){
		process_files(dir_list, inode, paths, jval_s(DLLnode->val),links);
	}
Example #7
0
int cmpStr(Jval s1, Jval s2)
{
	return strcmp(jval_s(s1), jval_s(s2));
}
Example #8
0
int compareDictionaryEntry(Jval *a, Jval *b) {
	return strcmp(jval_s(*a), jval_s(*b));
}
Example #9
0
main(int argc, char **argv)
{

//most of our variables and data structures
int x;
unsigned int j;
int i;
long hSize;
char c;
char experiment[2];
char absName[10000];
char locName[10000];
char otherName[10000];
char input[10000];
JRB check;
Dllist names;
Dllist otherNames;
JRB addresses;
ip *fullAddress;
unsigned int count;
char * buffer;
int hosts;
int killLoc,killAbs,noAbs;
unsigned char ipAddress[4];
unsigned char num[4];
unsigned int actNum[4];
unsigned int numOfNames;

//we initialize addresses, it will hold ALL the names as keys that link to their ip struct
addresses = make_jrb();

//we set hSize based off what the lab assignment said
hSize = 350000;
//allocate buffer
buffer = (char*) malloc(sizeof(char) * hSize);
//open descriptor
hosts = open("converted", O_RDONLY);
//read into buffer then close
read(hosts,buffer,hSize);
close(hosts);

//set x which we use to determine when to terminate 
//data assignment loop
x = 0;

while (x < hSize)
{

/* We initialize names and otherNames every time through loop. There is a reason
   there isn't just one e.g just names that is explained below. */
names = new_dllist();
otherNames = new_dllist();


/*We assign bytes from position determined by x e.g our place in the buffer
  to unsigned chars that comprise the IP address*/

memcpy(&ipAddress[0],buffer+x,sizeof(char));
memcpy(&ipAddress[1],buffer+x+1,sizeof(char));
memcpy(&ipAddress[2],buffer+x+2,sizeof(char));
memcpy(&ipAddress[3],buffer+x+3,sizeof(char));

/*We assign bytes from position determined by x e.g our place in the buffer
  to unsigned chars which we use to determine number of names*/

memcpy(&num[0],buffer+x+4,sizeof(unsigned char));
memcpy(&num[1],buffer+x+5,sizeof(unsigned char));
memcpy(&num[2],buffer+x+6,sizeof(unsigned char));
memcpy(&num[3],buffer+x+7,sizeof(unsigned char));

//we increment x e.g our position in buffer by 8 since we just checked 8 bytes */
x += 8;

/*we assign each of those unsigned chars as unsigned ints */
for (i = 0; i < 4; i++)
	actNum[i] = num[i];	

//we shift the uints into their proper position 
for (i = 3; i >= 0; i--)
	actNum[i] << (3-i)*8;

/*We set numOfNames to 0 and then or it with all of our unsigned ints to get proper
of names*/
numOfNames = 0;
for (i = 0; i < 4; i++)
	numOfNames = numOfNames | actNum[i];

/* We use number of names to determine how many times we need to run our
   names assignment loop */
for (j = 0; j < numOfNames; j++)
{
/*We use these three values to manage our loops e.g killLoc ends local loop
  killAbs ends absLoop. Note the distinction between killAbs and noAbs.
  KillAbs merely implies that we are done checking for absolute names.
  Noabs implies that there was no absolute name on this loop through. */
killLoc = 0;
killAbs = 0;
noAbs = 0;
while (!killLoc)
{
/* We assign byte from x e.g position in the buffer
   to a char and then increment x */
memcpy(&c,buffer+x,sizeof(char));
x++;
/* We use our char c to build a string and
   then concatenate with absName. */
experiment[0] = c;
experiment[1] = '\0';
strcat(absName,experiment);

/*if we see a period we kill local loop and
  move onto absolute loop*/
if (c == '.')
	killLoc = 1;
/*if we see a null terminating character we kill local loop, skip absolute 
  loop and set noabs to true. If we don't see a period or a null
  terminating character, we concatenate our local name.*/
else if(c == '\0')
{
	killLoc = 1;
	killAbs = 1;
	noAbs = 1;
}
else
	strcat(locName,experiment);
}
while (!killAbs)
{
/*Basically the same as the local loop, except now we
  just keep going until there is a null terminating 
  character without worrying about the period. If 
  there is an abs, noabs set to false is implicit
  from the start of the loop e.g we set it to false
  and only the local loop can set it to true.*/
memcpy(&c,buffer+x,sizeof(char));
x++;
experiment[0] = c;
experiment[1] = '\0';
strcat(absName,experiment);

if (c == '\0')
	killAbs = 1;
}

/*We add our local name and (if there is one) absolute name
  to names and otherNames.*/
dll_append(names,new_jval_s(strdup(locName)));
dll_append(otherNames,new_jval_s(strdup(locName)));
if (!noAbs)
{
	dll_append(names,new_jval_s(strdup(absName)));
	dll_append(otherNames,new_jval_s(strdup(absName)));
}
//We empty out our locName and absName for the next pass	
strcpy(locName,"");
strcpy(absName,"");
}

/*I have these variables down here for readability*/
Dllist tmp;
Dllist temp;
char final[10000];
char inner[10000];
dll_traverse(tmp,names)
{
	//assign the ip and copy name into final
	fullAddress = (ip *) tmp->val.v; 
	strcpy(final,jval_s(tmp->val));
	
	//initialize jrb list in ip
	fullAddress->names = make_jrb();
	
	/*Traverse through otherNames to assign the ip's names. The reason
	  we have otherNames and not just names is that running names
	  again within this loop messes up the data. Note that otherNames
	  contains the exact same content as names.*/
	dll_traverse(temp,otherNames)
	{
		strcpy(inner,jval_s(temp->val));
		jrb_insert_str(fullAddress->names,strdup(inner),new_jval_v(NULL));
	
	}
	//assign the ip address
	for (i = 0; i < 4; i++)
		fullAddress->address[i] = ipAddress[i];
	//insert into addresses

	jrb_insert_str(addresses,strdup(final),new_jval_v(fullAddress));
	
	
}