~ubuntu-branches/ubuntu/wily/grass/wily

« back to all changes in this revision

Viewing changes to lib/iostream/ami_stream.cc

Tags: 7.0.0~rc1+ds1-1~exp1
* New upstream release candidate.
* Repack upstream tarball, remove precompiled Python objects.
* Add upstream metadata.
* Update gbp.conf and Vcs-Git URL to use the experimental branch.
* Update watch file for GRASS 7.0.
* Drop build dependencies for Tcl/Tk, add build dependencies:
  python-numpy, libnetcdf-dev, netcdf-bin, libblas-dev, liblapack-dev
* Update Vcs-Browser URL to use cgit instead of gitweb.
* Update paths to use grass70.
* Add configure options: --with-netcdf, --with-blas, --with-lapack,
  remove --with-tcltk-includes.
* Update patches for GRASS 7.
* Update copyright file, changes:
  - Update copyright years
  - Group files by license
  - Remove unused license sections
* Add patches for various typos.
* Fix desktop file with patch instead of d/rules.
* Use minimal dh rules.
* Bump Standards-Version to 3.9.6, no changes.
* Use dpkg-maintscript-helper to replace directories with symlinks.
  (closes: #776349)
* Update my email to use @debian.org address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 * 
3
 
 *  MODULE:     iostream
4
 
 *
5
 
 *  COPYRIGHT (C) 2007 Laura Toma
6
 
 *   
7
 
 *  This program is free software; you can redistribute it and/or modify
8
 
 *  it under the terms of the GNU General Public License as published by
9
 
 *  the Free Software Foundation; either version 2 of the License, or
10
 
 *  (at your option) any later version.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *****************************************************************************/
18
 
 
19
 
#include <sys/types.h>
20
 
#include <sys/stat.h>
21
 
#include <stdio.h>
22
 
#include <stdlib.h>
23
 
#include <assert.h>
24
 
#include <fcntl.h>
25
 
#include <errno.h>
26
 
#include <unistd.h>
27
 
 
28
 
//#include <ami_stream.h>
29
 
#include <grass/iostream/ami_stream.h>
30
 
 
31
 
 
32
 
char *ami_str_error[] = {
33
 
  "AMI_ERROR_NO_ERROR",
34
 
  "AMI_ERROR_IO_ERROR",
35
 
  "AMI_ERROR_END_OF_STREAM",
36
 
  "AMI_ERROR_OUT_OF_RANGE",
37
 
  "AMI_ERROR_READ_ONLY",
38
 
  "AMI_ERROR_OS_ERROR",
39
 
  "AMI_ERROR_MM_ERROR",
40
 
  "AMI_ERROR_OBJECT_INITIALIZATION",
41
 
  "AMI_ERROR_PERMISSION_DENIED",
42
 
  "AMI_ERROR_INSUFFICIENT_MAIN_MEMORY",
43
 
  "AMI_ERROR_INSUFFICIENT_AVAILABLE_STREAMS",
44
 
  "AMI_ERROR_ENV_UNDEFINED",
45
 
  "AMI_ERROR_NO_MAIN_MEMORY_OPERATION",
46
 
};
47
 
 
48
 
/**********************************************************************/
49
 
/* creates a random file name, opens the file for reading and writing
50
 
   and and returns a file descriptor */
51
 
int
52
 
ami_single_temp_name(const std::string& base, char* tmp_path) {
53
 
 
54
 
  char *base_dir;
55
 
  int fd;
56
 
 
57
 
  // get the dir
58
 
  base_dir = getenv(STREAM_TMPDIR);
59
 
  if(!base_dir) {
60
 
        fprintf(stderr, "ami_stream: %s not set\n", STREAM_TMPDIR);
61
 
        assert(base_dir);
62
 
        exit(1);
63
 
  }
64
 
  sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base.c_str());
65
 
 
66
 
#ifdef __MINGW32__
67
 
  fd = mktemp(tmp_path) ? open(tmp_path, O_CREAT|O_EXCL|O_RDWR, 0600) : -1;
68
 
#else
69
 
  fd = mkstemp(tmp_path);
70
 
#endif
71
 
 
72
 
  if (fd == -1) {
73
 
    cerr <<  "ami_single_temp_name: ";
74
 
#ifdef __MINGW32__
75
 
    perror("mktemp failed: ");
76
 
#else
77
 
    perror("mkstemp failed: ");
78
 
#endif
79
 
    assert(0);
80
 
    exit(1);
81
 
  }
82
 
  return fd;
83
 
}
84
 
 
85
 
 
86
 
/**********************************************************************/
87
 
/* given fd=fide descriptor, associates with it a stream aopened in
88
 
   access_mode and returns it */
89
 
FILE* 
90
 
open_stream(int fd, AMI_stream_type st) {
91
 
  FILE* fp = NULL;
92
 
  
93
 
  assert(fd > -1);   
94
 
  switch (st) {
95
 
  case   AMI_READ_STREAM:
96
 
    fp = fdopen(fd, "rb");
97
 
    break;
98
 
  case   AMI_WRITE_STREAM:
99
 
    fp = fdopen(fd, "wb");
100
 
    break;
101
 
  case AMI_APPEND_WRITE_STREAM:
102
 
    fp = fdopen(fd, "ab");
103
 
    break;
104
 
  case AMI_APPEND_STREAM:
105
 
    fp = fdopen(fd, "ab+");
106
 
    break;
107
 
  case AMI_READ_WRITE_STREAM: 
108
 
        fp = fdopen(fd, "rb+");
109
 
        if (!fp) {
110
 
          //if file does not exist, create it
111
 
          fp = fdopen(fd, "wb+");
112
 
        }
113
 
        break;
114
 
  }
115
 
  if(!fp) {
116
 
    perror("fdopen");
117
 
  }
118
 
  assert(fp);
119
 
 
120
 
  return fp;
121
 
}
122
 
 
123
 
 
124
 
/**********************************************************************/
125
 
/* open the file whose name is pathname in access mode */
126
 
FILE* 
127
 
open_stream(char* pathname, AMI_stream_type st) {
128
 
 
129
 
  FILE* fp = NULL;
130
 
  assert(pathname);
131
 
 
132
 
  switch (st) {
133
 
  case   AMI_READ_STREAM:
134
 
    fp = fopen(pathname, "rb");
135
 
    break;
136
 
  case   AMI_WRITE_STREAM:
137
 
    fp = fopen(pathname, "wb");
138
 
    break;
139
 
  case AMI_APPEND_WRITE_STREAM:
140
 
    fp = fopen(pathname, "ab");
141
 
    break;
142
 
  case AMI_APPEND_STREAM:
143
 
    fp = fopen(pathname, "ab+");
144
 
    assert(fp);
145
 
    if (fseek (fp, 0, SEEK_END) == -1) {
146
 
      perror("AMI_STREAM: fseek failed ");
147
 
    }
148
 
    break;
149
 
  case AMI_READ_WRITE_STREAM: 
150
 
      fp = fopen(pathname, "rb+");
151
 
      if (!fp) {
152
 
        //if file does not exist, create it
153
 
      fp = fopen(pathname, "wb+");
154
 
      }
155
 
      break;
156
 
  }
157
 
  if (!fp) {
158
 
    perror(pathname);
159
 
    assert(0);
160
 
    exit(1);
161
 
  }
162
 
  assert(fp);
163
 
  return fp;
164
 
}
165