Skip to content
Permalink
Browse files

Better Makefile. Took care of warnings. Fixed some memory leaks.

  • Loading branch information
halhen committed Apr 23, 2009
1 parent 8d89a80 commit 1a9d715749d92633cdf54f4fe1958485fa4ad0ca
Showing with 73 additions and 44 deletions.
  1. +12 −2 Makefile
  2. +20 −0 global.h
  3. +17 −10 main.c
  4. +14 −18 test.sh
  5. +10 −7 xmlhelpers.c
  6. +0 −7 xmlhelpers.h
@@ -1,2 +1,12 @@
all:
gcc `pkg-config --cflags --libs fuse` `xml2-config --cflags --libs` main.c xmlhelpers.c -o xmlfs
CC = gcc
CFLAGS = -O2 -Wall -W
OBJECTS = main.o xmlhelpers.o

all : $(OBJECTS)
$(CC) `pkg-config --libs fuse` `xml2-config --libs` $(OBJECTS) -o xmlfs -g

%.o : %.c
$(CC) $(CFLAGS) `pkg-config --cflags fuse` `xml2-config --cflags` -c $<

clean:
rm xmlfs $(OBJECTS)
@@ -0,0 +1,20 @@
/* Useful definitions used throughout xmlfs */

#ifndef __GLOBAL_H_INCLUDED__
#define __GLOBAL_H_INCLUDED__

#define INDEX_BASE 1
#define CONTENT_FILENAME "#text"

/* Macro to make compilers not warn about unused parameters
* http://sourcefrog.net/weblog/software/languages/C/unused.html */
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif

#endif
27 main.c
@@ -28,6 +28,7 @@
#include <libxml/parser.h>
#include <libxml/tree.h>

#include "global.h"
#include "xmlhelpers.h"


@@ -38,7 +39,6 @@ char* create_formatted_name(xmlElement* el)
{
int ia, ib, ic;
char* str;
xmlElement* xmltmp;

if (!el)
return NULL;
@@ -47,18 +47,18 @@ char* create_formatted_name(xmlElement* el)
{
count_twins(el, &ia, &ib);
ia += INDEX_BASE;
ic = (int)ceil(log10(ia));
ic = (int)floor(log10(ia)) + 1;

str = malloc(strlen(el->name) + 2 + ic);
str = malloc(strlen((char*)el->name) + 2 + ic);
sprintf(str, "%d.%s", ia, el->name);
}
else if (el->type == XML_TEXT_NODE) {
str = malloc(strlen(CONTENT_FILENAME) + 1);
strcpy(str, CONTENT_FILENAME);
}
else {
str = malloc(strlen(el->name) + 1);
strcpy(str, el->name);
str = malloc(strlen((char*)el->name) + 1);
strcpy(str, (char*)el->name);
}

return str;
@@ -101,7 +101,7 @@ static int xmlfs_getattr(const char* path, struct stat* stbuf)
}

static int xmlfs_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info* fi)
off_t UNUSED(offset), struct fuse_file_info* UNUSED(fi))
{
xmlAttr* attr;
xmlNode* node = (xmlNode*)findxmlelement(path, xml_rootparent);
@@ -141,7 +141,7 @@ static int xmlfs_open(const char *path, struct fuse_file_info *fi)
}

static int xmlfs_read(const char *path, char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
off_t offset, struct fuse_file_info *UNUSED(fi))
{
size_t len;
xmlElement *xel;
@@ -197,7 +197,7 @@ static struct fuse_opt xmlfs_opts[] = {
FUSE_OPT_END
};

static int xmlfs_opt_proc(void* data, const char* arg, int key, struct fuse_args *outargs)
static int xmlfs_opt_proc(void* UNUSED(data), const char* UNUSED(arg), int key, struct fuse_args *outargs)
{
switch(key) {
case KEY_HELP:
@@ -237,6 +237,7 @@ int main(int argc, char* argv[])
struct xmlfs_config conf;
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);

xmlDoc* xmldoc;
const char* xmlfile = "/dev/stdin";

memset(&conf, 0, sizeof(conf));
@@ -245,13 +246,16 @@ int main(int argc, char* argv[])
if (conf.xmlfile)
xmlfile = conf.xmlfile;

xmlDoc* xmldoc = xmlReadFile(xmlfile, NULL, 0);
xmldoc = xmlReadFile(xmlfile, NULL, 0);

if (xmldoc == NULL) {
fprintf(stderr, "Could not read XML\n");
return 1;
}

xml_rootparent = xmlNewNode(NULL, "xmlfs_root");
/* FIXME: better way of having root node as top directory
* This way causes some strange free's at exit according to valgrind */
xml_rootparent = xmlNewNode(NULL, (xmlChar*)"xmlfs_root");
if (!xmlAddChild(xml_rootparent, xmlDocGetRootElement(xmldoc))) {
fprintf(stderr, "Error creating XML node\n");
return 1;
@@ -261,6 +265,9 @@ int main(int argc, char* argv[])

xmlFreeDoc(xmldoc);
xmlCleanupParser();
if (conf.xmlfile)
free(conf.xmlfile);
fuse_opt_free_args(&args);

return ret;
}
32 test.sh
@@ -7,33 +7,29 @@ mkdir -p mnt/

# Do basic tests
cd mnt/
if [ `ls | wc -l` -ne 1 ]; then
echo "Wrong number of elements in root directory";
if [ `find ./ -name '*.node' | wc -l` -ne 2 ]; then
echo "Too many %d.nodes in .";
fi

if [ `find 1.root/ -name '*.node' | wc -l` -ne 2 ]; then
echo "Too many %d.nodes in 1.root";
if [ `find ./1.node/ -name 'attribute' | wc -l` -ne 1 ]; then
echo "attribute error in ./1.node/";
fi

if [ `find 1.root/1.node/ -name 'attribute' | wc -l` -ne 1 ]; then
echo "attribute error in 1.root/1.node/";
fi
if [ `find 1.root/1.node/ -name 'attribute2' | wc -l` -ne 0 ]; then
echo "attribute2 should not be in 1.root/1.node";
if [ `find ./1.node/ -name 'attribute2' | wc -l` -ne 0 ]; then
echo "attribute2 should not be in ./1.node";
fi
if [ `cat 1.root/1.node/attribute` != "attrib_value" ]; then
echo "1.root/1.node/attribute does not contain 'attrib_value'";
if [ `cat ./1.node/attribute` != "attrib_value" ]; then
echo "./1.node/attribute does not contain 'attrib_value'";
fi

if [ `find 1.root/2.node/ -name 'attribute' | wc -l` -ne 0 ]; then
echo "attribute should not be in 1.root/1.node/";
if [ `find ./2.node/ -name 'attribute' | wc -l` -ne 0 ]; then
echo "attribute should not be in ./1.node/";
fi
if [ `find 1.root/2.node/ -name 'attribute2' | wc -l` -ne 1 ]; then
echo "attribute2 error in 1.root/1.node";
if [ `find ./2.node/ -name 'attribute2' | wc -l` -ne 1 ]; then
echo "attribute2 error in ./1.node";
fi

if [ "`cat 1.root/1.textcontent/#text`" != "some text" ]; then
echo "Failed to read 1.root/1.textcontent/#text properly";
if [ "`cat ./1.textcontent/#text`" != "some text" ]; then
echo "Failed to read ./1.textcontent/#text properly";
fi

cd ..
@@ -9,6 +9,9 @@
* http://code.k2h.se
*/

#include <string.h>

#include "global.h"
#include "xmlhelpers.h"

xmlElement* _findxmlelement(const char* path, xmlNode* parent)
@@ -37,8 +40,8 @@ xmlElement* _findxmlelement(const char* path, xmlNode* parent)

/* Search nodes - which are indexed */
for (node = parent->children, counter = INDEX_BASE; node; node = node->next) {
len = strlen(node->name);
if (strncmp(node->name, path, len) == 0 &&
len = strlen((char*)node->name);
if (strncmp((char*)node->name, path, len) == 0 &&
(path[len] == '/' || path[len] == '\0')) {
if (counter == index)
break;
@@ -51,7 +54,7 @@ xmlElement* _findxmlelement(const char* path, xmlNode* parent)

/* No node found, try attributes - which aren't indexed */
for (attr = parent->properties; attr; attr = attr->next) {
if (strcmp(attr->name, path) == 0)
if (strcmp((char*)attr->name, path) == 0)
return (xmlElement*)attr;
}

@@ -76,10 +79,10 @@ char* node_value(xmlElement* xel)
return NULL;

if (xel->type == XML_ATTRIBUTE_NODE && xel->parent)
return xmlGetProp((xmlNode*)xel->parent, xel->name);
return (char*)xmlGetProp((xmlNode*)xel->parent, xel->name);

if (xel->type == XML_TEXT_NODE && xel->parent)
return xmlNodeGetContent((xmlNode*)xel->parent);
return (char*)xmlNodeGetContent((xmlNode*)xel->parent);

return NULL;
}
@@ -98,13 +101,13 @@ void count_twins(xmlElement* el, int *twins_before, int *twins_total)
return;

for (tmp = (xmlElement*)el->prev; tmp; tmp = (xmlElement*)tmp->prev) {
if (el->type == tmp->type && strcmp(el->name, tmp->name) == 0)
if (el->type == tmp->type && strcmp((char*)el->name, (char*)tmp->name) == 0)
before += 1;
}
total = before + 1;

for (tmp = (xmlElement*)el->next; tmp; tmp = (xmlElement*)tmp->next) {
if (el->type == tmp->type && strcmp(el->name, tmp->name) == 0)
if (el->type == tmp->type && strcmp((char*)el->name, (char*)tmp->name) == 0)
total += 1;
}

@@ -12,15 +12,8 @@
#ifndef __XMLHELPERS_H_INCLUDED__
#define __XMLHELPERS_H_INCLUDED__

#include <string.h>
#include <libxml/tree.h>

#define INDEX_BASE 1
#define CONTENT_FILENAME "#text"




char* node_value(xmlElement* xel);
void count_twins(xmlElement* el, int *twins_before, int *twins_total);
xmlElement* findxmlelement(const char *path, xmlNode *root);

0 comments on commit 1a9d715

Please sign in to comment.
You can’t perform that action at this time.