I am embroiled in a long and brutal war against landfish over documentation. I am of the opinion that my style of documentation is the minimum required for someone else to be able to read my code; is the following too much documentation?
So, Massassi: do I document too much?
Code:
/* CS2110 Final Project
Written by <blrglglg>
Created on 4/2/07
Revisions:
Description:
Given a field to compare a specified column to, this will tokenize a search string and look for any matches to that search string
It will then return the MySQL result resource
*/
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <string.h>
#include <ctype.h>
#include "dbh.h"
#include "search.h"
#include "strip.h"
/* Given a number indicating which column search, this returns all the records that match the search and displays them
The numbering goes as follows:
1 - fName (First Name)
2 - lName (Last Name)
3 - Address (Lines 1 & 2)
4 - city
5 - state
6 - zip code */
void search(int fieldNum, MYSQL *mysql) {
/* Holds the search terms */
char c[1024];
/* Holds the eventual result */
MYSQL_RES *result;
/* Get the search terms */
printf("\nPlease enter the terms you wish to search by, separate by spaces: ");
fgets(c, 1024, stdin);
/* Strip the newline off of it */
strip(c);
/* This will hold the name of the column we search */
char colName[5];
/* Which column are we searching? */
/* We don't test for addresses, as that will be a specially-built query */
switch(fieldNum) {
/* First name */
case 1:
sprintf(colName, "fName");
break;
/* Last name */
case 2:
sprintf(colName, "lName");
break;
/* City */
case 4:
sprintf(colName, "city");
break;
/* State */
case 5:
sprintf(colName, "state");
break;
/* ZIP code */
case 6:
sprintf(colName, "zip");
break;
}
/* Will hold the search query */
char q[2048] = "SELECT addId, fName, lName, addLine1, addLine2, city, state, zip FROM addressBook";
/* If we're not searching addresses, use the specified column name to determine where we search */
if (fieldNum != 3) {
/* Tokenize the ssearch terms */
char *tokenPtr;
tokenPtr = strtok(c, " ");
/* Was there at least one search term? */
if (tokenPtr != NULL) {
/* Make sure that the given token is properly escaped */
char escapeToken[512];
mysql_real_escape_string(mysql, escapeToken, tokenPtr, strlen(tokenPtr));
sprintf(q, "%s WHERE UPPER(%s) LIKE UPPER('%%%s%%')", q, colName, escapeToken);
tokenPtr = strtok(NULL, " ");
/* Attach all the other search terms */
while (tokenPtr != NULL) {
/* Escape any dangerous characters in the string */
mysql_real_escape_string(mysql, escapeToken, tokenPtr, strlen(tokenPtr));
sprintf(q, "%s OR UPPER(%s) LIKE UPPER('%%%s%%')", q, colName, escapeToken);
tokenPtr = strtok(NULL, " ");
}
}
/* Tack on the "order by the auto-increment column" bit */
sprintf(q, "%s ORDER BY addID", q);
}
/* Otherwise, we're searching the addresses */
else {
/* Tokenize the ssearch terms */
char *tokenPtr;
tokenPtr = strtok(c, " ");
/* Was there at least one search term? */
if (tokenPtr != NULL) {
/* Make sure that the given token is properly escaped */
char escapeToken[512];
mysql_real_escape_string(mysql, escapeToken, tokenPtr, strlen(tokenPtr));
sprintf(q, "%s WHERE UPPER(addLine1) LIKE UPPER('%%%s%%') OR UPPER(addLine2) LIKE UPPER('%%%s%%')", q, escapeToken, escapeToken);
tokenPtr = strtok(NULL, " ");
/* Attach all the other search terms */
while (tokenPtr != NULL) {
/* Escape any dangerous characters in the string */
mysql_real_escape_string(mysql, escapeToken, tokenPtr, strlen(tokenPtr));
sprintf(q, "%s OR UPPER(addLine1) LIKE UPPER('%%%s%%') OR UPPER(addLine2) LIKE UPPER('%%%s%%')", q, escapeToken, escapeToken);
tokenPtr = strtok(NULL, " ");
}
}
/* Tack on the "order by the auto-increment column" bit */
sprintf(q, "%s ORDER BY addID", q);
}
/* Execute the query */
mysql_query(mysql, q);
/* Get the results */
result = mysql_store_result(mysql);
/* Print the number of results found */
printf("%d result(s) found\n", (int)mysql_num_rows(result));
/* If there was at least one result, print them all */
if (result) {
MYSQL_ROW row;
while((row = mysql_fetch_row(result))) {
printf("\nRecord ID: %s\n", row[0]);
printf("Name: %s %s\n", row[1], row[2]);
printf("Address: %s\n", row[3]);
/* If there is anything in address line 2, print it, too */
if (strlen(row[4]) > 0) {
printf(" %s\n", row[4]);
}
printf("City: %s\n", row[5]);
printf("State: %s\n", row[6]);
printf("ZIP: %s\n", row[7]);
}
}
mysql_free_result(result);
}
/* Prints the selections a user has */ /* If the user input is invalid, it simply returns a call to the menu */ int printSearchMenu() {
printf("Please select which field you would like to search by:\n\n");
printf("1. First name\n");
printf("2. Last name\n");
printf("3. Street address\n");
printf("4. City\n");
printf("5. State\n");
printf("6. ZIP code\n");
printf("7. Cancel\n");
/* Get the user's selection */
char userInput[1024];
printf("Please enter your selection: ");
fgets(userInput, 1024, stdin);
/* Is the input invalid? If it is, return a call to the menu */
if (atoi(userInput) < 1 || atoi(userInput) > 7) {
printf("\nERROR: your input must be an integer between 1 and 7.\n");
return printSearchMenu();
}
/* Otherwise, if it's not an invalid selection, return the integer version of the user's input */
return atoi(userInput);
}So, Massassi: do I document too much?
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken






Your code makes me want to cry.
