コード例 #1
0
ファイル: file.c プロジェクト: dblchu/subsurface
static void parse_file_buffer(const char *filename, struct memblock *mem, GError **error)
{
	char *fmt = strrchr(filename, '.');
	if (fmt && open_by_filename(filename, fmt+1, mem, error))
		return;

	parse_xml_buffer(filename, mem->buffer, mem->size, error);
}
コード例 #2
0
ファイル: file.c プロジェクト: Acidburn0zzz/subsurface
static int parse_file_buffer(const char *filename, struct memblock *mem)
{
	int ret;
	char *fmt = strrchr(filename, '.');
	if (fmt && (ret = open_by_filename(filename, fmt + 1, mem)) != 0)
		return ret;

	if (!mem->size || !mem->buffer)
		return report_error("Out of memory parsing file %s\n", filename);

	return parse_xml_buffer(filename, mem->buffer, mem->size, &dive_table, NULL);
}
コード例 #3
0
ファイル: file.c プロジェクト: dblchu/subsurface
static void suunto_read(struct zip_file *file, GError **error)
{
	int size = 1024, n, read = 0;
	char *mem = malloc(size);

	while ((n = zip_fread(file, mem+read, size-read)) > 0) {
		read += n;
		size = read * 3 / 2;
		mem = realloc(mem, size);
	}
	parse_xml_buffer(_("SDE file"), mem, read, error);
	free(mem);
}
コード例 #4
0
ファイル: file.c プロジェクト: ihabunek/subsurface
static void zip_read(struct zip_file *file, GError **error, const char *filename)
{
	int size = 1024, n, read = 0;
	char *mem = malloc(size);

	while ((n = zip_fread(file, mem+read, size-read)) > 0) {
		read += n;
		size = read * 3 / 2;
		mem = realloc(mem, size);
	}
	mem[read] = 0;
	parse_xml_buffer(filename, mem, read, &dive_table, error);
	free(mem);
}
コード例 #5
0
ファイル: import-csv.c プロジェクト: glance-/subsurface
int parse_manual_file(const char *filename, char **params, int pnr)
{
	struct memblock mem;
	time_t now;
	struct tm *timep;
	char curdate[9];
	char curtime[6];
	int ret, i;


	time(&now);
	timep = localtime(&now);
	strftime(curdate, DATESTR, "%Y%m%d", timep);

	/* As the parameter is numeric, we need to ensure that the leading zero
	* is not discarded during the transform, thus prepend time with 1 */
	strftime(curtime, TIMESTR, "1%H%M", timep);


	params[pnr++] = strdup("date");
	params[pnr++] = strdup(curdate);
	params[pnr++] = strdup("time");
	params[pnr++] = strdup(curtime);
	params[pnr++] = NULL;

	if (filename == NULL)
		return report_error("No manual CSV filename");

	mem.size = 0;
	if (try_to_xslt_open_csv(filename, &mem, "manualCSV"))
		return -1;

#ifndef SUBSURFACE_MOBILE
	if (verbose >= 2) {
		fprintf(stderr, "(echo '<manualCSV>'; cat %s;echo '</manualCSV>') | xsltproc ", filename);
		for (i=0; params[i]; i+=2)
			fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]);
		fprintf(stderr, "%s/xslt/manualcsv2xml.xslt -\n", SUBSURFACE_SOURCE);
	}
