File: list_gp.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (119 lines) | stat: -rw-r--r-- 3,019 bytes parent folder | download | duplicates (2)
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
/*!
   \file list_gp.c

   \brief Imagery Library - List group

   (C) 2001-2008 by the GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2). Read the file COPYING that comes with GRASS for details.

   \author USA CERL
 */

#include <string.h>
#include <grass/imagery.h>
#include <grass/glocale.h>

/*!
 * \brief Prints maps in a group (fancy version)
 *
 * \param group group name
 * \param ref group reference (set with I_get_group_ref())
 * \param fd where to print (typically stdout)
 * \return 0
 */
int I_list_group(const char *group, const struct Ref *ref, FILE *fd)
{
    char buf[80];
    int i;
    int len, tot_len;
    int max;

    if (ref->nfiles <= 0) {
        fprintf(fd, _("group <%s> is empty\n"), group);
        return 0;
    }
    max = 0;
    for (i = 0; i < ref->nfiles; i++) {
        I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
        len = strlen(buf) + 4;
        if (len > max)
            max = len;
    }
    fprintf(fd, _("group <%s> references the following raster maps\n"), group);
    fprintf(fd, "-------------\n");
    tot_len = 0;
    for (i = 0; i < ref->nfiles; i++) {
        I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
        tot_len += max;
        if (tot_len > 78) {
            fprintf(fd, "\n");
            tot_len = max;
        }
        fprintf(fd, "%-*s", max, buf);
    }
    if (tot_len)
        fprintf(fd, "\n");
    fprintf(fd, "-------------\n");

    return 0;
}

/*!
 * \brief Prints maps in a group (simple version)
 *
 * Same as I_list_group(), but without all the fancy stuff.
 * Prints one map per line in map@mapset form.
 *
 * \param ref group reference (set with I_get_group_ref())
 * \param fd where to print (typically stdout)
 * \return 0
 */
int I_list_group_simple(const struct Ref *ref, FILE *fd)
{
    int i;

    if (ref->nfiles <= 0)
        return 0;

    for (i = 0; i < ref->nfiles; i++)
        fprintf(fd, "%s@%s\n", ref->file[i].name, ref->file[i].mapset);

    return 0;
}

/*!
 * \brief Formats map name to fit in a 80 column layout
 *
 * Results in a map name in the "<map@mapset>" form.
 * If necessary truncates relevant part(s) and denotes
 * with ellipsis, e.g. "<verylongmapname...@mapset>".
 *
 * \param[out] buf formatted map name
 * \param name map name
 * \param mapset mapset name
 */
void I__list_group_name_fit(char *buf, const char *name, const char *mapset)
{
    char *frmt;
    char fr[32];
    int name_length = (int)strlen(name);
    int mapset_length = (int)strlen(mapset);

    if (name_length + mapset_length + 3 < 75) {
        frmt = "<%s@%s>";
    }
    else if (name_length > 35 && mapset_length > 35) {
        frmt = "<%.33s...@%.32s...>";
    }
    else if (name_length > 35) {
        sprintf(fr, "<%%.%ds...@%%s>", 68 - mapset_length);
        frmt = fr;
    }
    else {
        sprintf(fr, "<%%s@%%.%ds...>", 68 - name_length);
        frmt = fr;
    }
    snprintf(buf, 75, frmt, name, mapset);
}