7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
14 struct dive_table dive_table;
17 * Add a dive into the dive_table array
19 static void record_dive(struct dive *dive)
21 int nr = dive_table.nr, allocated = dive_table.allocated;
22 struct dive **dives = dive_table.dives;
24 if (nr >= allocated) {
25 allocated = (nr + 32) * 3 / 2;
26 dives = realloc(dives, allocated * sizeof(struct dive *));
29 dive_table.dives = dives;
30 dive_table.allocated = allocated;
32 dives[nr] = fixup_dive(dive);
36 static void start_match(const char *type, const char *name, char *buffer)
39 printf("Matching %s '%s' (%s)\n",
43 static void nonmatch(const char *type, const char *name, char *buffer)
46 printf("Unable to match %s '%s' (%s)\n",
51 typedef void (*matchfn_t)(char *buffer, void *);
53 static int match(const char *pattern, int plen,
54 const char *name, int nlen,
55 matchfn_t fn, char *buf, void *data)
59 if (memcmp(pattern, name + nlen - plen, plen))
66 * We keep our internal data in well-specified units, but
67 * the input may come in some random format. This keeps track
68 * of the incoming units.
71 enum { METERS, FEET } length;
72 enum { LITER, CUFT } volume;
73 enum { BAR, PSI } pressure;
74 enum { CELSIUS, FAHRENHEIT } temperature;
75 enum { KG, LBS } weight;
78 /* We're going to default to SI units for input */
79 static const struct units SI_units = {
83 .temperature = CELSIUS,
88 * Dive info as it is being built up..
90 static int alloc_samples;
91 static struct dive *dive;
92 static struct sample *sample;
94 static int event_index, cylinder_index;
96 static enum import_source {
104 static time_t utc_mktime(struct tm *tm)
106 static const int mdays[] = {
107 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
109 int year = tm->tm_year;
110 int month = tm->tm_mon;
111 int day = tm->tm_mday;
113 /* First normalize relative to 1900 */
116 else if (year > 1900)
119 /* Normalized to Jan 1, 1970: unix time */
122 if (year < 0 || year > 129) /* algo only works for 1970-2099 */
124 if (month < 0 || month > 11) /* array bounds */
126 if (month < 2 || (year + 2) % 4)
128 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_sec < 0)
130 return (year * 365 + (year + 1) / 4 + mdays[month] + day) * 24*60*60UL +
131 tm->tm_hour * 60*60 + tm->tm_min * 60 + tm->tm_sec;
134 static void divedate(char *buffer, void *_when)
137 time_t *when = _when;
140 success = tm.tm_sec | tm.tm_min | tm.tm_hour;
141 if (sscanf(buffer, "%d.%d.%d", &d, &m, &y) == 3) {
145 } else if (sscanf(buffer, "%d-%d-%d", &y, &m, &d) == 3) {
150 fprintf(stderr, "Unable to parse date '%s'\n", buffer);
155 *when = utc_mktime(&tm);
160 static void divetime(char *buffer, void *_when)
163 time_t *when = _when;
165 if (sscanf(buffer, "%d:%d:%d", &h, &m, &s) >= 2) {
170 *when = utc_mktime(&tm);
175 /* Libdivecomputer: "2011-03-20 10:22:38" */
176 static void divedatetime(char *buffer, void *_when)
180 time_t *when = _when;
182 if (sscanf(buffer, "%d-%d-%d %d:%d:%d",
183 &y, &m, &d, &hr, &min, &sec) == 6) {
190 *when = utc_mktime(&tm);
204 static enum number_type integer_or_float(char *buffer, union int_or_float *res)
210 /* Integer or floating point? */
211 val = strtol(buffer, &end, 10);
212 if (val < 0 || end == buffer)
215 /* Looks like it might be floating point? */
218 fp = strtod(buffer, &end);
229 static void pressure(char *buffer, void *_press)
232 pressure_t *pressure = _press;
233 union int_or_float val;
235 switch (integer_or_float(buffer, &val)) {
237 /* Just ignore zero values */
240 switch (units.pressure) {
242 /* Assume mbar, but if it's really small, it's bar */
248 mbar = val.fp * 68.95;
251 if (mbar > 5 && mbar < 500000) {
252 pressure->mbar = mbar + 0.5;
257 printf("Strange pressure reading %s\n", buffer);
262 static void depth(char *buffer, void *_depth)
264 depth_t *depth = _depth;
265 union int_or_float val;
267 switch (integer_or_float(buffer, &val)) {
269 switch (units.length) {
271 depth->mm = val.fp * 1000 + 0.5;
274 depth->mm = val.fp * 304.8 + 0.5;
279 printf("Strange depth reading %s\n", buffer);
284 static void temperature(char *buffer, void *_temperature)
286 temperature_t *temperature = _temperature;
287 union int_or_float val;
289 switch (integer_or_float(buffer, &val)) {
291 /* Ignore zero. It means "none" */
295 switch (units.temperature) {
297 temperature->mkelvin = (val.fp + 273.15) * 1000 + 0.5;
300 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
305 printf("Strange temperature reading %s\n", buffer);
310 static void sampletime(char *buffer, void *_time)
314 duration_t *time = _time;
316 i = sscanf(buffer, "%d:%d", &min, &sec);
323 time->seconds = sec + min*60;
326 printf("Strange sample time reading %s\n", buffer);
331 static void duration(char *buffer, void *_time)
333 sampletime(buffer, _time);
336 static void percent(char *buffer, void *_fraction)
338 fraction_t *fraction = _fraction;
339 union int_or_float val;
341 switch (integer_or_float(buffer, &val)) {
344 fraction->permille = val.fp * 10 + 0.5;
348 printf("Strange percentage reading %s\n", buffer);
354 static void gasmix(char *buffer, void *_fraction)
356 /* libdivecomputer does negative percentages. */
359 if (cylinder_index < MAX_CYLINDERS)
360 percent(buffer, _fraction);
363 static void gasmix_nitrogen(char *buffer, void *_gasmix)
365 /* Ignore n2 percentages. There's no value in them. */
368 static void cylindersize(char *buffer, void *_volume)
370 volume_t *volume = _volume;
371 union int_or_float val;
373 switch (integer_or_float(buffer, &val)) {
375 volume->mliter = val.fp * 1000 + 0.5;
379 printf("Strange volume reading %s\n", buffer);
385 static void utf8_string(char *buffer, void *_res)
387 *(char **)_res = buffer;
391 * Uemis water_pressure. In centibar. And when converting to
392 * depth, I'm just going to always use saltwater, because I
393 * think "true depth" is just stupid. From a diving standpoint,
394 * "true depth" is pretty much completely pointless, unless
395 * you're doing some kind of underwater surveying work.
397 * So I give water depths in "pressure depth", always assuming
398 * salt water. So one atmosphere per 10m.
400 static void water_pressure(char *buffer, void *_depth)
402 depth_t *depth = _depth;
403 union int_or_float val;
406 switch (integer_or_float(buffer, &val)) {
411 atm = (val.fp / 100) / 1.01325;
413 * atm to cm. Why not mm? The precision just isn't
416 cm = 100 * (atm - 1) + 0.5;
418 depth->mm = 10 * (long)cm;
422 fprintf(stderr, "Strange water pressure '%s'\n", buffer);
427 #define MATCH(pattern, fn, dest) \
428 match(pattern, strlen(pattern), name, len, fn, buf, dest)
430 static void get_index(char *buffer, void *_i)
437 static void centibar(char *buffer, void *_pressure)
439 pressure_t *pressure = _pressure;
440 union int_or_float val;
442 switch (integer_or_float(buffer, &val)) {
444 pressure->mbar = val.fp * 10 + 0.5;
447 fprintf(stderr, "Strange centibar pressure '%s'\n", buffer);
452 static void decicelsius(char *buffer, void *_temp)
454 temperature_t *temp = _temp;
455 union int_or_float val;
457 switch (integer_or_float(buffer, &val)) {
459 temp->mkelvin = (val.fp/10 + 273.15) * 1000 + 0.5;
462 fprintf(stderr, "Strange julian date: %s", buffer);
467 static int uemis_fill_sample(struct sample *sample, const char *name, int len, char *buf)
469 return MATCH(".reading.dive_time", sampletime, &sample->time) ||
470 MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
471 MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
472 MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
473 MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
478 * Divinglog is crazy. The temperatures are in celsius. EXCEPT
479 * for the sample temperatures, that are in Fahrenheit.
482 static void fahrenheit(char *buffer, void *_temperature)
484 temperature_t *temperature = _temperature;
485 union int_or_float val;
487 switch (integer_or_float(buffer, &val)) {
489 temperature->mkelvin = (val.fp + 459.67) * 5000/9;
492 fprintf(stderr, "Crazy Diving Log temperature reading %s\n", buffer);
497 static int divinglog_fill_sample(struct sample *sample, const char *name, int len, char *buf)
499 return MATCH(".p.time", sampletime, &sample->time) ||
500 MATCH(".p.depth", depth, &sample->depth) ||
501 MATCH(".p.temp", fahrenheit, &sample->temperature) ||
502 MATCH(".p.press1", pressure, &sample->cylinderpressure) ||
506 /* We're in samples - try to convert the random xml value to something useful */
507 static void try_to_fill_sample(struct sample *sample, const char *name, char *buf)
509 int len = strlen(name);
511 start_match("sample", name, buf);
512 if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
514 if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
516 if (MATCH(".sample.depth", depth, &sample->depth))
518 if (MATCH(".sample.temp", temperature, &sample->temperature))
520 if (MATCH(".sample.temperature", temperature, &sample->temperature))
522 if (MATCH(".sample.sampletime", sampletime, &sample->time))
524 if (MATCH(".sample.time", sampletime, &sample->time))
527 switch (import_source) {
529 if (uemis_fill_sample(sample, name, len, buf))
534 if (divinglog_fill_sample(sample, name, len, buf))
542 nonmatch("sample", name, buf);
546 * Crazy suunto xml. Look at how those o2/he things match up.
548 static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
550 return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
551 MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
552 MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
553 MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
554 MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
555 MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
556 MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
557 MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
558 MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
559 MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
563 static const char *country, *city;
565 static void divinglog_place(char *place, void *_location)
567 char **location = _location;
568 char buffer[256], *p;
571 len = snprintf(buffer, sizeof(buffer),
577 country ? country : "");
580 memcpy(p, buffer, len+1);
587 static int divinglog_dive_match(struct dive *dive, const char *name, int len, char *buf)
589 return MATCH(".divedate", divedate, &dive->when) ||
590 MATCH(".entrytime", divetime, &dive->when) ||
591 MATCH(".depth", depth, &dive->maxdepth) ||
592 MATCH(".tanksize", cylindersize, &dive->cylinder[0].type.size) ||
593 MATCH(".tanktype", utf8_string, &dive->cylinder[0].type.description) ||
594 MATCH(".comments", utf8_string, &dive->notes) ||
595 MATCH(".country.name", utf8_string, &country) ||
596 MATCH(".city.name", utf8_string, &city) ||
597 MATCH(".place.name", divinglog_place, &dive->location) ||
601 static int buffer_value(char *buffer)
603 int val = atoi(buffer);
608 static void uemis_length_unit(char *buffer, void *_unused)
610 units.length = buffer_value(buffer) ? FEET : METERS;
613 static void uemis_volume_unit(char *buffer, void *_unused)
615 units.volume = buffer_value(buffer) ? CUFT : LITER;
618 static void uemis_pressure_unit(char *buffer, void *_unused)
621 units.pressure = buffer_value(buffer) ? PSI : BAR;
625 static void uemis_temperature_unit(char *buffer, void *_unused)
627 units.temperature = buffer_value(buffer) ? FAHRENHEIT : CELSIUS;
630 static void uemis_weight_unit(char *buffer, void *_unused)
632 units.weight = buffer_value(buffer) ? LBS : KG;
635 static void uemis_time_unit(char *buffer, void *_unused)
639 static void uemis_date_unit(char *buffer, void *_unused)
643 /* Modified julian day, yay! */
644 static void uemis_date_time(char *buffer, void *_when)
646 time_t *when = _when;
647 union int_or_float val;
649 switch (integer_or_float(buffer, &val)) {
651 *when = (val.fp - 40587.5) * 86400;
654 fprintf(stderr, "Strange julian date: %s", buffer);
660 * Uemis doesn't know time zones. You need to do them as
661 * minutes, not hours.
663 * But that's ok, we don't track timezones yet either. We
664 * just turn everything into "localtime expressed as UTC".
666 static void uemis_time_zone(char *buffer, void *_when)
668 time_t *when = _when;
669 signed char tz = atoi(buffer);
674 /* 0 - air ; 1 - nitrox1 ; 2 - nitrox2 ; 3 = nitrox3 */
675 static int uemis_gas_template;
678 * Christ. Uemis tank data is a total mess.
680 * We're passed a "virtual cylinder" (0 - 6) for the different
681 * Uemis tank cases ("air", "nitrox_1", "nitrox_2.{bottom,deco}"
682 * and "nitrox_3.{bottom,deco,travel}". We need to turn that
683 * into the actual cylinder data depending on the gas template,
684 * and ignore the ones that are irrelevant for that template.
686 * So for "template 2" (nitrox_2), we ignore virtual tanks 0-1
687 * (which are "air" and "nitrox_1" respectively), and tanks 4-6
688 * (which are the three "nitrox_3" tanks), and we turn virtual
689 * tanks 2/3 into actual tanks 0/1.
693 static int uemis_cylinder_index(void *_cylinder)
695 cylinder_t *cylinder = _cylinder;
696 unsigned int index = cylinder - dive->cylinder;
699 fprintf(stderr, "Uemis cylinder pointer calculations broken\n");
702 switch(uemis_gas_template) {
703 case 1: /* Dive uses tank 1 */
706 case 0: /* Dive uses tank 0 */
710 case 2: /* Dive uses tanks 2-3 */
715 case 3: /* Dive uses tanks 4-6 */
724 static void uemis_cylindersize(char *buffer, void *_cylinder)
726 int index = uemis_cylinder_index(_cylinder);
728 cylindersize(buffer, &dive->cylinder[index].type.size);
731 static void uemis_percent(char *buffer, void *_cylinder)
733 int index = uemis_cylinder_index(_cylinder);
735 percent(buffer, &dive->cylinder[index].gasmix.o2);
738 static int uemis_dive_match(struct dive *dive, const char *name, int len, char *buf)
740 return MATCH(".units.length", uemis_length_unit, &units) ||
741 MATCH(".units.volume", uemis_volume_unit, &units) ||
742 MATCH(".units.pressure", uemis_pressure_unit, &units) ||
743 MATCH(".units.temperature", uemis_temperature_unit, &units) ||
744 MATCH(".units.weight", uemis_weight_unit, &units) ||
745 MATCH(".units.time", uemis_time_unit, &units) ||
746 MATCH(".units.date", uemis_date_unit, &units) ||
747 MATCH(".date_time", uemis_date_time, &dive->when) ||
748 MATCH(".time_zone", uemis_time_zone, &dive->when) ||
749 MATCH(".ambient.temperature", decicelsius, &dive->airtemp) ||
750 MATCH(".gas.template", get_index, &uemis_gas_template) ||
751 MATCH(".air.bottom_tank.size", uemis_cylindersize, dive->cylinder + 0) ||
752 MATCH(".air.bottom_tank.oxygen", uemis_percent, dive->cylinder + 0) ||
753 MATCH(".nitrox_1.bottom_tank.size", uemis_cylindersize, dive->cylinder + 1) ||
754 MATCH(".nitrox_1.bottom_tank.oxygen", uemis_percent, dive->cylinder + 1) ||
755 MATCH(".nitrox_2.bottom_tank.size", uemis_cylindersize, dive->cylinder + 2) ||
756 MATCH(".nitrox_2.bottom_tank.oxygen", uemis_percent, dive->cylinder + 2) ||
757 MATCH(".nitrox_2.deco_tank.size", uemis_cylindersize, dive->cylinder + 3) ||
758 MATCH(".nitrox_2.deco_tank.oxygen", uemis_percent, dive->cylinder + 3) ||
759 MATCH(".nitrox_3.bottom_tank.size", uemis_cylindersize, dive->cylinder + 4) ||
760 MATCH(".nitrox_3.bottom_tank.oxygen", uemis_percent, dive->cylinder + 4) ||
761 MATCH(".nitrox_3.deco_tank.size", uemis_cylindersize, dive->cylinder + 5) ||
762 MATCH(".nitrox_3.deco_tank.oxygen", uemis_percent, dive->cylinder + 5) ||
763 MATCH(".nitrox_3.travel_tank.size", uemis_cylindersize, dive->cylinder + 6) ||
764 MATCH(".nitrox_3.travel_tank.oxygen", uemis_percent, dive->cylinder + 6) ||
768 /* We're in the top-level dive xml. Try to convert whatever value to a dive value */
769 static void try_to_fill_dive(struct dive *dive, const char *name, char *buf)
771 int len = strlen(name);
773 start_match("dive", name, buf);
775 switch (import_source) {
777 if (suunto_dive_match(dive, name, len, buf))
782 if (uemis_dive_match(dive, name, len, buf))
787 if (divinglog_dive_match(dive, name, len, buf))
795 if (MATCH(".date", divedate, &dive->when))
797 if (MATCH(".time", divetime, &dive->when))
799 if (MATCH(".datetime", divedatetime, &dive->when))
801 if (MATCH(".maxdepth", depth, &dive->maxdepth))
803 if (MATCH(".meandepth", depth, &dive->meandepth))
805 if (MATCH(".depth.max", depth, &dive->maxdepth))
807 if (MATCH(".depth.mean", depth, &dive->meandepth))
809 if (MATCH(".duration", duration, &dive->duration))
811 if (MATCH(".divetime", duration, &dive->duration))
813 if (MATCH(".divetimesec", duration, &dive->duration))
815 if (MATCH(".surfacetime", duration, &dive->surfacetime))
817 if (MATCH(".airtemp", temperature, &dive->airtemp))
819 if (MATCH(".watertemp", temperature, &dive->watertemp))
821 if (MATCH(".temperature.air", temperature, &dive->airtemp))
823 if (MATCH(".temperature.water", temperature, &dive->watertemp))
825 if (MATCH(".cylinderstartpressure", pressure, &dive->cylinder[0].start))
827 if (MATCH(".cylinderendpressure", pressure, &dive->cylinder[0].end))
829 if (MATCH(".location", utf8_string, &dive->location))
831 if (MATCH(".notes", utf8_string, &dive->notes))
834 if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
836 if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
838 if (MATCH(".cylinder.description", utf8_string, &dive->cylinder[cylinder_index].type.description))
840 if (MATCH(".cylinder.start", pressure, &dive->cylinder[cylinder_index].start))
842 if (MATCH(".cylinder.end", pressure, &dive->cylinder[cylinder_index].end))
845 if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
847 if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
849 if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
852 nonmatch("dive", name, buf);
856 * File boundaries are dive boundaries. But sometimes there are
857 * multiple dives per file, so there can be other events too that
858 * trigger a "new dive" marker and you may get some nesting due
859 * to that. Just ignore nesting levels.
861 static void dive_start(void)
869 size = dive_size(alloc_samples);
873 memset(dive, 0, size);
874 memset(&tm, 0, sizeof(tm));
877 static void sanitize_gasmix(gasmix_t *mix)
881 o2 = mix->o2.permille;
882 he = mix->he.permille;
884 /* Regular air: leave empty */
888 /* 20.9% or 21% O2 is just air */
889 if (o2 >= 209 && o2 <= 210) {
890 mix->o2.permille = 0;
896 if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
898 fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
899 memset(mix, 0, sizeof(*mix));
903 * See if the size/workingpressure looks like some standard cylinder
906 static void match_standard_cylinder(cylinder_type_t *type)
912 /* Do we already have a cylinder description? */
913 if (type->description)
916 cuft = type->size.mliter / 1000;
917 psi = type->workingpressure.mbar / 68.95;
920 case 2300 ... 2500: /* 2400 psi: LP tank */
923 case 2600 ... 2700: /* 2640 psi: LP+10% */
926 case 2900 ... 3100: /* 3000 psi: ALx tank */
929 case 3400 ... 3500: /* 3442 psi: HP tank */
932 case 3700 ... 3850: /* HP+10% */
938 len = snprintf(buffer, sizeof(buffer), fmt, cuft);
942 memcpy(p, buffer, len+1);
943 type->description = p;
948 * There are two ways to give cylinder size information:
949 * - total amount of gas in cuft (depends on working pressure and physical size)
952 * where "physical size" is the one that actually matters and is sane.
954 * We internally use physical size only. But we save the workingpressure
955 * so that we can do the conversion if required.
957 static void sanitize_cylinder_type(cylinder_type_t *type)
959 double volume_of_air, atm, volume;
961 /* If we have no working pressure, it had *better* be just a physical size! */
962 if (!type->workingpressure.mbar)
965 /* No size either? Nothing to go on */
966 if (!type->size.mliter)
969 /* Ok, we have both size and pressure: try to match a description */
970 match_standard_cylinder(type);
972 /* .. and let's assume that the 'size' was cu ft of air */
973 volume_of_air = type->size.mliter * 28.317; /* milli-cu ft to milliliter */
974 atm = type->workingpressure.mbar / 1013.25; /* working pressure in atm */
975 volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
976 type->size.mliter = volume + 0.5;
979 static void sanitize_cylinder_info(struct dive *dive)
983 for (i = 0; i < MAX_CYLINDERS; i++) {
984 sanitize_gasmix(&dive->cylinder[i].gasmix);
985 sanitize_cylinder_type(&dive->cylinder[i].type);
989 static void dive_end(void)
993 sanitize_cylinder_info(dive);
999 static void event_start(void)
1003 static void event_end(void)
1008 static void cylinder_start(void)
1012 static void cylinder_end(void)
1017 static void sample_start(void)
1024 if (nr >= alloc_samples) {
1027 alloc_samples = (alloc_samples * 3)/2 + 10;
1028 size = dive_size(alloc_samples);
1029 dive = realloc(dive, size);
1033 sample = dive->sample + nr;
1034 memset(sample, 0, sizeof(*sample));
1038 static void sample_end(void)
1047 static void entry(const char *name, int size, const char *raw)
1049 char *buf = malloc(size+1);
1053 memcpy(buf, raw, size);
1056 try_to_fill_sample(sample, name, buf);
1060 try_to_fill_dive(dive, name, buf);
1065 static const char *nodename(xmlNode *node, char *buf, int len)
1067 if (!node || !node->name)
1075 const char *name = node->name;
1076 int i = strlen(name);
1078 unsigned char c = name[i];
1079 *--buf = tolower(c);
1083 node = node->parent;
1084 if (!node || !node->name)
1094 static void visit_one_node(xmlNode *node)
1097 const unsigned char *content;
1098 char buffer[MAXNAME];
1101 content = node->content;
1105 /* Trim whitespace at beginning */
1106 while (isspace(*content))
1109 /* Trim whitespace at end */
1110 len = strlen(content);
1111 while (len && isspace(content[len-1]))
1117 /* Don't print out the node name if it is "text" */
1118 if (!strcmp(node->name, "text"))
1119 node = node->parent;
1121 name = nodename(node, buffer, sizeof(buffer));
1123 entry(name, len, content);
1126 static void traverse(xmlNode *root);
1128 static void traverse_properties(xmlNode *node)
1132 for (p = node->properties; p; p = p->next)
1133 traverse(p->children);
1136 static void visit(xmlNode *n)
1139 traverse_properties(n);
1140 traverse(n->children);
1143 static void suunto_importer(void)
1145 import_source = SUUNTO;
1149 static void uemis_importer(void)
1151 import_source = UEMIS;
1155 static void DivingLog_importer(void)
1157 import_source = DIVINGLOG;
1160 * Diving Log units are really strange.
1162 * Temperatures are in C, except in samples,
1163 * when they are in Fahrenheit. Depths are in
1164 * meters, but pressure is in PSI.
1167 units.pressure = PSI;
1171 * I'm sure this could be done as some fancy DTD rules.
1172 * It's just not worth the headache.
1174 static struct nesting {
1176 void (*start)(void), (*end)(void);
1178 { "dive", dive_start, dive_end },
1179 { "Dive", dive_start, dive_end },
1180 { "sample", sample_start, sample_end },
1181 { "SAMPLE", sample_start, sample_end },
1182 { "reading", sample_start, sample_end },
1183 { "event", event_start, event_end },
1184 { "gasmix", cylinder_start, cylinder_end },
1185 { "cylinder", cylinder_start, cylinder_end },
1186 { "P", sample_start, sample_end },
1188 /* Import type recognition */
1189 { "SUUNTO", suunto_importer },
1190 { "Divinglog", DivingLog_importer },
1191 { "pre_dive", uemis_importer },
1196 static void traverse(xmlNode *root)
1200 for (n = root; n; n = n->next) {
1201 struct nesting *rule = nesting;
1204 if (!strcmp(rule->name, n->name))
1207 } while (rule->name);
1217 /* Per-file reset */
1218 static void reset_all(void)
1221 * We reset the units for each file. You'd think it was
1222 * a per-dive property, but I'm not going to trust people
1223 * to do per-dive setup. If the xml does have per-dive
1224 * data within one file, we might have to reset it per
1225 * dive for that format.
1228 import_source = UNKNOWN;
1231 void parse_xml_file(const char *filename, GError **error)
1235 doc = xmlReadFile(filename, NULL, 0);
1237 fprintf(stderr, "Failed to parse '%s'.\n", filename);
1240 *error = g_error_new(g_quark_from_string("divelog"),
1242 "Failed to parse '%s'",
1250 traverse(xmlDocGetRootElement(doc));
1256 void parse_xml_init(void)