added comments and cleaned up trailing spaces

This commit is contained in:
Chris Cromer 2016-10-21 18:42:37 -03:00
parent 7012998cfa
commit 11900ca5cc
10 changed files with 86 additions and 15 deletions

View File

@ -13,4 +13,4 @@ generarxml_SOURCES = main.c \
encoding.h encoding.h
generarxml_CPPFLAGS = $(XML_CPPFLAGS) generarxml_CPPFLAGS = $(XML_CPPFLAGS)
generarxml_LDFLAGS= $(XML_LIBS) generarxml_LDFLAGS= $(XML_LIBS)
generarxml_LDADD = ${libxml2_LIBS} generarxml_LDADD = ${libxml2_LIBS}

View File

@ -1,2 +1,9 @@
/*
* This function converts the latin1 ISO_8859-1 encoding into utf-8.
*/
char *latin2utf8(char *input); char *latin2utf8(char *input);
/*
* This function converts the utf-8 encoding into latin1 ISO_8859-1.
*/
char *utf82latin(char *input); char *utf82latin(char *input);

View File

@ -26,7 +26,7 @@ int main(int argc, char **argv) {
printusage(0); printusage(0);
return 1; return 1;
} }
/* Read the command line arguments */ /* Read the command line arguments */
#ifdef DEBUG #ifdef DEBUG
printf("Arguments:\n"); printf("Arguments:\n");
@ -147,7 +147,7 @@ void cleanup() {
/* Cleanup on aisle 3 */ /* Cleanup on aisle 3 */
int i; int i;
int j; int j;
if (config) { if (config) {
if (config->file) { if (config->file) {
free(config->file); free(config->file);
@ -168,7 +168,7 @@ void cleanup() {
free(config); free(config);
} }
if (book) { if (book) {
for (i = 0; i < book->chapters; i++) { for (i = 0; i < book->chapters; i++) {
for (j = 0; j < book->chapter[i]->verses; j++) { for (j = 0; j < book->chapter[i]->verses; j++) {
free(book->chapter[i]->verse[j]); free(book->chapter[i]->verse[j]);

View File

@ -1,5 +1,14 @@
#define MEANING 30 + 12 #define MEANING 30 + 12
/*
* This struct houses all the information from the .conf file.
*
* file is the output xml file
* bible is the bible we are searching for
* book is the book we are searching for
* chapter contains the name of the chapter
* chapter_numbers contains the specific chapter numbers to search for
*/
struct configuration { struct configuration {
char *file; char *file;
char *bible; char *bible;
@ -8,6 +17,14 @@ struct configuration {
char *chapter_numbers; char *chapter_numbers;
} typedef CONFIG; } typedef CONFIG;
/*
* This struct houses all the data for a specific chapter.
*
* chapter is the chapter number of the struct
* current is used to track iteration in the struct
* verses is the number of verses contained in the verse variable
* verse contains the verses
*/
struct chapterdata { struct chapterdata {
int chapter; int chapter;
int current; int current;
@ -15,14 +32,34 @@ struct chapterdata {
char **verse; char **verse;
} typedef CHAPTER; } typedef CHAPTER;
/*
* This struct contains the data for the book and it's chapters.
* current is used for iteration in the struct
* chapters is the ammount of chapters that are contained in the chapter variable
* chapter contains all of the chapters
*/
struct bookdata { struct bookdata {
int current; int current;
int chapters; int chapters;
CHAPTER **chapter; CHAPTER **chapter;
} typedef BOOK; } typedef BOOK;
/*
* Global variables.
*
* config contains the configuration that has been loaded from the .conf file
* book contains all the information read from Biblia.txt
*/
CONFIG *config; CONFIG *config;
BOOK *book; BOOK *book;
/*
* This function is called on exit to clean up the memory usage.
*/
void cleanup(); void cleanup();
/*
* Print information on the program's usage. If the argument is 1, the user
* put something incorrect as an argument.
*/
void printusage(int error); void printusage(int error);

View File

@ -5,8 +5,10 @@
#include <libxml/tree.h> #include <libxml/tree.h>
#include "main.h" #include "main.h"
/*
int makexml() { * This function generates the xml and saves it to the output file.
*/
void makexml() {
int i = 0; int i = 0;
int j = 0; int j = 0;
int length; int length;
@ -19,7 +21,7 @@ int makexml() {
xmlNodePtr versetag = NULL; xmlNodePtr versetag = NULL;
xmlNodePtr node = NULL; xmlNodePtr node = NULL;
xmlNodePtr text = NULL; xmlNodePtr text = NULL;
LIBXML_TEST_VERSION; LIBXML_TEST_VERSION;
doc = xmlNewDoc(BAD_CAST "1.0"); doc = xmlNewDoc(BAD_CAST "1.0");
@ -40,6 +42,7 @@ int makexml() {
xmlAddChild(node, text); xmlAddChild(node, text);
xmlAddChild(booktag, node); xmlAddChild(booktag, node);
/* add the chapters */
for (i = 0; i < book->chapters; i++) { for (i = 0; i < book->chapters; i++) {
chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo"); chaptertag = xmlNewNode(NULL, BAD_CAST "Capitulo");
xmlAddChild(booktag, chaptertag); xmlAddChild(booktag, chaptertag);
@ -55,6 +58,7 @@ int makexml() {
free(temp); free(temp);
/* add the verses */
chapter = book->chapter[i]; chapter = book->chapter[i];
chapter->current = 0; chapter->current = 0;
for (chapter->current = 0; chapter->current < chapter->verses; chapter->current++) { for (chapter->current = 0; chapter->current < chapter->verses; chapter->current++) {
@ -90,6 +94,4 @@ int makexml() {
xmlFreeDoc(doc); xmlFreeDoc(doc);
xmlCleanupParser(); xmlCleanupParser();
xmlMemoryDump(); xmlMemoryDump();
return 0;
} }

View File

@ -1 +1,4 @@
int makexml(); /*
* This function generates the xml and saves it to the output file.
*/
void makexml();

View File

@ -15,9 +15,9 @@ int readconfig(char *config_file) {
xmlNodePtr node = NULL; xmlNodePtr node = NULL;
xmlNodePtr subnode = NULL; xmlNodePtr subnode = NULL;
/* Initilize the library */ /* initilize the library */
LIBXML_TEST_VERSION LIBXML_TEST_VERSION
context = xmlNewParserCtxt(); context = xmlNewParserCtxt();
if (context == NULL) { if (context == NULL) {
fprintf(stderr, "No pudo alocar contexto de analizador!\n"); fprintf(stderr, "No pudo alocar contexto de analizador!\n");
@ -105,10 +105,10 @@ int readconfig(char *config_file) {
subnode = subnode->next; subnode = subnode->next;
} }
} }
node = node->next; node = node->next;
} }
xmlFreeDoc(config_xml); xmlFreeDoc(config_xml);
} }
@ -118,7 +118,7 @@ int readconfig(char *config_file) {
xmlFreeParserCtxt(context); xmlFreeParserCtxt(context);
return 1; return 1;
} }
xmlFreeParserCtxt(context); xmlFreeParserCtxt(context);
return 0; return 0;

View File

@ -1 +1,5 @@
/*
* This function reads the designated config file and save into int the
* config struct.
*/
int readconfig(char *config_file); int readconfig(char *config_file);

View File

@ -8,6 +8,10 @@
#include "readfile.h" #include "readfile.h"
#include "encoding.h" #include "encoding.h"
/*
* This function reads Biblia.txt, searches for the information in config,
* and stores it into an array of strings to be used later to create the xml.
*/
int readfile() { int readfile() {
FILE *file = NULL; FILE *file = NULL;
CHAPTER *chapter = NULL; CHAPTER *chapter = NULL;
@ -126,6 +130,7 @@ int readfile() {
} }
if (line != NULL) { if (line != NULL) {
if (strcmp(line, config->bible) == 0) { if (strcmp(line, config->bible) == 0) {
/* found the bible */
matches[0] = true; matches[0] = true;
matches[1] = false; matches[1] = false;
matches[2] = false; matches[2] = false;
@ -134,6 +139,7 @@ int readfile() {
#endif #endif
} }
if (strcmp(line, config->book) == 0) { if (strcmp(line, config->book) == 0) {
/* found the book */
matches[1] = true; matches[1] = true;
matches[2] = false; matches[2] = false;
#ifdef DEBUG #ifdef DEBUG
@ -145,6 +151,7 @@ int readfile() {
temp = (char *) malloc((length + 2) * sizeof(char)); temp = (char *) malloc((length + 2) * sizeof(char));
snprintf(temp, length + 2, "%s %d", config->chapter, i); snprintf(temp, length + 2, "%s %d", config->chapter, i);
if (strcmp(line, temp) == 0) { if (strcmp(line, temp) == 0) {
/* found a chapter */
matches[2] = true; matches[2] = true;
book->current++; book->current++;
book->chapter[book->current] = (CHAPTER *) malloc(sizeof(CHAPTER)); book->chapter[book->current] = (CHAPTER *) malloc(sizeof(CHAPTER));
@ -160,6 +167,7 @@ int readfile() {
free(temp); free(temp);
} }
if (matches[0] == true && matches[1] == true && matches[2] == true) { if (matches[0] == true && matches[1] == true && matches[2] == true) {
/* 3 matches, start getting the verses */
length = snprintf(NULL, 0, "%d", end + 1) + strlen(config->chapter); length = snprintf(NULL, 0, "%d", end + 1) + strlen(config->chapter);
temp = (char *) malloc((length + 2) * sizeof(char)); temp = (char *) malloc((length + 2) * sizeof(char));
snprintf(temp, length + 2, "%s %d", config->chapter, end + 1); snprintf(temp, length + 2, "%s %d", config->chapter, end + 1);
@ -215,6 +223,7 @@ int readfile() {
} }
} }
if (matches[0] == true && matches[1] == true && matches[2] == true && strcmp(line, "------------------------------------------------------------------------") == 0) { if (matches[0] == true && matches[1] == true && matches[2] == true && strcmp(line, "------------------------------------------------------------------------") == 0) {
/* end of a section and we already have 3 matches, no reason to continue */
#ifdef DEBUG #ifdef DEBUG
printf("Bible end match: %lu -> %s\n", (long) j + 1, line); printf("Bible end match: %lu -> %s\n", (long) j + 1, line);
#endif #endif

View File

@ -1,2 +1,11 @@
/*
* This variable controls how much memory is used to start with,
* mesured in bytes.
*/
#define MAX_LINES 100 #define MAX_LINES 100
/*
* This function reads Biblia.txt, searches for the information in config,
* and stores it into an array of strings to be used later to create the xml.
*/
int readfile(); int readfile();