Esempio n. 1
0
File: IO.c Progetto: mike3651/CSE332
int readFile(FILE *infile, customer customerList[]) {
	int quantity; 
	float price;
	char name[NAME_LENGTH], items[MAX_ITEM];
	int customerCount = 0;
	while(fscanf(infile, "%s %d %s %*c %f", name, &quantity, items,  &price) == 4)
	{
		customer c;
		// create the item
		item i;
		strcpy(i.itemName, items);
		i.amountPurchased = quantity;
		i.itemPrice = price;

		// see if the customer doesn't exist
		if (doesNotExist(customerList, name, customerCount)) {
			strcpy(customerList[customerCount].name, name);
			// create the customer			
			strcpy(c.name, name);		
			c.itemCount = 1;	
			customerCount++;
			c.items[c.itemCount - 1] = i;	
			c.totalCost = quantity * price;
			customerList[customerCount - 1] = c;
		} else {
			updateCustomer(customerList, name, customerCount, i);
		}				
	}

	for (int k = 0; k < customerCount; k++) {
		sortByValue(&customerList[k]);
	}		

	for (int k = 0; k < customerCount; k++) {
		reversePrices(&customerList[k]);
	}		

	// ITEMS HAVE BEEN SORTED //


	sortByCustomers(customerList, customerCount);

	// CUSTOMERS HAVE BEEN SORTED//

	return customerCount;
}
void processRequest(char *request, int fd, struct logInformation *logInfo)
{
    char command[BUFFERLENGTH];
    char argument[BUFFERLENGTH];
    
    // parse the command and argument from the request
    if (sscanf(request, "%s%s", command, argument) != 2)
        return;
    
    sanitize(argument);
    //printf("sanitized version is %s\n", argument);
    
    // get the quoted first line of the request and store it in the log structure
    int size = 0;
    char *requestPointer = request;
    while (*requestPointer != '\r' && *requestPointer != '\n')
    {
        size++;
        requestPointer++;
    }
    logInfo->quotedFirstLineOfRequest = malloc((size+3) * sizeof(char));
    strcpy(logInfo->quotedFirstLineOfRequest, "\"");
    strncat(logInfo->quotedFirstLineOfRequest, request, size);
    strcat(logInfo->quotedFirstLineOfRequest, "\"");
    
    if (strcmp(command, "GET") == 0 || strcmp(command, "POST") == 0)
    {
        if (strcmp(argument, "status") == 0)
        {
            reply_status(fd, logInfo);
        }
        else if (strcmp(getFileNameExtension(argument), "cgi") == 0) // cgi script request
        {
            reply_cgi(argument, fd, logInfo);
        }
        else if (doesNotExist(argument))
        {    
            reply_404(argument, fd, logInfo);
        }
        else if (isDirectory(argument))
        {
            // check if the directory contains an index.html file
            char *path = malloc(strlen(argument) + strlen("index.html") + 2);
            strcpy(path, argument);
            strcat(path, "/");
            strcat(path, "index.html");
            if (doesNotExist(path))
            {
                reply_ls(argument, fd, logInfo); // if index.html does not exist then list the contents of the directory
            }    
            else // otherwise cat index.html
            {
                reply_cat(path, fd, logInfo);
            }
            free(path);
        }
        else
        {
            reply_cat(argument, fd, logInfo);
        }
    }
    else
    {
        reply_notImplemented(fd, logInfo);
    }
}