1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/*
* $Id: dumbfile.c 20674 2017-12-18 17:58:47Z yeti-dn $
* Copyright (C) 2009 David Necas (Yeti).
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* [FILE-MAGIC-FREEDESKTOP]
* <mime-type type="application/x-gwyddion-dump-spm">
* <comment>Gwyddion dumb dump data</comment>
* <magic priority="80">
* <match type="string" offset="0" value="/0/data/"/>
* </magic>
* <glob pattern="*.dump"/>
* <glob pattern="*.DUMP"/>
* </mime-type>
**/
/**
* [FILE-MAGIC-USERGUIDE]
* Gwyddion dumb dump data
* .dump
* Read
**/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwyddion/gwyutils.h>
#include <libgwymodule/gwymodule-file.h>
#include <app/gwymoduleutils-file.h>
#include <app/data-browser.h>
#include "err.h"
#define MAGIC "/0/data/"
#define MAGIC_SIZE (sizeof(MAGIC)-1)
#define EXTENSION ".dump"
static gboolean module_register (void);
static gint dumb_detect (const GwyFileDetectInfo *fileinfo,
gboolean only_name);
static GwyContainer* dumb_load (const gchar *filename,
GwyRunType mode,
GError **error);
static GwyContainer* text_dump_import(gchar *buffer,
gsize size,
const gchar *filename,
GError **error);
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Reads and exports Gwyddion dumb dump files."),
"Yeti <yeti@gwyddion.net>",
"1.2",
"David Nečas (Yeti)",
"2011",
};
GWY_MODULE_QUERY2(module_info, dumbfile)
static gboolean
module_register(void)
{
gwy_file_func_register("dumbfile",
N_("Gwyddion dumb dump files (.dump)"),
(GwyFileDetectFunc)&dumb_detect,
(GwyFileLoadFunc)&dumb_load,
NULL, NULL);
return TRUE;
}
static gint
dumb_detect(const GwyFileDetectInfo *fileinfo,
gboolean only_name)
{
if (only_name)
return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 15 : 0;
if (fileinfo->file_size < MAGIC_SIZE
|| memcmp(fileinfo->head, MAGIC, MAGIC_SIZE) != 0)
return 0;
return 100;
}
static GwyContainer*
dumb_load(const gchar *filename,
G_GNUC_UNUSED GwyRunType mode,
GError **error)
{
GwyContainer *container = NULL;
gchar *buffer = NULL;
GError *err = NULL;
gsize size;
if (!g_file_get_contents(filename, &buffer, &size, &err)) {
err_GET_FILE_CONTENTS(error, &err);
return NULL;
}
if (size < MAGIC_SIZE || memcmp(buffer, MAGIC, MAGIC_SIZE) != 0) {
err_FILE_TYPE(error, "Gwyddion dumb dump");
goto fail;
}
container = text_dump_import(buffer, size, filename, error);
fail:
g_free(buffer);
return container;
}
static GwyContainer*
text_dump_import(gchar *buffer,
gsize size,
const gchar *filename,
GError **error)
{
gchar *val, *key, *pos, *line, *title;
GwyContainer *data;
GwyDataField *dfield;
gdouble xreal, yreal;
gint xres, yres, id;
GwySIUnit *uxy, *uz;
const guchar *s;
gdouble *d;
gsize n;
data = gwy_container_new();
pos = buffer;
while ((line = gwy_str_next_line(&pos)) && *line) {
val = strchr(line, '=');
if (!val || *line != '/') {
g_warning("Garbage key: %s", line);
continue;
}
if ((gsize)(val - buffer) + 1 > size) {
g_set_error(error, GWY_MODULE_FILE_ERROR,
GWY_MODULE_FILE_ERROR_DATA,
_("End of file reached when value was expected."));
goto fail;
}
*val = '\0';
val++;
if (!gwy_strequal(val, "[") || !pos || *pos != '[') {
gwy_debug("<%s>=<%s>", line, val);
if (*val)
gwy_container_set_string_by_name(data, line, g_strdup(val));
else
gwy_container_remove_by_name(data, line);
continue;
}
g_assert(pos && *pos == '[');
pos++;
dfield = NULL;
gwy_container_gis_object_by_name(data, line, &dfield);
id = 0;
sscanf(line, "/%d", &id);
/* get datafield parameters from already read values, failing back
* to values of original data field */
key = g_strconcat(line, "/xres", NULL);
if (gwy_container_gis_string_by_name(data, key, &s))
xres = atoi(s);
else if (dfield)
xres = gwy_data_field_get_xres(dfield);
else {
g_set_error(error, GWY_MODULE_FILE_ERROR,
GWY_MODULE_FILE_ERROR_DATA,
_("Missing data field width."));
goto fail;
}
g_free(key);
key = g_strconcat(line, "/yres", NULL);
if (gwy_container_gis_string_by_name(data, key, &s))
yres = atoi(s);
else if (dfield)
yres = gwy_data_field_get_yres(dfield);
else {
g_set_error(error, GWY_MODULE_FILE_ERROR,
GWY_MODULE_FILE_ERROR_DATA,
_("Missing data field height."));
goto fail;
}
g_free(key);
key = g_strconcat(line, "/xreal", NULL);
if (gwy_container_gis_string_by_name(data, key, &s))
xreal = g_ascii_strtod(s, NULL);
else if (dfield)
xreal = gwy_data_field_get_xreal(dfield);
else {
g_warning("Missing real data field width.");
xreal = 1.0; /* 0 could cause troubles */
}
g_free(key);
key = g_strconcat(line, "/yreal", NULL);
if (gwy_container_gis_string_by_name(data, key, &s))
yreal = g_ascii_strtod(s, NULL);
else if (dfield)
yreal = gwy_data_field_get_yreal(dfield);
else {
g_warning("Missing real data field height.");
yreal = 1.0; /* 0 could cause troubles */
}
g_free(key);
if (!(xres > 0 && yres > 0 && xreal > 0 && yreal > 0)) {
g_set_error(error, GWY_MODULE_FILE_ERROR,
GWY_MODULE_FILE_ERROR_DATA,
_("Data field dimensions are not positive numbers."));
goto fail;
}
key = g_strconcat(line, "/unit-xy", NULL);
if (gwy_container_gis_string_by_name(data, key, &s))
uxy = gwy_si_unit_new((const gchar*)s);
else if (dfield) {
uxy = gwy_data_field_get_si_unit_xy(dfield);
uxy = gwy_si_unit_duplicate(uxy);
}
else {
g_warning("Missing lateral units.");
uxy = gwy_si_unit_new("m");
}
g_free(key);
key = g_strconcat(line, "/unit-z", NULL);
if (gwy_container_gis_string_by_name(data, key, &s))
uz = gwy_si_unit_new((const gchar*)s);
else if (dfield) {
uz = gwy_data_field_get_si_unit_z(dfield);
uz = gwy_si_unit_duplicate(uz);
}
else {
g_warning("Missing value units.");
uz = gwy_si_unit_new("m");
}
g_free(key);
key = g_strconcat(line, "/title", NULL);
title = NULL;
gwy_container_gis_string_by_name(data, key, (const guchar**)&title);
/* We got the contained string but that would disappear. */
title = g_strdup(title);
g_free(key);
n = xres*yres*sizeof(gdouble);
if ((gsize)(pos - buffer) + n + 3 > size) {
g_set_error(error, GWY_MODULE_FILE_ERROR,
GWY_MODULE_FILE_ERROR_DATA,
_("End of file reached inside a data field."));
goto fail;
}
dfield = GWY_DATA_FIELD(gwy_data_field_new(xres, yres, xreal, yreal,
FALSE));
gwy_data_field_set_si_unit_xy(dfield, GWY_SI_UNIT(uxy));
GWY_OBJECT_UNREF(uxy);
gwy_data_field_set_si_unit_z(dfield, GWY_SI_UNIT(uz));
GWY_OBJECT_UNREF(uz);
d = gwy_data_field_get_data(dfield);
#if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
memcpy(d, pos, n);
#else
gwy_memcpy_byte_swap(pos, (guint8*)d,
sizeof(gdouble), xres*yres, sizeof(gdouble)-1);
#endif
pos += n;
val = gwy_str_next_line(&pos);
if (!gwy_strequal(val, "]]")) {
g_set_error(error, GWY_MODULE_FILE_ERROR,
GWY_MODULE_FILE_ERROR_DATA,
_("Missing end of data field marker."));
GWY_OBJECT_UNREF(dfield);
goto fail;
}
gwy_container_remove_by_prefix(data, line);
gwy_container_set_object_by_name(data, line, dfield);
g_object_unref(dfield);
if (title) {
key = g_strconcat(line, "/title", NULL);
gwy_container_set_string_by_name(data, key, title);
g_free(key);
}
gwy_file_channel_import_log_add(data, id, NULL, filename);
}
return data;
fail:
gwy_container_remove_by_prefix(data, NULL);
g_object_unref(data);
return NULL;
}
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|