frustration on URL encode-decode
I'm working on this program to take a person's name, address, and phone number and convert it URL coding print it, then unencode it then print it again normally, with labels over each part. I've run into a couple problems.
1. I think I'm overcomplicating it first of all.
2. I want to capture the input but my scanf stops capture when I type a space in my URlEncode function.
3. I haven't gotten to it yet, but I know I'm going to hit a wall when I try to convert the ASCII characters to hex and printing them to standard output (I've commented that part out)
Here is my code, I'm not looking for anyone to write my code, but I think I need a better strategy and approach. Thanks.
Code:
////////////////////////////////////////////////////////////////////////////////
//
// File Name : URL-encoder-decoder.c
//
// Date : August, 2007
//
// Platform : IBM PC (Windows XP)
//
// Compiler : Microsoft Visual C++ 6.0
//
////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_DEPRECATE
#define MAX_ARRAY_SIZE 100 // maximum array size
//compiler includes
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
//constants
//user defined definitions
typedef struct { // personal_type definition
char name[MAX_ARRAY_SIZE];
char address[MAX_ARRAY_SIZE];
char phoneNumber[MAX_ARRAY_SIZE];
} PERSONAL_TYPE;
//function prototypes
PERSONAL_TYPE UrlEncode(void); //function to scan personal info and encode it and print it
//PERSONAL_TYPE UrlDecode(void); //function to decode URL info
void PrintPersonInfo (PERSONAL_TYPE personToPrint); //function to print information after decoded
////////////////////////////////////////////////////////////////////////////////
//
// Function Name : main
//
// Originated : August, 2007
//
// Abstract : processing to read values for personal information and
// encode then decode the information
//
// Parameters : None
//
// Return Value : error code - zero
//
// Misc. I/O : na
//
// Revisions : August, 2007
//
////////////////////////////////////////////////////////////////////////////////
int main(void){
//local variables
PERSONAL_TYPE personOne = {"\0", "\0", "\0"}; // empty data to pass to functions
PrintPersonInfo(personOne); // function call to print personOne information by value
personOne = UrlEncode();
exit(0);
}
////////////////////////////////////////////////////////////////////////////////
//
// Function Name : PrintPersonInfo
//
// Originated : August, 2007
//
// Abstract : function to personal name and address
//
// Parameters : PERSONAL_TYPE
//
// Return Value : none
//
// Misc. I/O : na
//
// Revisions : August, 2007
//
////////////////////////////////////////////////////////////////////////////////
void PrintPersonInfo(PERSONAL_TYPE personToPrint) {
printf("\n==================================================================\n");
printf("NAME: %s ADDRESS: %s PHONE NUMBER: %s\n",personToPrint.address,
personToPrint.name, personToPrint.phoneNumber); // prints structures from PERSONAL_TYPE
} // end function PrintPersonInfo
////////////////////////////////////////////////////////////////////////////////
//
// Function Name : UrlEncode
//
// Originated : August, 2007
//
// Abstract : function to scan name, address, phone number and URL encode it
//
// Parameters : PERSONAL_TYPE
//
// Return Value : none
//
// Misc. I/O : na
//
// Revisions : August, 2007
//
////////////////////////////////////////////////////////////////////////////////
PERSONAL_TYPE UrlEncode(void) {
// local variables
inc c;
PERSONAL_TYPE inputPerson = {"<NULL>", "<NULL>", "<NULL>"}; //empty variable for function
//begin
printf("Enter your Name: "); // print message to user
scanf("%s", inputPerson.name);// scan struture for name and assign to inputPerson
printf("Enter your address: ");// print message to user
scanf("%s", inputPerson.address);// scan struture for address and assign to inputPerson
printf("Enter your phone number: "); // print messsage to user
scanf("%s", inputPerson.phoneNumber); // scan struture for phone and assign to inputPerson
/*
void UrlEncode(void){ // encoding function
int c=0;
char *h = "0123456789abcdef";
while((c = getchar()) != EOF ){
if( 'a' <= c && c <= 'z'
|| 'A' <= c && c <= 'Z'
|| '0' <= c && c <= '9'
|| c == '-' || c == '_' || c == '.' )
putchar(c);
else if( c == ' ' )
putchar('+');
else {
putchar('%');
putchar(h[c >> 4]);
putchar(h[c & 0x0f]);
}
}
}*/
return inputPerson; // return struture inputPerson to function caller
} // end function UrlEncode
/////////////////////////////////////////////////////////////////////////
/*
void UrlDecode(void){ // decode function
char c, c1, c2;
printf("enter your sentence\n");
while( (c = getchar()) != EOF ){
if( c == '%' ){
c1 = getchar();
c2 = getchar();
if( c1 == EOF || c2 == EOF ) exit(0);
c1 = tolower(c1);
c2 = tolower(c2);
if( ! isxdigit(c1) || ! isxdigit(c2) ) exit(0);
if( c1 <= '9' )
c1 = c1 - '0';
else
c1 = c1 - 'a' + 10;
if( c2 <= '9' )
c2 = c2 - '0';
else
c2 = c2 - 'a' + 10;
putchar( 16 * c1 + c2 );
} else if( c == '+' )
putchar(' ');
else
putchar(c);
}
} */
getting closer but new problem
I've taken the next step and removed the user's input from the encoding process. now I have a new problem. The encoder function encodes the text just fine, except it is appehending %0A which i think is the newline, and it doesn't return the encoded text back to main. So my functions to print out the encoded segments will just print what the user inputted in the first place. Any ideas?
Code:
////////////////////////////////////////////////////////////////////////////////
//
// File Name : URLCoding.c
//
// Platform : IBM PC (Windows XP)
//
// Compiler : Microsoft Visual C++ 6.0
//
////////////////////////////////////////////////////////////////////////////////
#define _CRT_SECURE_NO_DEPRECATE
// Compiler Includes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Constants
#define MAX_STRING_LENGTH 100 // Maximum file name length
#define MAX_ARRAY_SIZE 100 // maximum array size
//User defined definitions struct { // PERSON_TYPE structure definition
typedef struct {
char name[MAX_ARRAY_SIZE];
char address[MAX_ARRAY_SIZE];
char phone[MAX_ARRAY_SIZE];
} PERSON_TYPE;
//function prototypes
char encode_string(string, len); // function to encode information
PERSON_TYPE scanPersonByValue(void); // function to scan person info
void printPersonInfo (PERSON_TYPE personToPrint); //function to print information
int main (void) {
//local variables
PERSON_TYPE personOne = {"<John Paul Jones>","<123 Rainbow Way>","<1-888-555-5555>"};
printPersonInfo(personOne); // prints out default array
personOne = scanPersonByValue(); //
printPersonInfo(personOne); // prints out new information user typed in
// encoded each portion of the datatype.
encode_string(personOne.name, (strlen(personOne.name)));
encode_string(personOne.address, (strlen(personOne.address)));
encode_string(personOne.phone, (strlen(personOne.phone)));
// this function should print each part as encoded, but it returns what the user typed in
printPersonInfo(personOne);
return 0;
}
PERSON_TYPE scanPersonByValue (void) {
PERSON_TYPE inputPerson;
char buff[MAX_ARRAY_SIZE] = {"\0"};
printf("\nenter your name:");
fgets(inputPerson.name, sizeof buff, stdin);
printf("\nenter your address:");
fgets(inputPerson.address,sizeof buff, stdin);
printf("\nenter your phone number:");
fgets(inputPerson.phone, sizeof buff, stdin);
return inputPerson;
} //end function scanPersonByValue
void printPersonInfo (PERSON_TYPE personToPrint) {
printf("\n==================================================================\n");
printf("NAME: %s \nADDRESS: %s \nPHONE: %s\n",personToPrint.name,
personToPrint.address, personToPrint.phone); // prints structures from PERSON_TYPE */
} // end function PrintpersonInfo
//this function encodes the data passed to it using URL encoding scheme
char encode_string(string, len)
char *string;
size_t len;
{
for(;len--;string++) {
if (*string=='*' || *string=='-'
|| *string=='.'
|| '0'<=*string && *string<= '9'
|| 'A'<=*string && *string<= 'Z'
|| 'a'<=*string && *string<='z'
|| *string=='_') {
printf("%c",*string);
}
else if (*string == ' ') {
printf("+");
}
else {
printf("%%%02X",*string);
}
}
return *string;
}// end function encode_string