File: dump.c

package info (click to toggle)
gwyddion 2.67-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 54,152 kB
  • sloc: ansic: 412,023; python: 7,885; sh: 5,492; makefile: 4,957; xml: 3,954; cpp: 2,107; pascal: 418; perl: 154; ruby: 130
file content (154 lines) | stat: -rw-r--r-- 4,136 bytes parent folder | download | duplicates (3)
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
/**
 * $Id: dump.c 22454 2019-08-31 19:53:40Z yeti-dn $
 * A Gwyddion plug-in providing the `dump' format by simply copying what
 * it gets from plug-in proxy.
 * Written by Yeti <yeti@gwyddion.net>.  Public domain.
 **/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_STDBOOL_H
#include <stdbool.h>
#else
#define bool int
enum { false = 0, true = 1 };
#endif

#ifdef __GNUC__
#define UNUSED __attribute__((unused))
#else
#define UNUSED /* */
#endif

typedef struct {
    const char *name;              /* action name: register, load, run, ... */
    int arg_num;                   /* number of arguments NOT including
                                      program name, nor action name */
    bool (*action)(char *args[]);  /* action itself, will get array of
                                      arg_num program argument, NOT including
                                      program name, nor action name */
} PluginAction;

static bool run_action    (unsigned int nactions,
                           PluginAction *actions,
                           int argc,
                           char *argv[]);
static bool copy_file     (const char *src_file,
                           const char *dest_file);

/* Plug-in helper data.
 * Each action (register, run) is represented by a PluginAction struct with
 * one function actually performing the action. */
static bool action_register(char *args[]);
static bool action_load    (char *args[]);
static bool action_save    (char *args[]);

static PluginAction plugin_actions[] = {
    /* name       arguments action */
    { "register", 0,        &action_register },
    { "load",     2,        &action_load     },
    { "save",     2,        &action_save     },
};

#define NACTIONS sizeof(plugin_actions)/sizeof(plugin_actions[0])

int
main(int argc, char *argv[])
{
    return !run_action(NACTIONS, plugin_actions, argc, argv);
}

/* "register" action: print registration information to standard output */
static bool
action_register(UNUSED char *args[])
{
    puts("dump\n"
         "Plug-in proxy dump (.dump)\n"
         "*.dump\n"
         "load save");
    return true;
}

/* "load" action: load files, i.e. copy them with no conversion to dump files */
static bool
action_load(char *args[])
{
    /* Copy second file to first (dump) file */
    return copy_file(args[1], args[0]);
}

/* "save" action: save files, i.e. copy dump files with no conversion to them */
static bool
action_save(char *args[])
{
    /* Copy first (dump) file to second file */
    return copy_file(args[0], args[1]);
}


/* copy src_file to dest_file */
static bool
copy_file(const char *src_file,
          const char *dest_file)
{
    enum { BUF_SIZE = 4096 };
    char *buffer;
    FILE *fhr, *fhw;
    unsigned long int n;

    if (!(fhr = fopen(src_file, "rb"))) {
        fprintf(stderr, "Cannot open `%s' for reading.", src_file);
        return false;
    }
    if (!(fhw = fopen(dest_file, "wb"))) {
        fprintf(stderr, "Cannot open `%s' for writing.", dest_file);
        fclose(fhr);
        return false;
    }
    buffer = (char*)malloc(BUF_SIZE);

    do {
        n = fread(buffer, 1, BUF_SIZE, fhr);
        if (n && fwrite(buffer, 1, n, fhw) != n) {
            fprintf(stderr, "Cannot write to `%s'.", dest_file);
            break;
        }
    } while (n == BUF_SIZE);

    free(buffer);
    fclose(fhr);
    fclose(fhw);

    return true;
}

/************ plug-in helper ***************/
/* Find action and run it */
static bool
run_action(unsigned int nactions, PluginAction *actions,
           int argc, char *argv[])
{
    unsigned int i;

    for (i = 0; i < nactions; i++) {
        if (argc == actions[i].arg_num + 2
            && strcmp(actions[i].name, argv[1]) == 0) {
            actions[i].action(argv + 2);
            return true;
        }
    }
    fprintf(stderr, "Plug-in has to be called from Gwyddion plugin-proxy.\n");
    return false;
}


/* 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 : */