File: io.c

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (124 lines) | stat: -rw-r--r-- 1,999 bytes parent folder | download
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

#include <stdio.h>
#include <stdlib.h>

#if !defined(VMS) && !defined(_WIN32)
#include <unistd.h>
#endif

#ifdef _WIN32
#include <windows.h>
#include <io.h>
#endif

#if defined(cray) || defined(__SVR4) || defined(_WIN32)
#include <fcntl.h>
#else
#include <sys/file.h>
#endif

#include <sys/stat.h>
#include <sys/types.h>

#include "gkscore.h"

int gks_open_file(const char *path, const char *mode)
{
  int fd, flags, omode;
#ifdef _WIN32
  wchar_t w_path[MAX_PATH];
#endif

  switch (*mode)
    {
    case 'r':
#if defined(_WIN32)
      flags = O_RDONLY | O_BINARY;
#else
      flags = O_RDONLY;
#endif
      omode = 0;
      break;

    case 'w':
#if defined(_WIN32)
      flags = O_CREAT | O_TRUNC | O_WRONLY | O_BINARY;
      omode = S_IREAD | S_IWRITE;
#else
      flags = O_CREAT | O_TRUNC | O_WRONLY;
      omode = 0644;
#endif
      break;

    default:
      return -1;
    }

#ifdef _WIN32
  MultiByteToWideChar(CP_UTF8, 0, path, strlen(path) + 1, w_path, MAX_PATH);
  fd = _wopen(w_path, flags, omode);
#else
  fd = open(path, flags, omode);
#endif
  if (fd < 0)
    {
      gks_perror("file open error (%s)", path);
      perror("open");
    }

  return fd;
}

int gks_read_file(int fd, void *buf, int count)
{
  int cc;

#ifdef _WIN32
  cc = _read(fd, buf, count);
#else
  cc = read(fd, buf, count);
#endif
  if (cc != count)
    {
      gks_perror("file read error (fd=%d, cc=%d)", fd, cc);
      if (cc == -1) perror("read");
    }

  return cc;
}

int gks_write_file(int fd, void *buf, int count)
{
  int cc;

#ifdef _WIN32
  cc = _write(fd, buf, count);
#else
  cc = write(fd, buf, count);
#endif
  if (cc != count)
    {
      gks_perror("file write error (fd=%d, cc=%d)", fd, cc);
      if (cc == -1) perror("write");
    }

  return cc;
}

int gks_close_file(int fd)
{
  int result;

#ifdef _WIN32
  result = _close(fd);
#else
  result = close(fd);
#endif
  if (result < 0)
    {
      gks_perror("file close error (fd=%d)", fd);
      perror("close");
    }

  return result;
}