Exemple #1
0
/******************************************************************************
   Function: spell_check

Description: when given a word, find it in a dictionary

     Inputs: diciontary - a pointer to a file 
             word - a const char pointer to a string
     
    Outputs: WORD_OK if word was found, WORD_BAD if word was not found
******************************************************************************/
int spell_check(const char *dictionary, const char *word)
{
  FILE *fp;                             /* file pointer */
  int compare_result = FALSE;           /* variable to toggle if word found */
  char file_word[LONGEST_WORD];         /* array for the string from file */
  char passed_word[LONGEST_WORD] = {0}; /* array for word passed to us */
  
  strcpy(passed_word, word);            /* copies word into our array */
  
    /* converts string to uppercase */
  passed_word[LONGEST_WORD] = *mystrupr(passed_word); 
  
  fp = fopen(dictionary, "rt");         /* initialize file pointer */
  
    /* if file pointer isn't null */
  if(fp)
  {
      /* while not at end of file */
    while(!feof(fp))
    {
        /* if next line line isn't null put it in file_word array */
      if(fgets(file_word, LONGEST_WORD, fp))
      {
          /* get rid of new line at end */
        NewLineReplace(file_word); 
        
          /* convert to uppercase */
        file_word[LONGEST_WORD] = *mystrupr(file_word);
        
          /* if first letter of dictionary word is greater than first letter
           * of passed word just stop looking */
        if(file_word[0] > passed_word[0])
          break;
        
          /* compares the passed word anf file word */
        compare_result = strcmp(file_word, passed_word); 
        
          /* if strings are the same we found a matching word so close the 
           * file and return word ok */
        if(compare_result == TRUE)
        {
          fclose(fp);
          return WORD_OK;
        }
      }
    }
    
    fclose(fp); /* close file when we're at the end if word wasn't found */
    return WORD_BAD; /* couldn't find matchinf word so return word bad */
  }
  else
  {
      /* file pointer was null so return file error */
    return FILE_ERR_OPEN; 
  }
}
int main(int argc, char **argv)
{


    //checks arguments
    if(argc == 1)
    {
        printf("Usage like %s IV\n",argv[0]);
        exit(1);
    }
    if(argc > 2)
    {
        printf("Too many arguments\n");
        exit(2);
    }

    mystrupr(argv[1]);// conversion to upper case

    if(check_roman_numerals(argv[1]) != 0)
    {
        printf("Ivalid roman numeral\n");
        exit(3);

    }


    printf("%d\n",romanToArab(argv[1]));
    return 0;

}
void test1(void)
{
int i;
const char *words[] = {"Four", "score", "and", "seven", "years", "ago", "our",
                       "fathers", "brought", "forth", "on", "this",
                       "continent", "a", "new", "NATION", "fast123",
                       "123  abc", "Hello!!", "", "*&^%$#8UPPERlower"
                      };

int num_words = sizeof(words) / sizeof(*words);

printf("\nTest1--------------------------------------------------------\n");
for (i = 0; i < num_words; i++)
{
    char buffer[LONGEST_WORD + 1];
    strcpy(buffer, words[i]);
    mystrupr(buffer);
    printf("Word: %s (%s)\n", words[i], buffer);
}
}
Exemple #4
0
/******************************************************************************
   Function: words_starting_with

Description: checks words in a dictionary to see if they satar with a certain 
             letter

     Inputs: dictionary - a pointer a file 
             letter  -  a letter to check dictionary entries against
     
    Outputs: number of words that start with the letter given
******************************************************************************/
int words_starting_with(const char *dictionary, char letter)
{
  FILE *fp;                  /* file pointer */
  char buffer[LONGEST_WORD]; /* buffer array for each line */
  int words = 0;             /* word counter */
  
    /* initialize file pointer */
  fp = fopen(dictionary, "rt");
  
    /* if fle open was successful */
  if(fp)
  {
      /* while not at end of file */
    while(!feof(fp))
    {
        /* get the next line and put it in buffer */
      if(fgets(buffer, LONGEST_WORD, fp))
      {
          /* convert letter to upper case if necessary */
        if(letter >= 'a' && letter <= 'z')
          letter -= CHANGETOUPPER;
        
          /* convert string to all uppercase */
        buffer[LONGEST_WORD] = *mystrupr(buffer);
        
          /* if first letter in buffer matches letter given words++ */
        if(letter == *buffer)
          words++;
      }
    }
    fclose(fp);              /* close file whne we're done */
    
    return words;            /* return number of words we found */
  }
  else
  {
    return FILE_ERR_OPEN;    /* file couldnt be opened */
  }
}
static int mywebsocket_connect__(const char* url, int ct)
{
	int ret = -1;
	int port = 80;
	char server[256];
	char loc[256];
	char redirect[1024 * 2];
	char tmp[1024 * 2];
	char key[1024];
	char val[1024];
	char* p;
	int rescode;

	int s;
	int read, st, clen = -1, wri;
	int chunk = 0;

	if (url == NULL)return ret;
	if (ct >= 10)return -1;

	if (strlen(url)>800)return ret;

	if (is_ws(url) == 0)return ret;
	port = get_port(url);
	get_server(url, server, sizeof(server));
	get_location(url, loc, sizeof(loc));

	if (server[0] == 0 || loc[0] == 0)return ret;

	//printf("port=%d\n",port);
	//printf("server=>>%s<<\n", server);
	//printf("dir=>>%s<<\n", loc);
	//printf("\n");


	sprintf(tmp, "GET %s HTTP/1.1\r\n"
		"Host: %s\r\n"
		"Upgrade: websocket\r\n"
		"Connection: upgrade\r\n"
		"Sec-WebSocket-Version: 13\r\n"
		"Sec-WebSocket-Key: E4WSEcseoWr4csPLS2QJHA==\r\n"
		"\r\n"
		, loc, server);

	s = dualsock_create(server, port);

	if (s == -1)return ret;

	//printf("connect!!\n");
	//printf("%s", tmp);

	wri = send(s, tmp, strlen(tmp), 0);
	if (wri < 1) {
		closesocket(s);
		return ret;
	}
	read = recv_until(s, tmp, sizeof(tmp), '\n');
	if (read < 1) {
		closesocket(s);
		return ret;
	}
	//printf("%s",tmp);

	st = 0;
	sscanf(tmp, "%*s%d", &st);

	redirect[0] = 0;

	while (1) {
		read = recv_until(s, tmp, sizeof(tmp), '\n');
		if (read < 1)break;
		if (tmp[0] == '\r' || tmp[0] == '\n')break;
		p = strchr(tmp, ':');
		if (p == NULL)continue;

		//printf("%s", tmp);

		*p = 0;
		p++;
		key[0] = 0;
		val[0] = 0;
		sscanf(tmp, "%s", key);
		sscanf(p, "%s", val);
		mystrupr(key);
		if (strcmp(key, "LOCATION") == 0) {
			strcpy(redirect, val);
		}
		else if (strcmp(key, "CONTENT-LENGTH") == 0) {
			sscanf(val, "%d", &clen);
		}
		else if (strcmp(key, "TRANSFER-ENCODING") == 0) {
			strcpy(redirect, val);
			if (strstr(val, "chunked"))chunk = 1;
		}
	}
	if (read < 1) {
		closesocket(s);
		return ret;
	}
	if (clen > 1024 * 1024 - 100) {
		closesocket(s);
		return ret;
	}
	rescode = st;
	if (clen < 0)clen = 1024 * 1024 - 100;

	read = 0;
	if (st >= 200){
		if (clen > 0) {
			if (chunk) {
				read = recv_chunked_null(s);
			}
			else {
				read = recv_sz_null(s, clen);
			}
		}
	}
	if (st >= 300 && st <= 399) {
		closesocket(s);

		if (redirect[0] == 0 || strlen(redirect) > 500 || strlen(loc)>500 || strlen(server)>500) {
			return ret;
		}
		if (strstr(redirect, "http://") == NULL) {
			if (redirect[0] == '/') {
				strcpy(tmp, redirect);
				sprintf(redirect, "http://%s:%d%s", server, port, tmp);
			}
			else {
				strcpy(tmp, redirect);
				sprintf(redirect, "http://%s:%d%s", server, port, loc);
				p = strrchr(redirect, '/');
				if (p) {
					p++;
					*p = 0;
					strcat(redirect, tmp);
				}
			}
		}
		ret = mywebsocket_connect__(redirect, ct + 1);
		return ret;
	}
	if (st !=101) {
		closesocket(s);
		return ret;
	}
	ret = s;
	return ret;
}