~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/xplt/src/play/win/files.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * files.c -- $Id: files.c,v 1.1 2003/03/08 15:26:49 travo Exp $
 
3
 * MS Windows version of plib file operations
 
4
 *
 
5
 * Copyright (c) 1999.  See accompanying LEGAL file for details.
 
6
 */
 
7
 
 
8
#include "config.h"
 
9
#include "pstdio.h"
 
10
#include "pstdlib.h"
 
11
#include "playw.h"
 
12
 
 
13
#include <stdio.h>
 
14
#include <io.h>
 
15
#include <fcntl.h>
 
16
 
 
17
struct p_file {
 
18
  FILE *fp;  /* use FILE* for text file operations */
 
19
  int fd;    /* use file descriptor for binary file operations */
 
20
  int binary;
 
21
};
 
22
 
 
23
p_file *
 
24
p_fopen(const char *unix_name, const char *mode)
 
25
{
 
26
  FILE *fp = fopen(w_pathname(unix_name), mode);
 
27
  p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
 
28
  if (f) {
 
29
    f->fp = fp;
 
30
    f->fd = _fileno(fp);
 
31
    for (; mode[0] ; mode++) if (mode[0]=='b') break;
 
32
    f->binary = (mode[0]=='b');
 
33
    /* problem:
 
34
     *   in _O_TEXT mode, ftell only works properly if the file
 
35
     *   really is a DOS file with CRLF newlines; if it is a UNIX
 
36
     *   file (like all of the distribution .i files), ftell fails
 
37
     * on the other hand, using _O_BINARY makes fgets fail to remove
 
38
     *   the CR and fputs not insert CR
 
39
     * (may depend on Windows flavor, this is Win2k */
 
40
    if (!f->binary) _setmode(f->fd, _O_BINARY);
 
41
  }
 
42
  return f;
 
43
}
 
44
 
 
45
p_file *
 
46
p_popen(const char *command, const char *mode)
 
47
{
 
48
  /* WARNING- for non-console programs, returned FILE* causes
 
49
   * program to hang forever (according to msdn documentation) */
 
50
  FILE *fp = _popen(command, mode);
 
51
  p_file *f = fp? p_malloc(sizeof(p_file)) : 0;
 
52
  if (f) {
 
53
    f->fp = fp;
 
54
    f->fd = fileno(fp);
 
55
    for (; mode[0] ; mode++) if (mode[0]=='b') break;
 
56
    f->binary = (mode[0]=='b') | 2;
 
57
  }
 
58
  return f;
 
59
}
 
60
 
 
61
int
 
62
p_fclose(p_file *file)
 
63
{
 
64
  int flag = (file->binary&2)? _pclose(file->fp) : fclose(file->fp);
 
65
  p_free(file);
 
66
  return flag;
 
67
}
 
68
 
 
69
char *
 
70
p_fgets(p_file *file, char *buf, int buflen)
 
71
{
 
72
  char *b = fgets(buf, buflen, file->fp);
 
73
  if (b) {
 
74
    /* remove CR if line ends with CRLF */
 
75
    int n;
 
76
    for (n=0 ; n<buflen-2 ; n++) {
 
77
      if (!buf[n]) break;
 
78
      if (buf[n]=='\015' && buf[n+1]=='\n' && buf[n+2]=='\0')
 
79
        buf[n] = '\n', buf[n+1] = '\0';
 
80
    }
 
81
  }
 
82
  return b;
 
83
}
 
84
 
 
85
int
 
86
p_fputs(p_file *file, const char *buf)
 
87
{
 
88
  int n, dn, i;
 
89
  char b[1026];
 
90
  for (n=0 ;;) {
 
91
    for (i=0 ; i<1024 ; i++,buf++) {
 
92
      if (!buf[0]) break;
 
93
      if (buf[0] == '\n') b[i++] = '\015';
 
94
      b[i] = buf[0];
 
95
    }
 
96
    b[i] = '\0';
 
97
    dn = fputs(b, file->fp);
 
98
    if (dn < 0) return dn;
 
99
    n += dn;
 
100
    if (!buf[0]) break;
 
101
  }
 
102
  return n;
 
103
}
 
104
 
 
105
unsigned long
 
106
p_fread(p_file *file, void *buf, unsigned long nbytes)
 
107
{
 
108
  if (file->binary&1) {
 
109
    int fd = fileno(file->fp);
 
110
    return _read(fd, buf, nbytes);
 
111
  } else {
 
112
    return fread(buf, 1, nbytes, file->fp);
 
113
  }
 
114
}
 
115
 
 
116
unsigned long
 
117
p_fwrite(p_file *file, const void *buf, unsigned long nbytes)
 
118
{
 
119
  if (file->binary&1) {
 
120
    int fd = fileno(file->fp);
 
121
    return _write(fd, buf, nbytes);
 
122
  } else {
 
123
    return fwrite(buf, 1, nbytes, file->fp);
 
124
  }
 
125
}
 
126
 
 
127
unsigned long
 
128
p_ftell(p_file *file)
 
129
{
 
130
  if (file->binary&1)
 
131
    return _tell(file->fd);
 
132
  else /* broken in _O_TEXT mode, see p_fopen */
 
133
    return ftell(file->fp);
 
134
}
 
135
 
 
136
int
 
137
p_fseek(p_file *file, unsigned long addr)
 
138
{
 
139
  if (file->binary&1)
 
140
    return -(_lseek(file->fd, addr, SEEK_SET)==-1L);
 
141
  else
 
142
    return fseek(file->fp, addr, SEEK_SET);
 
143
}
 
144
 
 
145
int
 
146
p_fflush(p_file *file)
 
147
{
 
148
  return fflush(file->fp);
 
149
}
 
150
 
 
151
int
 
152
p_feof(p_file *file)
 
153
{
 
154
  return feof(file->fp);
 
155
}
 
156
 
 
157
int
 
158
p_ferror(p_file *file)
 
159
{
 
160
  int flag = ferror(file->fp);
 
161
  clearerr(file->fp);
 
162
  return flag;
 
163
}
 
164
 
 
165
unsigned long
 
166
p_fsize(p_file *file)
 
167
{
 
168
  return _filelength(file->fd);
 
169
}
 
170
 
 
171
int
 
172
p_remove(const char *unix_name)
 
173
{
 
174
  return -(!DeleteFile(w_pathname(unix_name)));
 
175
}
 
176
 
 
177
int
 
178
p_rename(const char *unix_old, const char *unix_new)
 
179
{
 
180
  char old[P_WKSIZ+1];
 
181
  old[0] = '\0';
 
182
  strncat(old, w_pathname(unix_old), P_WKSIZ);
 
183
  return -(!MoveFile(old, w_pathname(unix_new)));
 
184
}