Skip to content

Instantly share code, notes, and snippets.

@gegagome
Last active November 8, 2015 17:20
Show Gist options
  • Save gegagome/cd201469428c03da3e4b to your computer and use it in GitHub Desktop.
Save gegagome/cd201469428c03da3e4b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define URL_PROMPT "URL:"
#define MAX_BUFFER 256000
char *addressFinder(const char *sPtr, const char *word);
int instanceCounter(const char *ptr, const char *w);
void printPointerDifference(const char *p1, const char *p2, int diff);
void instanceTag(const char *ptr, const char *w);
char *isWhiteSpace (const char *space);
int main(void) {
char curl[BUFSIZ + 1] = "curl -s ";
char urlContent[MAX_BUFFER];
char buffer[BUFSIZ + 1];
FILE* fp;
// grader thing
setvbuf(stdout, NULL, _IONBF, 0);
// let's prompt for the url and save it
printf("%s http://wfs.sbcc.edu/staff/nfguebels/web/cs137/html_src_list/simple.html\n", URL_PROMPT);
fgets(buffer, BUFSIZ, stdin);
// fgets always ends with '\n' so let's change it to '\0'
buffer[strlen(buffer) - 1] = '\0';
// let's concatenate the curl command with the variable url
strcat(curl, buffer);
// check for debugging
// printf("%s\n", curl);
// let's store the content of the html document into fp
fp = popen(curl, "r");
if (fp != NULL) {
while (fgets(buffer, BUFSIZ, fp) != NULL) {
// let's concatenate the contents of this file line by line
strcat(urlContent, buffer);
}
// remove contents of buffer
buffer[0] = '\0';
pclose(fp);
printf("%s", urlContent);
do {
printf("Ready\n");
fgets(buffer, BUFSIZ, stdin);
// buffer[strlen(buffer) - 1] = '\0';
switch (buffer[0]) {
case 'c':
printf("%d\n", instanceCounter(urlContent, "src=\""));
break;
case 't':
instanceTag(urlContent, "src=\"");
break;
case 'u':
instanceTag(urlContent, "src=\"");
break;
case 'f':
instanceTag(urlContent, "src=\"");
break;
}
} while (buffer[0] != 'q');
} else {
printf("File didn't load. Sorry");
}
printf("Complete");
return EXIT_SUCCESS;
}
char *addressFinder(const char *sPtr, const char *word) {
sPtr = strstr(sPtr, word);
return sPtr;
}
int instanceCounter(const char *ptr, const char *w) {
int count = 0;
while (ptr = addressFinder(ptr, w)) {
if (isspace(*(--ptr))) {
count++;
}
ptr += 2;
}
return count;
}
void instanceTag(const char *ptr, const char *w) {
const char *temp1;
const char *temp2;
// int repeat = 0;
while (ptr = addressFinder(ptr, w)) {
// printf("\n%s\n", "===============");
temp1 = NULL;
temp2 = NULL;
temp1 = ptr;
if (isspace(*(--temp1))) {
// printf("Found a %c\n", *temp1);
while (*(--temp1) != '<') {
// --temp1;
}
if (*temp1 == '<') {
temp1 += 1;
// printf("temp1 is %c\n", *(temp1));
temp2 = temp1;
temp2 = isWhiteSpace(temp2);
// temp2 = strchr (temp2, '\n');
// printf("temp2 is %c\n", *(temp2));
}
if (temp2) {
printPointerDifference(temp1, temp2, (temp2 - temp1));
}
} else {
// printf("Not a ' '\n");
}
ptr += 1;
// printf("%d\n", ++repeat);
}
}
void printPointerDifference(const char *p1, const char *p2, int diff) {
// printf("p1 is %c\n", *p1);
// printf("p2 is %c\n", *p2);
// printf("diff is %d\n", diff);
if (diff > 0) {
char array[diff + 1];
strncpy(array, p1, diff);
array[diff] = '\0';
// printf("diff is %d\n", diff);
printf("%s\n", array);
}
}
void quoteContents () {
}
char *isWhiteSpace (const char *space) {
while (!isspace(*(++space))) {
// printf("%c\n", *space);
}
return space;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment