Created
July 6, 2010 03:58
-
-
Save hobs/464978 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* | |
* learning_c | |
* | |
* Created by Constantine on 7/5/10. | |
* | |
*/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <string.h> // declares strcmp(). | |
#include <stdlib.h> // We'll need that later for malloc() and realloc | |
struct BookmarksEntry | |
{ | |
// right now this has a limitation of 255 characters for a URL, | |
// will look into how to do it longer | |
char url[255]; | |
char description[500]; | |
int rating; | |
bool isSfw; | |
}; | |
// track the amount of "database" entries | |
int totalDBEntries = 0; | |
struct BookmarksEntry *myBookmarks = NULL; | |
void CreateNewEntry() | |
{ | |
int sfw = 0; | |
// should create new entry in the myBookmarks struct | |
// find the length or size of the myBookmarks variable | |
// realloc the current myBookmarks and add one to the current size | |
// then use the last item in the array to store the entry | |
if (myBookmarks == NULL) | |
{ | |
myBookmarks = malloc(sizeof(struct BookmarksEntry)); | |
if (myBookmarks == NULL) | |
printf("ERROR, WE GOT PROBLEMS"); | |
return; | |
} | |
else | |
{ | |
struct BookmarksEntry *newPtr = NULL; | |
// takes the existing bookmarks db, the count of how many db items we have, and the size of the struct we made | |
// to assign it to a new pointer | |
// the reason we dont directly assign it is because we can run into out of memory errors | |
newPtr = realloc(myBookmarks, (totalDBEntries+1) *sizeof(struct BookmarksEntry)); | |
if (newPtr == NULL) | |
{ | |
printf("OOM :("); | |
return; | |
} | |
myBookmarks = newPtr; | |
} | |
++totalDBEntries; | |
printf("Enter a URL for your bookmark:\n>"); | |
scanf("%255s", myBookmarks[totalDBEntries -1].url); | |
fpurge( stdin ); | |
printf("Enter a description:\n>"); | |
scanf("%500s", myBookmarks[totalDBEntries -1].description); | |
fpurge( stdin ); | |
printf("Enter a rating, 1-6 (neutral midpoint):\n>"); | |
scanf("%d", &myBookmarks[totalDBEntries -1].rating); | |
fpurge( stdin ); | |
printf("Is this safe for work? (1 for yes, anything other number for no):\n>"); | |
scanf("%d", &sfw); | |
if (sfw == 1) | |
myBookmarks[totalDBEntries -1].isSfw = true; | |
else | |
myBookmarks[totalDBEntries -1].isSfw = false; | |
} | |
void ListCurrentEntries() | |
{ | |
// lists all entries in the myBookmarks struct | |
// walk through myBookmarks and list each entry | |
int x = 0; | |
while (x < totalDBEntries) | |
{ | |
printf("Bookmark %d URL: %s \n", (x+1),myBookmarks[x].url); | |
printf("Bookmark %d description: %s \n", (x+1), myBookmarks[x].description); | |
printf("Bookmark %d rating: %d \n", (x+1), myBookmarks[x].rating); | |
x+=1; | |
} | |
} | |
void CleanMem() | |
{ | |
// cleans out the stored memory locations of everything we have done | |
if (myBookmarks != NULL) | |
free(myBookmarks); | |
myBookmarks = NULL; | |
totalDBEntries = 0; | |
} | |
int main() | |
{ | |
fpurge( stdin ); | |
char userInput[5]; | |
bool theNeverEndingStory = true; | |
while (theNeverEndingStory == true) | |
{ | |
printf("Please type new, list, or quit:\n"); | |
printf("new list quit:\n>"); | |
scanf("%5s", userInput); | |
if(strcmp(userInput, "new") == 0) | |
CreateNewEntry(); | |
if(strcmp(userInput, "list") == 0) | |
ListCurrentEntries(); | |
if(strcmp(userInput, "quit") == 0) | |
theNeverEndingStory = false; | |
else printf("what the butt?"); | |
} | |
CleanMem(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment