Jump to content

Todo list exporting as a odf spreadsheet

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
matio

matio

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
This is kind of a C problem. I've made a todo list program int C that works nicely, the next thing I want to do is export it as a ods file. My first problem is that the outputted file has weird characters in. My other problem is that the ods file is not valid (but I don't know if I'll get any help here about that).


Code:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <sqlite3.h>

#include "cdootlib.h"


int retval           = 0;

int q_cnt            = 5;

int q_size           = 150;

int ind              = 0;

char **queries       = 0;

sqlite3_stmt *stmt   = 0;

sqlite3      *handle = 0;



void setupSQLite(){

	queries = malloc(sizeof(char) * q_cnt * q_size);

	retval = sqlite3_open("todo.sqlite3", &handle);

	if(retval){

		printf("Database connection failed\n");

		exit(-1);

	}


	char create_table[200] = "CREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY, project TEXT, summary TEXT, priority INTEGER, done INTEGER);";

	sqlite3_exec(handle,create_table,0,0,0);

}


[B]void exportToODS(char *fn){

	system("unzip exampleODS.ods -d exampleODS > /dev/null");

	system("cp contentExample.xml exampleODS/content.xml");

	FILE *fp = fopen("exampleODS/content.xml", "a");

	char text[2000];

	

	queries[ind++] = "SELECT * FROM todos order by project, priority;";

	retval = sqlite3_prepare_v2(handle,queries[ind-1],-1,&stmt,0);

	if(retval){

	    printf("Selecting data from DB Failed\n");

		exit(-1);

	}

	int cols = sqlite3_column_count(stmt);


	char project[20];

	while(1){

		retval = sqlite3_step(stmt);

		if(retval == SQLITE_ROW){

			for(int col=0 ; col<cols;col++){

				const char *val = (const char*)sqlite3_column_text(stmt,col);

				char *cname = sqlite3_column_name(stmt,col);


				if(strcmp(cname, "project") == 0){

					if(strcmp(val, (const char*)project) != 0){

						strcpy(project, val);

						char stuff[300];

						sprintf(stuff, "<table:table-column table:style-name=\"co4\" table:default-cell-style-name=\"Default\"/><table:table-row table:style-name=\"ro1\"><table:table-cell table:style-name=\"ce1\" office:value-type=\"string\"><text:p>%s</text:p></table:table-cell><table:table-cell/></table:table-row>", project);

						strcat(text, stuff);

					}

					if(strcmp(sqlite3_column_text(stmt,col+2), "1") == 0){

						// red

					}else if(strcmp(sqlite3_column_text(stmt,col+2), "2") == 0){

						// yellow

					}else if(strcmp(sqlite3_column_text(stmt,col+2), "3") == 0){

						// green					

					}

				}else if(strcmp(cname, "id") != 0){

					if(strcmp(cname, "summary") == 0){

						// summary here

					}

				}

			}

		}

		else if(retval == SQLITE_DONE){

			break;

		}else{

			printf("Some error encountered\n");

			exit(-1);

		}

  }	

  strcat(text, "</table:table></office:spreadsheet></office:body></office:document-content>");

  fwrite(text, 1, strlen(text), fp);

  fclose(fp);

  printf("%s\n", text);

}[/B]


void killSQLite(){

	sqlite3_close(handle);

	free(queries);

}


contentExample.xml:

<?xml version="1.0" encoding="UTF-8"?>

<office:document-content

 xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"

  xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"

   xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"

   xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"

    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"

    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"

     xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc

     /elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"

      xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"

    xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"

     xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"

      xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"

       xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML"

        xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"

         xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc"

          xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" office:version="1.2" grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl"><office:scripts/><office:font-face-decls><style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss" style:font-pitch="variable"/><style:font-face style:name="DejaVu Sans" svg:font-family="'DejaVu Sans'" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-family-generic="system" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles><style:style style:name="co3" style:family="table-column"><style:table-column-properties fo:break-before="auto" style:column-width="0.8925in"/></style:style><style:style style:name="co2" style:family="table-column"><style:table-column-properties fo:break-before="auto" style:column-width="1.9909in"/></style:style><style:style style:name="co4" style:family="table-column"><style:table-column-properties fo:break-before="auto" style:column-width="3.5299in"/></style:style><style:style style:name="ro1" style:family="table-row"><style:table-row-properties style:row-height="0.178in" fo:break-before="auto" style:use-optimal-row-height="true"/></style:style><style:style style:name="ta1" style:family="table" style:master-page-name="Default"><style:table-properties table:display="true" style:writing-mode="lr-tb"/></style:style><style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#00ffff"/></style:style><style:style style:name="ce2" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#ff0000"/></style:style><style:style style:name="ce3" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#ffff00"/></style:style><style:style style:name="ce4" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#00ff00"/></style:style><style:style style:name="ta_extref" style:family="table"><style:table-properties table:display="false"/></style:style></office:automatic-styles>

<office:body><office:spreadsheet><table:table table:name="Sheet1" table:style-name="ta1" table:print="false"><table:table-column table:style-name="co2" table:default-cell-style-name="Default"/><table:table-column table:style-name="co4" table:default-cell-style-name="Default"/>


Outputted file:

<?xml version="1.0" encoding="UTF-8"?>

<office:document-content

 xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"

  xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"

   xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"

   xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"

    xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"

    xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"

     xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc

     /elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"

      xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"

    xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"

     xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"

      xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"

       xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML"

        xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"

         xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc"

          xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" xmlns:formx="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0" office:version="1.2" grddl:transformation="http://docs.oasis-open.org/office/1.2/xslt/odf2rdf.xsl"><office:scripts/><office:font-face-decls><style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss" style:font-pitch="variable"/><style:font-face style:name="DejaVu Sans" svg:font-family="'DejaVu Sans'" style:font-family-generic="system" style:font-pitch="variable"/><style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-family-generic="system" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles><style:style style:name="co3" style:family="table-column"><style:table-column-properties fo:break-before="auto" style:column-width="0.8925in"/></style:style><style:style style:name="co2" style:family="table-column"><style:table-column-properties fo:break-before="auto" style:column-width="1.9909in"/></style:style><style:style style:name="co4" style:family="table-column"><style:table-column-properties fo:break-before="auto" style:column-width="3.5299in"/></style:style><style:style style:name="ro1" style:family="table-row"><style:table-row-properties style:row-height="0.178in" fo:break-before="auto" style:use-optimal-row-height="true"/></style:style><style:style style:name="ta1" style:family="table" style:master-page-name="Default"><style:table-properties table:display="true" style:writing-mode="lr-tb"/></style:style><style:style style:name="ce1" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#00ffff"/></style:style><style:style style:name="ce2" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#ff0000"/></style:style><style:style style:name="ce3" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#ffff00"/></style:style><style:style style:name="ce4" style:family="table-cell" style:parent-style-name="Default"><style:table-cell-properties fo:background-color="#00ff00"/></style:style><style:style style:name="ta_extref" style:family="table"><style:table-properties table:display="false"/></style:style></office:automatic-styles>

<office:body><office:spreadsheet><table:table table:name="Sheet1" table:style-name="ta1" table:print="false"><table:table-column table:style-name="co2" table:default-cell-style-name="Default"/><table:table-column table:style-name="co4" table:default-cell-style-name="Default"/>�/�e<table:table-column table:style-name="co4" table:default-cell-style-name="Default"/><table:table-row table:style-name="ro1"><table:table-cell table:style-name="ce1" office:value-type="string"><text:p>Program this</text:p></table:table-cell><table:table-cell/></table:table-row></table:table></office:spreadsheet></office:body></office:document-content>


This is probably not the best place to post, and sorry for the long post. Any help is appreciated! :D

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
With the output you provided, it's kind of hard to see where the differences are, and therefor where the error might be. I've looked at ODF files before, so this is as good a place as any to see what's going on. One thought that comes to mind: ODF files are Unicode compliant. Depending on your output, you may be outputting ASCII that gets interpreted as Unicode characters.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog