home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / endiandemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  4.7 KB  |  147 lines

  1. {
  2. GPC demo program for the endianness conversion routines.
  3.  
  4. Copyright (C) 2000-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, version 2.
  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. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21.  
  22. As a special exception, if you incorporate even large parts of the
  23. code of this demo program into another program with substantially
  24. different functionality, this does not cause the other program to
  25. be covered by the GNU General Public License. This exception does
  26. not however invalidate any other reasons why it might be covered
  27. by the GNU General Public License.
  28. }
  29.  
  30. program EndianDemo;
  31.  
  32. uses GPC;
  33.  
  34. { To get reproducable data sizes in the file }
  35. type
  36.   {$ifdef __GPC__}
  37.   Int16 = Integer (16);
  38.   Int32 = Integer (32);
  39.   {$else}
  40.   Int16 = Integer;
  41.   Int32 = LongInt;
  42.   {$endif}
  43.  
  44. var
  45.   a, b, k, l : array [1 .. 10] of Int16;
  46.   c, d, m, n : array [1 .. 10] of Int32;
  47.   s, t, u, v : String [20];
  48.   i : Int32;
  49.   BytesRead : Integer;
  50.   f : file;
  51.   Buf : array [1 .. $1000] of Byte;
  52.  
  53. begin
  54.   { Initialize the variables }
  55.   s := 'foo';
  56.   t := 'bar';
  57.   u := '';
  58.   v := '';
  59.   for i := 1 to 10 do
  60.     begin
  61.       a [i] := 100 * i;
  62.       b [i] := 0;
  63.       c [i] := 10000 * i;
  64.       d [i] := 0;
  65.       k [i] := 42 * i;
  66.       l [i] := 0;
  67.       m [i] := $f00 * i;
  68.       n [i] := 0
  69.     end;
  70.  
  71.   { Create a file }
  72.   Assign (f, 'tmpfile.dat');
  73.   Rewrite (f, 1);
  74.   { Write the first part of an array to the file in little endian format... }
  75.   BlockWriteLittleEndian (f, a, SizeOf (a [1]), 5);  (*@@iocritical*)CheckInOutRes;
  76.   { ...and the rest in big endian format (probably quite pointless, but
  77.     this is just a demo ;-) }
  78.   BlockWriteBigEndian (f, a [6], SizeOf (a [1]), 5); (*@@iocritical*)CheckInOutRes;
  79.   { Again for an Int32 array }
  80.   BlockWriteLittleEndian (f, c, SizeOf (c [1]), 5);  (*@@iocritical*)CheckInOutRes;
  81.   BlockWriteBigEndian (f, c [6], SizeOf (c [1]), 5); (*@@iocritical*)CheckInOutRes;
  82.   { Now write a string in little endian format... }
  83.   WriteStringLittleEndian (f, s);                    (*@@iocritical*)CheckInOutRes;
  84.   { ...and another one in big endian format }
  85.   WriteStringBigEndian (f, t);                       (*@@iocritical*)CheckInOutRes;
  86.  
  87.   { Just a check (see below) }
  88.   if (k [10] <> 420) or (m [10] <> $9600) then
  89.     begin
  90.       Writeln (StdErr, 'internal error in variable initializtion');
  91.       Halt (1)
  92.     end;
  93.  
  94.   { Convert k and m in place, then write them to the file }
  95.   ConvertToLittleEndian (k, SizeOf (k [1]), 10);
  96.   ConvertToBigEndian (m, SizeOf (m [1]), 10);
  97.   BlockWrite (f, k, SizeOf (k));
  98.   BlockWrite (f, m, SizeOf (m));
  99.  
  100.   { Now, depending on the system's endianness, either k or m is in
  101.     the wrong format and contains the wrong value for the system. }
  102.   if (k [10] = 420) and (m [10] = $9600) then
  103.     begin
  104.       Writeln (StdErr, 'internal error: endianness conversion not correct');
  105.       Halt (1)
  106.     end;
  107.  
  108.   { Read back the values in their respective endianness }
  109.   Reset (f, 1);
  110.   BlockReadLittleEndian (f, b, SizeOf (b [1]), 5);   (*@@iocritical*)CheckInOutRes;
  111.   BlockReadBigEndian (f, b [6], SizeOf (b [1]), 5);  (*@@iocritical*)CheckInOutRes;
  112.   BlockReadLittleEndian (f, d, SizeOf (d [1]), 5);   (*@@iocritical*)CheckInOutRes;
  113.   BlockReadBigEndian (f, d [6], SizeOf (d [1]), 5);  (*@@iocritical*)CheckInOutRes;
  114.   ReadStringLittleEndian (f, u);                     (*@@iocritical*)CheckInOutRes;
  115.   ReadStringBigEndian (f, v);                        (*@@iocritical*)CheckInOutRes;
  116.   BlockRead (f, l, SizeOf (l));
  117.   BlockRead (f, n, SizeOf (n));
  118.   ConvertFromLittleEndian (l, SizeOf (l [1]), 10);
  119.   ConvertFromBigEndian (n, SizeOf (n [1]), 10);
  120.  
  121.   { Output the values read }
  122.   Writeln ('Values read:');
  123.   for i := 1 to 10 do Write (b [i], ', ');
  124.   Writeln;
  125.   for i := 1 to 10 do Write (d [i], ', ');
  126.   Writeln;
  127.   Writeln (u);
  128.   Writeln (v);
  129.   for i := 1 to 10 do Write (l [i], ', ');
  130.   Writeln;
  131.   for i := 1 to 10 do Write (n [i], ', ');
  132.   Writeln;
  133.   Writeln;
  134.  
  135.   { Dump the bytes of the file }
  136.   Reset (f, 1);
  137.   BlockRead (f, Buf, SizeOf (Buf), BytesRead);
  138.   CloseFile (f);
  139.   Writeln ('Binary file dump:');
  140.   for i := 1 to BytesRead do
  141.     begin
  142.       Write (Buf [i], ', ');
  143.       if i mod 16 = 0 then Writeln
  144.     end;
  145.   Writeln
  146. end.
  147.