#endif
	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);

	free(mem.buffer);
	for (i = 0; i < pnr - 2; ++i)
		free(params[i]);
	return ret;
}
コード例 #6
0
ファイル: file.c プロジェクト: Acidburn0zzz/subsurface
int parse_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int o2sensor1f, int o2sensor2f, int o2sensor3f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int setpointf, int sepidx, const char *csvtemplate, int unitidx)
{
	int ret, i;
	struct memblock mem;
	char *params[35];
	time_t now;
	struct tm *timep = NULL;
	int previous;

	/* Increase the limits for recursion and variables on XSLT
	 * parsing */
	xsltMaxDepth = 30000;
#if LIBXSLT_VERSION > 10126
	xsltMaxVars = 150000;
#endif

	if (timef >= MAXCOLS || depthf >= MAXCOLS || tempf >= MAXCOLS || po2f >= MAXCOLS || o2sensor1f >= MAXCOLS || o2sensor2f >= MAXCOLS || o2sensor3f >= MAXCOLS || cnsf >= MAXCOLS || ndlf >= MAXCOLS || cnsf >= MAXCOLS || stopdepthf >= MAXCOLS || pressuref >= MAXCOLS || setpointf >= MAXCOLS)
		return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);

	init_csv_file_parsing(params, &now, timep, timef, depthf, tempf, po2f, o2sensor1f, o2sensor2f, o2sensor3f, cnsf, ndlf, ttsf, stopdepthf, pressuref, setpointf, sepidx, csvtemplate, unitidx);

	if (filename == NULL)
		return report_error("No CSV filename");

	mem.size = 0;
	if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
		return -1;

	previous = dive_table.nr;
	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);

	free(mem.buffer);
	for (i = 0; params[i]; i += 2)
		free(params[i + 1]);

	return ret;
}
コード例 #7
0
ファイル: import-csv.c プロジェクト: glance-/subsurface
static int parse_seabear_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate)
{
	int ret, i;
	struct memblock mem;
	time_t now;
	struct tm *timep = NULL;
	char *ptr, *ptr_old = NULL;
	char *NL = NULL;
	char tmpbuf[MAXCOLDIGITS];

	/* Increase the limits for recursion and variables on XSLT
	 * parsing */
	xsltMaxDepth = 30000;
#if LIBXSLT_VERSION > 10126
	xsltMaxVars = 150000;
#endif

	time(&now);
	timep = localtime(&now);

	strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep);
	params[pnr++] = "date";
	params[pnr++] = strdup(tmpbuf);

	/* As the parameter is numeric, we need to ensure that the leading zero
	* is not discarded during the transform, thus prepend time with 1 */
	strftime(tmpbuf, MAXCOLDIGITS, "1%H%M", timep);
	params[pnr++] = "time";
	params[pnr++] = strdup(tmpbuf);


	if (filename == NULL)
		return report_error("No CSV filename");

	if (readfile(filename, &mem) < 0)
		return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);

	/* Determine NL (new line) character and the start of CSV data */
	ptr = mem.buffer;
	while ((ptr = strstr(ptr, "\r\n\r\n")) != NULL) {
		ptr_old = ptr;
		ptr += 1;
		NL = "\r\n";
	}

	if (!ptr_old) {
		ptr = mem.buffer;
		while ((ptr = strstr(ptr, "\n\n")) != NULL) {
			ptr_old = ptr;
			ptr += 1;
			NL = "\n";
		}
		ptr_old += 2;
	} else
		ptr_old += 4;

	/*
	 * If file does not contain empty lines, it is not a valid
	 * Seabear CSV file.
	 */
	if (NL == NULL)
		return -1;

	/*
	 * On my current sample of Seabear DC log file, the date is
	 * without any identifier. Thus we must search for the previous
	 * line and step through from there. That is the line after
	 * Serial number.
	 */
	ptr = strstr(mem.buffer, "Serial number:");
	if (ptr)
		ptr = strstr(ptr, NL);

	/*
	 * Write date and time values to params array, if available in
	 * the CSV header
	 */

	if (ptr) {
		ptr += strlen(NL) + 2;
		/*
		 * pnr is the index of NULL on the params as filled by
		 * the init function. The two last entries should be
		 * date and time. Here we overwrite them with the data
		 * from the CSV header.
		 */

		memcpy(params[pnr - 3], ptr, 4);
		memcpy(params[pnr - 3] + 4, ptr + 5, 2);
		memcpy(params[pnr - 3] + 6, ptr + 8, 2);
		params[pnr - 3][8] = 0;

		memcpy(params[pnr - 1] + 1, ptr + 11, 2);
		memcpy(params[pnr - 1] + 3, ptr + 14, 2);
		params[pnr - 1][5] = 0;
	}

	params[pnr++] = NULL;

	/* Move the CSV data to the start of mem buffer */
	memmove(mem.buffer, ptr_old, mem.size - (ptr_old - (char*)mem.buffer));
	mem.size = (int)mem.size - (ptr_old - (char*)mem.buffer);

	if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
		return -1;

	/*
	 * Lets print command line for manual testing with xsltproc if
	 * verbosity level is high enough. The printed line needs the
	 * input file added as last parameter.
	 */

	if (verbose >= 2) {
		fprintf(stderr, "xsltproc ");
		for (i=0; params[i]; i+=2)
			fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]);
		fprintf(stderr, "xslt/csv2xml.xslt\n");
	}

	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
	free(mem.buffer);
	for (i = 0; params[i]; i += 2)
		free(params[i + 1]);

	return ret;
}
コード例 #8
0
ファイル: import-csv.c プロジェクト: glance-/subsurface
int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate)
{
	int ret, i;
	struct memblock mem;
	time_t now;
	struct tm *timep = NULL;
	char tmpbuf[MAXCOLDIGITS];

	/* Increase the limits for recursion and variables on XSLT
	 * parsing */
	xsltMaxDepth = 30000;
#if LIBXSLT_VERSION > 10126
	xsltMaxVars = 150000;
#endif

	if (filename == NULL)
		return report_error("No CSV filename");

	mem.size = 0;
	if (!strcmp("DL7", csvtemplate)) {
		return parse_dan_format(filename, params, pnr);
	} else if (strcmp(params[0], "date")) {
		time(&now);
		timep = localtime(&now);

		strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep);
		params[pnr++] = "date";
		params[pnr++] = strdup(tmpbuf);

		/* As the parameter is numeric, we need to ensure that the leading zero
		 * is not discarded during the transform, thus prepend time with 1 */

		strftime(tmpbuf, MAXCOLDIGITS, "1%H%M", timep);
		params[pnr++] = "time";
		params[pnr++] = strdup(tmpbuf);
		params[pnr++] = NULL;
	}

	if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
		return -1;

	/*
	 * Lets print command line for manual testing with xsltproc if
	 * verbosity level is high enough. The printed line needs the
	 * input file added as last parameter.
	 */

#ifndef SUBSURFACE_MOBILE
	if (verbose >= 2) {
		fprintf(stderr, "(echo '<csv>'; cat %s;echo '</csv>') | xsltproc ", filename);
		for (i=0; params[i]; i+=2)
			fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]);
		fprintf(stderr, "%s/xslt/csv2xml.xslt -\n", SUBSURFACE_SOURCE);
	}
#endif
	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);

	free(mem.buffer);
	for (i = 0; params[i]; i += 2)
		free(params[i + 1]);

	return ret;
}
コード例 #9
0
ファイル: import-csv.c プロジェクト: glance-/subsurface
static int parse_dan_format(const char *filename, char **params, int pnr)
{
	int ret = 0, i;
	size_t end_ptr = 0;
	struct memblock mem, mem_csv;
	char tmpbuf[MAXCOLDIGITS];

	char *ptr = NULL;
	char *NL = NULL;
	char *iter = NULL;

	if (readfile(filename, &mem) < 0)
		return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);

	/* Determine NL (new line) character and the start of CSV data */
	if ((ptr = strstr(mem.buffer, "\r\n")) != NULL) {
		NL = "\r\n";
	} else if ((ptr = strstr(mem.buffer, "\n")) != NULL) {
		NL = "\n";
	} else {
		fprintf(stderr, "DEBUG: failed to detect NL\n");
		return -1;
	}

	while ((end_ptr < mem.size) && (ptr = strstr(mem.buffer + end_ptr, "ZDH"))) {
		char *iter_end = NULL;
		unsigned int pnr_local = pnr;

		/*
		 * Process the dives, but let the last round be parsed
		 * from C++ code
		 */

		if (end_ptr)
			process_dives(true, false);

		mem_csv.buffer = malloc(mem.size + 1);
		mem_csv.size = mem.size;

		iter = ptr + 4;
		iter = strchr(iter, '|');
		if (iter) {
			memcpy(tmpbuf, ptr + 4, iter - ptr - 4);
			tmpbuf[iter - ptr - 4] = 0;
			params[pnr_local++] = "diveNro";
			params[pnr_local++] = strdup(tmpbuf);
		}

		//fprintf(stderr, "DEBUG: BEGIN end_ptr %d round %d <%s>\n", end_ptr, j++, ptr);
		iter = ptr + 1;
		for (i = 0; i <= 4 && iter; ++i) {
			iter = strchr(iter, '|');
			if (iter)
				++iter;
		}

		if (!iter) {
			fprintf(stderr, "DEBUG: Data corrupt");
			return -1;
		}

		/* Setting date */
		memcpy(tmpbuf, iter, 8);
		tmpbuf[8] = 0;
		params[pnr_local++] = "date";
		params[pnr_local++] = strdup(tmpbuf);

		/* Setting time, gotta prepend it with 1 to
		 * avoid octal parsing (this is stripped out in
		 * XSLT */
		tmpbuf[0] = '1';
		memcpy(tmpbuf + 1, iter + 8, 6);
		tmpbuf[7] = 0;
		params[pnr_local++] = "time";
		params[pnr_local++] = strdup(tmpbuf);

		/* Air temperature */
		memset(tmpbuf, 0, sizeof(tmpbuf));
		iter = strchr(iter, '|');

		if (iter && iter + 1) {
			iter = iter + 1;
			iter_end = strchr(iter, '|');

			if (iter_end) {
				memcpy(tmpbuf, iter, iter_end - iter);
				params[pnr_local++] = "airTemp";
				params[pnr_local++] = strdup(tmpbuf);
			}
		}
		params[pnr_local] = NULL;

		/* Search for the next line */
		if (iter)
			iter = parse_dan_new_line(iter, NL);
		if (!iter)
			return -1;

		/* We got a trailer, no samples on this dive */
		if (strncmp(iter, "ZDT", 3) == 0) {
			end_ptr = iter - (char *)mem.buffer;

			/* Water temperature */
			memset(tmpbuf, 0, sizeof(tmpbuf));
			for (i = 0; i < 5 && iter; ++i)
				iter = strchr(iter + 1, '|');

			if (iter && iter + 1) {
				iter = iter + 1;
				iter_end = strchr(iter, '|');

				if (iter_end) {
					memcpy(tmpbuf, iter, iter_end - iter);
					params[pnr_local++] = "waterTemp";
					params[pnr_local++] = strdup(tmpbuf);
				}
			}
			params[pnr_local] = NULL;
			ret |= parse_xml_buffer(filename, "<csv></csv>", 11, &dive_table, (const char **)params);
			continue;
		}

		/* After ZDH we should get either ZDT (above) or ZDP */
		if (strncmp(iter, "ZDP{", 4) != 0) {
			fprintf(stderr, "DEBUG: Input appears to violate DL7 specification\n");
			end_ptr = iter - (char *)mem.buffer;
			continue;
		}

		if (ptr && ptr[4] == '}') {
			end_ptr += ptr - (char *)mem_csv.buffer;
			return report_error(translate("gettextFromC", "No dive profile found from '%s'"), filename);
		}

		if (ptr)
			ptr = parse_dan_new_line(ptr, NL);
		if (!ptr)
			return -1;

		end_ptr = ptr - (char *)mem.buffer;

		/* Copy the current dive data to start of mem_csv buffer */
		memcpy(mem_csv.buffer, ptr, mem.size - (ptr - (char *)mem.buffer));
		ptr = strstr(mem_csv.buffer, "ZDP}");
		if (ptr) {
			*ptr = 0;
		} else {
			fprintf(stderr, "DEBUG: failed to find end ZDP\n");
			return -1;
		}
		mem_csv.size = ptr - (char*)mem_csv.buffer;

		iter = parse_dan_new_line(ptr + 1, NL);
		if (iter && strncmp(iter, "ZDT", 3) == 0) {
			/* Water temperature */
			memset(tmpbuf, 0, sizeof(tmpbuf));
			for (i = 0; i < 5 && iter; ++i)
				iter = strchr(iter + 1, '|');

			if (iter && iter + 1) {
				iter = iter + 1;
				iter_end = strchr(iter, '|');

				if (iter_end) {
					memcpy(tmpbuf, iter, iter_end - iter);
					params[pnr_local++] = "waterTemp";
					params[pnr_local++] = strdup(tmpbuf);
				}
			}
			params[pnr_local] = NULL;
		}

		if (try_to_xslt_open_csv(filename, &mem_csv, "csv"))
			return -1;

		ret |= parse_xml_buffer(filename, mem_csv.buffer, mem_csv.size, &dive_table, (const char **)params);
		end_ptr += ptr - (char *)mem_csv.buffer;
		free(mem_csv.buffer);
	}

	free(mem.buffer);
	for (i = 0; params[i]; i += 2)
		free(params[i + 1]);

	return ret;
}
コード例 #10
0
ファイル: file.c プロジェクト: Acidburn0zzz/subsurface
int parse_seabear_csv_file(const char *filename, int timef, int depthf, int tempf, int po2f, int o2sensor1f, int o2sensor2f, int o2sensor3f, int cnsf, int ndlf, int ttsf, int stopdepthf, int pressuref, int sepidx, const char *csvtemplate, int unitidx, const char *delta)
{
	int ret, i, pnr;
	struct memblock mem;
	char *params[SBPARAMS];
	char deltabuf[MAXCOLDIGITS];
	time_t now;
	struct tm *timep = NULL;
	char *ptr, *ptr_old = NULL;
	char *NL = NULL;

	/* Increase the limits for recursion and variables on XSLT
	 * parsing */
	xsltMaxDepth = 30000;
#if LIBXSLT_VERSION > 10126
	xsltMaxVars = 150000;
#endif

	if (timef >= MAXCOLS || depthf >= MAXCOLS || tempf >= MAXCOLS || po2f >= MAXCOLS || o2sensor1f >= MAXCOLS || o2sensor2f >= MAXCOLS || o2sensor3f >= MAXCOLS || cnsf >= MAXCOLS || ndlf >= MAXCOLS || cnsf >= MAXCOLS || stopdepthf >= MAXCOLS || pressuref >= MAXCOLS)
		return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);

	pnr = init_csv_file_parsing(params, &now, timep, timef, depthf, tempf, po2f, o2sensor1f, o2sensor2f, o2sensor3f, cnsf, ndlf, ttsf, stopdepthf, pressuref, -1, sepidx, csvtemplate, unitidx);

	if (filename == NULL)
		return report_error("No CSV filename");

	if (readfile(filename, &mem) < 0)
		return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);

	/* Determine NL (new line) character and the start of CSV data */
	ptr = mem.buffer;
	while ((ptr = strstr(ptr, "\r\n\r\n")) != NULL) {
		ptr_old = ptr;
		ptr += 1;
		NL = "\r\n";
	}

	if (!ptr_old) {
		ptr = mem.buffer;
		while ((ptr = strstr(ptr, "\n\n")) != NULL) {
			ptr_old = ptr;
			ptr += 1;
			NL = "\n";
		}
		ptr_old += 2;
	} else
		ptr_old += 4;

	/*
	 * If file does not contain empty lines, it is not a valid
	 * Seabear CSV file.
	 */
	if (NL == NULL)
		return -1;

	/*
	 * On my current sample of Seabear DC log file, the date is
	 * without any identifier. Thus we must search for the previous
	 * line and step through from there. That is the line after
	 * Serial number.
	 */
	ptr = strstr(mem.buffer, "Serial number:");
	if (ptr)
		ptr = strstr(ptr, NL);

	/*
	 * Write date and time values to params array, if available in
	 * the CSV header
	 */

	if (ptr) {
		ptr += strlen(NL) + 2;
		/*
		 * pnr is the index of NULL on the params as filled by
		 * the init function. The two last entries should be
		 * date and time. Here we overwrite them with the data
		 * from the CSV header.
		 */

		memcpy(params[pnr - 3], ptr, 4);
		memcpy(params[pnr - 3] + 4, ptr + 5, 2);
		memcpy(params[pnr - 3] + 6, ptr + 8, 2);
		params[pnr - 3][8] = 0;

		memcpy(params[pnr - 1] + 1, ptr + 11, 2);
		memcpy(params[pnr - 1] + 3, ptr + 14, 2);
		params[pnr - 1][5] = 0;
	}

	snprintf(deltabuf, MAXCOLDIGITS, "%s", delta);
	params[pnr++] = "delta";
	params[pnr++] = strdup(deltabuf);
	params[pnr++] = NULL;

	/* Move the CSV data to the start of mem buffer */
	memmove(mem.buffer, ptr_old, mem.size - (ptr_old - (char*)mem.buffer));
	mem.size = (int)mem.size - (ptr_old - (char*)mem.buffer);

	if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
		return -1;

	/*
	 * Lets print command line for manual testing with xsltproc if
	 * verbosity level is high enough. The printed line needs the
	 * input file added as last parameter.
	 */

	if (verbose >= 2) {
		fprintf(stderr, "xsltproc ");
		for (i=0; params[i]; i+=2)
			fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]);
		fprintf(stderr, "xslt/csv2xml.xslt\n");
	}

	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);
	free(mem.buffer);
	for (i = 0; params[i]; i += 2)
		free(params[i + 1]);

	return ret;
}
コード例 #11
0
ファイル: file.c プロジェクト: Acidburn0zzz/subsurface
int parse_manual_file(const char *filename, int sepidx, int units, int dateformat, int durationformat,
		      int numberf, int datef, int timef, int durationf, int locationf, int gpsf, int maxdepthf, int meandepthf,
		      int divemasterf, int buddyf, int suitf, int notesf, int weightf, int tagsf, int cylsizef, int startpresf, int endpresf,
		      int o2f, int hef, int airtempf, int watertempf)
{
	if (verbose > 4) {
		fprintf(stderr, "filename %s, sepidx %d, units %d, dateformat %d, durationformat %d\n", filename, sepidx, units, dateformat, durationformat);
		fprintf(stderr, "numberf %d, datef %d, timef %d, durationf %d, locationf %d, gpsf %d, maxdepthf %d, meandepthf %d\n", numberf, datef, timef, durationf, locationf, gpsf, maxdepthf, meandepthf);
		fprintf(stderr, "divemasterf %d, buddyf %d, suitf %d, notesf %d, weightf %d, tagsf %d, cylsizef %d, startpresf %d, endpresf %d\n", divemasterf, buddyf, suitf, notesf, weightf, tagsf, cylsizef, startpresf, endpresf);
		fprintf(stderr, "o2f %d, hef %d, airtempf %d, watertempf %d\n", o2f, hef, airtempf, watertempf);
	}
	struct memblock mem;
	int pnr = 0;
	char *params[55];
	char numberbuf[MAXCOLDIGITS];
	char datebuf[MAXCOLDIGITS];
	char timebuf[MAXCOLDIGITS];
	char durationbuf[MAXCOLDIGITS];
	char locationbuf[MAXCOLDIGITS];
	char gpsbuf[MAXCOLDIGITS];
	char maxdepthbuf[MAXCOLDIGITS];
	char meandepthbuf[MAXCOLDIGITS];
	char divemasterbuf[MAXCOLDIGITS];
	char buddybuf[MAXCOLDIGITS];
	char suitbuf[MAXCOLDIGITS];
	char notesbuf[MAXCOLDIGITS];
	char weightbuf[MAXCOLDIGITS];
	char tagsbuf[MAXCOLDIGITS];
	char separator_index[MAXCOLDIGITS];
	char unit[MAXCOLDIGITS];
	char datefmt[MAXCOLDIGITS];
	char durationfmt[MAXCOLDIGITS];
	char cylsizebuf[MAXCOLDIGITS];
	char startpresbuf[MAXCOLDIGITS];
	char endpresbuf[MAXCOLDIGITS];
	char o2buf[MAXCOLDIGITS];
	char hebuf[MAXCOLDIGITS];
	char airtempbuf[MAXCOLDIGITS];
	char watertempbuf[MAXCOLDIGITS];
	time_t now;
	struct tm *timep;
	char curdate[9];
	char curtime[6];
	int ret;

	if (numberf >= MAXCOLS || datef >= MAXCOLS || timef >= MAXCOLS || durationf >= MAXCOLS || locationf >= MAXCOLS || gpsf >= MAXCOLS || maxdepthf >= MAXCOLS || meandepthf >= MAXCOLS || buddyf >= MAXCOLS || suitf >= MAXCOLS || notesf >= MAXCOLS || weightf >= MAXCOLS || tagsf >= MAXCOLS || cylsizef >= MAXCOLS || startpresf >= MAXCOLS || endpresf >= MAXCOLS || o2f >= MAXCOLS || hef >= MAXCOLS || airtempf >= MAXCOLS || watertempf >= MAXCOLS)
		return report_error(translate("gettextFromC", "Maximum number of supported columns on CSV import is %d"), MAXCOLS);

	snprintf(numberbuf, MAXCOLDIGITS, "%d", numberf);
	snprintf(datebuf, MAXCOLDIGITS, "%d", datef);
	snprintf(timebuf, MAXCOLDIGITS, "%d", timef);
	snprintf(durationbuf, MAXCOLDIGITS, "%d", durationf);
	snprintf(locationbuf, MAXCOLDIGITS, "%d", locationf);
	snprintf(gpsbuf, MAXCOLDIGITS, "%d", gpsf);
	snprintf(maxdepthbuf, MAXCOLDIGITS, "%d", maxdepthf);
	snprintf(meandepthbuf, MAXCOLDIGITS, "%d", meandepthf);
	snprintf(divemasterbuf, MAXCOLDIGITS, "%d", divemasterf);
	snprintf(buddybuf, MAXCOLDIGITS, "%d", buddyf);
	snprintf(suitbuf, MAXCOLDIGITS, "%d", suitf);
	snprintf(notesbuf, MAXCOLDIGITS, "%d", notesf);
	snprintf(weightbuf, MAXCOLDIGITS, "%d", weightf);
	snprintf(tagsbuf, MAXCOLDIGITS, "%d", tagsf);
	snprintf(separator_index, MAXCOLDIGITS, "%d", sepidx);
	snprintf(unit, MAXCOLDIGITS, "%d", units);
	snprintf(datefmt, MAXCOLDIGITS, "%d", dateformat);
	snprintf(durationfmt, MAXCOLDIGITS, "%d", durationformat);
	snprintf(cylsizebuf, MAXCOLDIGITS, "%d", cylsizef);
	snprintf(startpresbuf, MAXCOLDIGITS, "%d", startpresf);
	snprintf(endpresbuf, MAXCOLDIGITS, "%d", endpresf);
	snprintf(o2buf, MAXCOLDIGITS, "%d", o2f);
	snprintf(hebuf, MAXCOLDIGITS, "%d", hef);
	snprintf(airtempbuf, MAXCOLDIGITS, "%d", airtempf);
	snprintf(watertempbuf, MAXCOLDIGITS, "%d", watertempf);
	time(&now);
	timep = localtime(&now);
	strftime(curdate, DATESTR, "%Y%m%d", timep);

	/* As the parameter is numeric, we need to ensure that the leading zero
	* is not discarded during the transform, thus prepend time with 1 */
	strftime(curtime, TIMESTR, "1%H%M", timep);

	params[pnr++] = "numberField";
	params[pnr++] = numberbuf;
	params[pnr++] = "dateField";
	params[pnr++] = datebuf;
	params[pnr++] = "timeField";
	params[pnr++] = timebuf;
	params[pnr++] = "durationField";
	params[pnr++] = durationbuf;
	params[pnr++] = "locationField";
	params[pnr++] = locationbuf;
	params[pnr++] = "gpsField";
	params[pnr++] = gpsbuf;
	params[pnr++] = "maxDepthField";
	params[pnr++] = maxdepthbuf;
	params[pnr++] = "meanDepthField";
	params[pnr++] = meandepthbuf;
	params[pnr++] = "divemasterField";
	params[pnr++] = divemasterbuf;
	params[pnr++] = "buddyField";
	params[pnr++] = buddybuf;
	params[pnr++] = "suitField";
	params[pnr++] = suitbuf;
	params[pnr++] = "notesField";
	params[pnr++] = notesbuf;
	params[pnr++] = "weightField";
	params[pnr++] = weightbuf;
	params[pnr++] = "tagsField";
	params[pnr++] = tagsbuf;
	params[pnr++] = "date";
	params[pnr++] = curdate;
	params[pnr++] = "time";
	params[pnr++] = curtime;
	params[pnr++] = "separatorIndex";
	params[pnr++] = separator_index;
	params[pnr++] = "units";
	params[pnr++] = unit;
	params[pnr++] = "datefmt";
	params[pnr++] = datefmt;
	params[pnr++] = "durationfmt";
	params[pnr++] = durationfmt;
	params[pnr++] = "cylindersizeField";
	params[pnr++] = cylsizebuf;
	params[pnr++] = "startpressureField";
	params[pnr++] = startpresbuf;
	params[pnr++] = "endpressureField";
	params[pnr++] = endpresbuf;
	params[pnr++] = "o2Field";
	params[pnr++] = o2buf;
	params[pnr++] = "heField";
	params[pnr++] = hebuf;
	params[pnr++] = "airtempField";
	params[pnr++] = airtempbuf;
	params[pnr++] = "watertempField";
	params[pnr++] = watertempbuf;
	params[pnr++] = NULL;

	if (filename == NULL)
		return report_error("No manual CSV filename");

	mem.size = 0;
	if (try_to_xslt_open_csv(filename, &mem, "manualCSV"))
		return -1;

	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);

	free(mem.buffer);
	return ret;
}
コード例 #12
0
ファイル: file.c プロジェクト: abentley/subsurface
int parse_csv_file(const char *filename, char **params, int pnr, const char *csvtemplate)
{
	int ret, i;
	struct memblock mem;
	time_t now;
	struct tm *timep = NULL;
	char tmpbuf[MAXCOLDIGITS];

	/* Increase the limits for recursion and variables on XSLT
	 * parsing */
	xsltMaxDepth = 30000;
#if LIBXSLT_VERSION > 10126
	xsltMaxVars = 150000;
#endif

	if (filename == NULL)
		return report_error("No CSV filename");

	mem.size = 0;
	if (!strcmp("DL7", csvtemplate)) {
		char *ptr = NULL;
		char *NL = NULL;
		char *iter = NULL;

		csvtemplate = "csv";
		if (readfile(filename, &mem) < 0)
			return report_error(translate("gettextFromC", "Failed to read '%s'"), filename);

		/* Determine NL (new line) character and the start of CSV data */
		if ((ptr = strstr(mem.buffer, "\r\n")) != NULL) {
			NL = "\r\n";
		} else if ((ptr = strstr(mem.buffer, "\n")) != NULL) {
			NL = "\n";
		} else {
			fprintf(stderr, "DEBUG: failed to detect NL\n");
			return -1;
		}

		ptr = strstr(mem.buffer, "ZDH");
		if (ptr) {
			iter = ptr + 1;
			for (i = 0; i <= 4 && iter; ++i) {
				iter = strchr(iter, '|');
				if (iter)
					++iter;
			}

			/* Setting date */
			memcpy(tmpbuf, iter, 8);
			tmpbuf[8] = 0;
			params[pnr++] = "date";
			params[pnr++] = strdup(tmpbuf);

			/* Setting time, gotta prepend it with 1 to
			 * avoid octal parsing (this is stripped out in
			 * XSLT */
			tmpbuf[0] = '1';
			memcpy(tmpbuf + 1, iter + 8, 6);
			tmpbuf[7] = 0;
			params[pnr++] = "time";
			params[pnr++] = strdup(tmpbuf);
			params[pnr++] = NULL;
		}

		ptr = strstr(mem.buffer, "ZDP");
		if (ptr)
			ptr = strstr(ptr, NL);
		if (ptr)
			ptr += strlen(NL);

		/* Move the CSV data to the start of mem buffer */
		memmove(mem.buffer, ptr, mem.size - (ptr - (char*)mem.buffer));
		ptr = strstr(mem.buffer, "ZDP");
		if (ptr) {
			*ptr = 0;
		} else {
			fprintf(stderr, "DEBUG: failed to find end ZDP\n");
			return -1;
		}
		mem.size = ptr - (char*)mem.buffer;
	} else if (strcmp(params[0], "date")) {
		time(&now);
		timep = localtime(&now);

		strftime(tmpbuf, MAXCOLDIGITS, "%Y%m%d", timep);
		params[pnr++] = "date";
		params[pnr++] = strdup(tmpbuf);

		/* As the parameter is numeric, we need to ensure that the leading zero
		 * is not discarded during the transform, thus prepend time with 1 */

		strftime(tmpbuf, MAXCOLDIGITS, "1%H%M", timep);
		params[pnr++] = "time";
		params[pnr++] = strdup(tmpbuf);
		params[pnr++] = NULL;
	}

	if (try_to_xslt_open_csv(filename, &mem, csvtemplate))
		return -1;

	/*
	 * Lets print command line for manual testing with xsltproc if
	 * verbosity level is high enough. The printed line needs the
	 * input file added as last parameter.
	 */

#ifndef SUBSURFACE_MOBILE
	if (verbose >= 2) {
		fprintf(stderr, "(echo '<csv>'; cat %s;echo '</csv>') | xsltproc ", filename);
		for (i=0; params[i]; i+=2)
			fprintf(stderr, "--stringparam %s %s ", params[i], params[i+1]);
		fprintf(stderr, "%s/xslt/csv2xml.xslt -\n", SUBSURFACE_SOURCE);
	}
#endif
	ret = parse_xml_buffer(filename, mem.buffer, mem.size, &dive_table, (const char **)params);

	free(mem.buffer);
	for (i = 0; params[i]; i += 2)
		free(params[i + 1]);

	return ret;
}