Ejemplo n.º 1
0
main()
{
  Queue q;
  Stack s;
  Jval v;
  IS is;
  int i;

  q = new_queue();
  s = new_stack();
  i = 0;
  is = new_inputstruct(NULL);

  while (get_line(is) >= 0) {
    if (i % 2 == 0) {
      queue_enqueue(q, new_jval_s(strdup(is->fields[0])));
    } else {
      stack_push(s, new_jval_s(strdup(is->fields[0])));
    }
    i++;
  }
  while (!queue_empty(q)) { 
    v = queue_dequeue(q);
    printf("%s\n", v.s);
  }
  while (!stack_empty(s)) { 
    v = stack_pop(s);
    printf("%s\n", v.s);
  }
}
Ejemplo n.º 2
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);
}
Ejemplo n.º 3
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));
}
Ejemplo n.º 4
0
void trav_dir(Dllist dir_list, JRB inode, JRB path, char *name){
	DIR* dir;
	struct dirent* entry;

	char dir_name[1000],
		 buf[PATH_MAX+1];
	dir = opendir(name);

	if(dir == NULL){
		fprintf(stderr,"Error: directory opening failed\n");
		exit(1);				
	}

	while((entry = readdir(dir)) != NULL){
		strcpy(dir_name,entry->d_name);
		if(strcmp(dir_name,".") && strcmp(dir_name,"..")){
			char entry_path[PATH_MAX+1];
			if(strcmp(name,dir_name)){
				strcpy(entry_path,name);
				strcat(entry_path,"/");
				strcat(entry_path,dir_name);
			}

			char *res = realpath(entry_path,buf);
			if(res){
				char buf[PATH_MAX+1];
				dll_append(dir_list,new_jval_s(strdup(res)));
			}else{
				fprintf(stderr,"ERROR: entry path does not exist\n");
				exit(1);
			}
		}
	}
	closedir(dir);
}
Ejemplo n.º 5
0
void getTableData(FILE *f, SymbolTable *table) {
	char *word;

	if((word = myMalloc(sizeof(char), SIZE)) == NULL) {
		return;
	}	

	rewind(f);
	while(fscanf(f, "%s\n", word) != EOF) {
		if(strcmp(word, "") == 0)
			continue;
		addEntry(new_jval_s(word), new_jval_s(word), table);
		if((word = myMalloc(sizeof(char), SIZE)) == NULL) {
			return;
		}
	}
	rewind(f);
	sortEntry(table);
}
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));
				}
			}
		}
Ejemplo n.º 7
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));
  }
Ejemplo n.º 8
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));
	
	
}