File: hist.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (148 lines) | stat: -rw-r--r-- 3,756 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
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
/****************************************************************************
*
* MODULE:       Vector library 
*   	    	
* AUTHOR(S):    Radim Blazek
*
*
* PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT:    (C) 2001 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.
*
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "Vect.h"

/*!
 \fn int Vect_hist_command ( struct Map_info *Map )
 \brief write command info to history file
 \return 0 OK, -1 error
 \param struct Map_info *Map
*/
int 
Vect_hist_command ( struct Map_info *Map )
{
    char *cmd, buf[2000];

    G_debug (3, "Vect_hist_command()");

    cmd = G_recreate_command();

    Vect_hist_write ( Map, "COMMAND: " );
    Vect_hist_write ( Map, cmd );
    Vect_hist_write ( Map, "\n" );

    sprintf ( buf, "GISDBASE: %s\n", G_gisdbase()); /* Needed ?*/
    Vect_hist_write ( Map, buf );

    sprintf ( buf, "LOCATION: %s MAPSET: %s USER: %s DATE: %s\n", 
	            G_location(), G_mapset(), G_whoami(), G_date()); /* Needed ?*/
    Vect_hist_write ( Map, buf );
    
    return 0;
}

/*!
 \fn int Vect_hist_write ( struct Map_info *Map, char *str )
 \brief write string to history file
 \return the number of characters printed
 \param struct Map_info *Map, char *str
*/
int 
Vect_hist_write ( struct Map_info *Map, char *str )
{
    int ret ;
    
    G_debug (5, "Vect_hist_write()");
    ret = fprintf ( Map->hist_fp, str ); 
    fflush ( Map->hist_fp );

    return ( ret ); 
}

/*!
 \fn char Vect_hist_read ( char *s, int size, struct Map_info *Map )
 \brief reads one line from history file without newline character
 \return return s on success, and NULL on error or EOF
 \param s buffer, allocated space must be size+1
 \param size maximum number of character
 \param Map 
*/
char * 
Vect_hist_read ( char *s, int size, struct Map_info *Map )
{
    int ret;
    G_debug (5, "Vect_hist_read()");

    if ( Map->hist_fp == NULL ) return NULL; /* OK for shapefile etc. */

    ret = G_getl2 (s, size, Map->hist_fp);

    if ( ret == 1 ) return s;

    return NULL;
}

/*!
 \fn int Vect_hist_rewind ( struct Map_info *Map )
 \brief rewind history file
 \return the number of characters printed
 \param struct Map_info *Map, char *str
*/
void 
Vect_hist_rewind ( struct Map_info *Map )
{
    G_debug (3, "Vect_hist_rewind()");

    if ( Map->hist_fp != NULL )
        rewind ( Map->hist_fp );
}

/*!
 \fn int Vect_hist_copy ( struct Map_info *In, struct Map_info *Out )
 \brief copy history from one map to another
 \return 0 OK, -1 error
 \param struct Map_info *In, struct Map_info *Out
*/
int 
Vect_hist_copy ( struct Map_info *In, struct Map_info *Out )
{
    int red, ret;
    char buf[1000];
    
    G_debug (3, "Vect_hist_copy()");

    if ( In->hist_fp == NULL ) return 0; /* This is correct (old hist doesn't exist) */
    if ( Out->hist_fp == NULL ) return -1; 

    fseek ( Out->hist_fp, 0, SEEK_END);
    rewind ( In->hist_fp );

    while ( (red = fread (buf, 1, 1000, In->hist_fp)) ) {
        if ( !(ret = fwrite (buf, 1, red, Out->hist_fp))) {
	    return (-1);
        }
	fflush ( Out->hist_fp );
    }

    /* In ends with \n ? */
    fseek ( In->hist_fp, -1, SEEK_END);
    if ( fread ( buf, 1, 1, In->hist_fp) != 1 ) {
        return -1;
    }

    if ( buf[0] != '\n' ) {
        Vect_hist_write ( Out, "\n");
    }

    /* Separator */
    Vect_hist_write ( Out, "---------------------------------------------------------------------------------\n");
    return ( 0 ); 
}