File: binary.cat

package info (click to toggle)
scilab 2.4-1
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 55,196 kB
  • ctags: 38,019
  • sloc: ansic: 231,970; fortran: 148,976; tcl: 7,099; makefile: 4,585; sh: 2,978; csh: 154; cpp: 101; asm: 39; sed: 5
file content (114 lines) | stat: -rw-r--r-- 3,920 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

binary(1)                      Scilab Function                      binary(1)
NAME
  binary - binary file management

CALLING SEQUENCE
  [fd,err]=mopen('file-name' [, mode,  swap ])
  [err]=mclose([fd])
  [x]=mget([n,type,fd])       // default values n=1,type='l',fd=-1
  [err]=mput(x [,type,fd])    // default values type='l',fd=-1
  str=mgetstr([n,fd])         // default values n=1, fd=-1
  [err]=mputstr(str [, fd]);  // default value  fd = -1
  [err]=meof([fd])            // default value  fd = -1
  mseek(n [,fd, flag])        // default values fd = -1, flag = 'set'
  mtell([fd])                 // default value  fd = -1

PARAMETERS

  mode,type : strings.

  n,err,fd  : scalar

  x         : vector

DESCRIPTION
  A set of function to read and write binary files.

  The type parameter can be one of the following :

  "l","s","ul","us","d","f","c","uc"
               : for reading or writing respectively a long, a short, an
               unsigned long, an unsigned short, a double, a float, a char
               and an unsigned char. The bytes which are read are automati-
               cally swapped if necessary (by checking little-endian status )
               in order to produce machine independent binary files.

  The automatic swap of bytes can be cancelled by adding a third ( with value
  zero ) argument to the mopen function  ( mopen(file,"wb",0)).

  "ull","uls","ubl","ubs"
       : for reading or writing respectively unsigned little-endian long or
       short and unsigned big-endian long or short.

  The fd parameter returned by the function mopen is used as a file descrip-
  tor (it's a positive integer). When specifying the fd parameter, the value
  -1 refers to the default file ( i.e the last opened file).

  The mode parameter can be "rb" for read binary or "wb" for write binary.

  The function fseek() sets the position of the next input or output opera-
  tion on  the stream fd. The new position is at the signed distance given by
  n bytes from  the  beginning,  from the current position, or from the end
  of the file, according  to the flag value which can be 'set', 'cur' or
  'end'.

  The function ftell() returns the offset of the current byte  relative  to
  the beginning of the file associated with the named stream fd.

EXAMPLE
  filen = 'test.bin'
  mopen(filen,'wb');
  mput(1996,'ull');mput(1996,'uls');mput(1996,'ubl');mput(1996,'ubs');
  mput(1996,'l');mput(1996,'s');mput(98,'uc');mput(98,'c');
  mput(1996,'d');mput(1996,'f');mput(1996,'ul');mput(1996,'us');
  mclose();

  mopen(filen,'rb')
  if 1996<>mget(1,'ull') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'uls') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'ubl') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'ubs') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'l') ;write(%io(2),'BUG');end;
  if 1996<>mget(1,'s') ;write(%io(2),'Bug');end;
  if 98<>mget(1,'uc') ;write(%io(2),'Bug');end;
  if 98<>mget(1,'c') ;write(%io(2),'Bug');end;
  // with eventuel swap
  if 1996<>mget(1,'d') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'f') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'ul') ;write(%io(2),'Bug');end;
  if 1996<>mget(1,'us') ;write(%io(2),'Bug');end;
  mclose();

  // an example with two files

  file1 = 'test1.bin';
  file2 = 'test2.bin';
  fd1=mopen(file1,'wb');
  fd2=mopen(file2,'wb');
  mput(1996,'ull',fd1);
  mput(1996,'ull',fd2);
  mclose(fd1);
  mclose(fd2);

  fd1=mopen(file1,'rb');
  if 1996<>mget(1,'ull',fd1) ;write(%io(2),'Bug');end;
  fd2=mopen(file2,'rb');
  if 1996<>mget(1,'ull',fd2) ;write(%io(2),'Bug');end;
  mclose(fd1);
  mclose(fd2);

  // and example with mseek
  file3='test3.bin'
  fd1= mopen(file3,'wb');
  for i=1:10, mput(i,'d'); end
  mseek(0);
  mput(678,'d');
  mseek(0,fd1,'end');
  mput(932,'d');
  mclose(fd1)
  fd1= mopen(file3,'rb');
  res=mget(11,'d')
  res1=[1:11]; res1(1)=678;res1($)=932;
  if res1<>res ;write(%io(2),'Bug');end;
  mclose(fd1);