Recent

Author Topic: How to convert all values of ioresult() into text messages?  (Read 8691 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 905
How to convert all values of ioresult() into text messages?
« on: September 30, 2019, 07:07:21 pm »
I'm looking for a function which converts the values of function system.ioresult() into text messages on Windows and Linux. I didn't find one.

The only thing I found is function sysutils.SysErrorMessage(), but this does not match: e.g. if you write to a full disk, ioresult() returns 101 (both on Windows 7 and on Linux Ubuntu 18.04). But SysErrorMessage(101) returns "The exclusive Semaphore belongs to another process" on Windows and "Network is unreachable" on Linux. Another example: if you write on a write-protected medium, ioresult() returns 150 on Windows, but SysErrorMessage(150) returns "No system trace information was specified in file CONFIG.SYS".

So does a function exist to convert ioresult() values into text messages or must I create this function by myself?
Thanks in advance.
« Last Edit: September 30, 2019, 08:22:45 pm by Hartmut »

Handoko

  • Hero Member
  • *****
  • Posts: 5425
  • My goal: build my own game engine using Lazarus
Re: How to convert the values of ioresult() into text messages?
« Reply #1 on: September 30, 2019, 07:39:12 pm »
Not sure, but you can try to write a function for it:

Code: Pascal  [Select][+][-]
  1. function ErrorString(e: Integer): string;
  2. begin
  3.   case e of
  4.       1: Result := 'Invalid function number';
  5.       2: Result := 'File not found.';
  6.       3: Result := 'Path not found.';
  7.       4: Result := 'Too many open files.';
  8.       5: Result := 'Access denied.';
  9.       6: Result := 'Invalid file handle.';
  10.      12: Result := 'Invalid file-access mode.';
  11.      15: Result := 'Invalid disk number.';
  12.      16: Result := 'Cannot remove current directory.';
  13.      17: Result := 'Cannot rename across volumes.';
  14.     100: Result := 'Error when reading from disk.';
  15.     101: Result := 'Error when writing to disk.';
  16.     102: Result := 'File not assigned.';
  17.     103: Result := 'File not open.';
  18.     104: Result := 'File not opened for input.';
  19.     105: Result := 'File not opened for output.';
  20.     106: Result := 'Invalid number.';
  21.     150: Result := 'Disk is write protected.';
  22.     151: Result := 'Unknown device.';
  23.     152: Result := 'Drive not ready.';
  24.     153: Result := 'Unknown command.';
  25.     154: Result := 'CRC check failed.';
  26.     155: Result := 'Invalid drive specified..';
  27.     156: Result := 'Seek error on disk.';
  28.     157: Result := 'Invalid media type.';
  29.     158: Result := 'Sector not found.';
  30.     159: Result := 'Printer out of paper.';
  31.     160: Result := 'Error when writing to device.';
  32.     161: Result := 'Error when reading from device.';
  33.     162: Result := 'Hardware failure.';
  34.     200: Result := 'Division by zero';
  35.     201: Result := 'Range check error';
  36.     202: Result := 'Stack overflow error';
  37.     203: Result := 'Heap overflow error';
  38.     204: Result := 'Invalid pointer operation';
  39.     205: Result := 'Floating point overflow';
  40.     206: Result := 'Floating point underflow';
  41.     207: Result := 'Invalid floating point operation';
  42.     210: Result := 'Object not initialized';
  43.     211: Result := 'Call to abstract method';
  44.     212: Result := 'Stream registration error';
  45.     213: Result := 'Collection index out of range';
  46.     214: Result := 'Collection overflow error';
  47.     215: Result := 'Arithmetic overflow error';
  48.     216: Result := 'General protection fault';
  49.   end;
  50. end;

Sources:
https://www.freepascal.org/docs-html/rtl/system/ioresult.html
http://www.delphifaq.com/faq/delphi/delphi_ide/f153.shtml

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to convert the values of ioresult() into text messages?
« Reply #2 on: September 30, 2019, 07:44:21 pm »
Handokos Function is ok until error 162 but the errors above 200 are no ioerrors:

INPUT-/OUTPUT-error.


Hartmut

  • Hero Member
  • *****
  • Posts: 905
Re: How to convert the values of ioresult() into text messages?
« Reply #3 on: September 30, 2019, 08:09:36 pm »
Thanks to Handoko and winni for your answers. The problem with writing my own function ist to get a complete list of all really possible error codes. https://www.freepascal.org/docs-html/rtl/system/ioresult.html shows 29 error codes, but is not complete. During the years I stumbled over:
 - error 18 comes on Linux if you try to rename across drives (although error 17 is declared for that)
 - error 80 comes on Windows if you try to rename across drives and the destination exists
 - error 84 comes on Linux if you have bad characters in a filename which are not UTF8 coded
 - error 123 comes on Windows if a filename has illegal characters
 - error 131 comes on Windows if you seek() to a negative value
 - error 218 comes on Linux if you seek() to a negative value or have a filename with UTF8-characters on a FAT drive

I'm sure this list is not complete. Does anybody know more error codes that can be returned by ioresult() on Windows or Linux and their meaning?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to convert all values of ioresult() into text messages?
« Reply #4 on: September 30, 2019, 08:32:19 pm »
And some years ago ioresult 107 was implemented but never documented. From memory I think it had to do with

Code: Pascal  [Select][+][-]
  1. write(enum)

Hartmut - it looks like that you know more about IOerrors that the internet and the fpc documentation. Write the final function IOerrorMsg !

Winni

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: How to convert the values of ioresult() into text messages?
« Reply #5 on: September 30, 2019, 08:36:27 pm »
See also: https://www.freepascal.org/docs-html/rtl/system/ioresult.html
Note there is some leeway in the mapping: I have seen free slots (but in the correct category ) set in proprietary solutions. IOResult is writeable:
Code: Pascal  [Select][+][-]
  1. {$I-}
  2. begin
  3.   InOutRes := 80; // non-default slot
  4.   writeln(IOResult);
  5. end.
« Last Edit: September 30, 2019, 08:55:18 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to convert all values of ioresult() into text messages?
« Reply #6 on: September 30, 2019, 08:44:30 pm »
Thaddy - this list is not complete!

That is Hartmut's problem!

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to convert all values of ioresult() into text messages?
« Reply #7 on: September 30, 2019, 08:52:50 pm »
And there was IOerror 32 on Windows. It appeared at shared network drives when somebody had opened a file exlusivly and you tried to open it - even only for ro.

I don't know if it's deprecated.

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: How to convert all values of ioresult() into text messages?
« Reply #8 on: September 30, 2019, 08:59:26 pm »
Thaddy - this list is not complete!
The list IS complete for all standard error slots in Pascal.
I added an example to my answer. There is simply room for non-standard error slots.
That is OK as long as the category is correct.
Your answer was wrong anyway, because the IOResult is a word size and can be set programattically, as I demonstrated.
This is an old but trusted technique to some (although very early it was byte size? - have to check that - , you could still use the above code.)

Custom code can look like this:
Code: Pascal  [Select][+][-]
  1. type
  2.   DosErrors = 2..99;
  3.   IOErrors = 100..149;
  4.   FatalErrors = 150 ..199;
  5.   CompilerErrors = 200..255;
  6.   CustomErrors = 256..High(word);  
  7. {$I-}
  8. begin
  9.   InOutRes := 1234; // non-default slot in user area
  10.   writeln(IOResult);
  11. end.
« Last Edit: September 30, 2019, 09:11:20 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to convert all values of ioresult() into text messages?
« Reply #9 on: September 30, 2019, 09:11:05 pm »
@Thaddy

IOResult 32 appeared at WfW (windows for Workgroups, 16 Bit) around 1993.
Free Pascal was not born then. Why does fpc not take care of the reality?

That you can assign values to Ioresult is well known and no scecret.

Winni


440bx

  • Hero Member
  • *****
  • Posts: 5302
Re: How to convert all values of ioresult() into text messages?
« Reply #10 on: September 30, 2019, 09:12:31 pm »
I'm looking for a function which converts the values of function system.ioresult() into text messages on Windows and Linux. I didn't find one.
From the responses you got above, my guess is there is no complete, up to date, list of IoResult values around you could use to produce such a function.

If that is correct (and it seems to be), doing a text search (grep or equivalent) in the compiler sources for the identifier "InOutRes" reveals the locations and values assigned to that variable which is what is returned by IoResult (using the word pointer HInOutRes.)

IOW, with a reasonable effort you can make an accurate list of  values returned by IoResult per O/S.

Note that for some OSs (maybe all, I don't know) there seem to be places where InOutRes is assigned whatever value is held in the variable errno.  In those cases you'll have to determine the possible values of errno.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: How to convert all values of ioresult() into text messages?
« Reply #11 on: September 30, 2019, 09:15:12 pm »
It is documented here:

https://www.freepascal.org/docs-html/rtl/system/inoutres.html

It is rather simple to read the manual.
Apart from the IOResult list as documented here https://www.freepascal.org/docs-html/rtl/system/ioresult.html and the run-time errors documented here: https://www.freepascal.org/docs-html/current/user/userap4.html#x190-197000D the actual list may vary on a per programmer basis.
For convenience I added category type ranges to my second example, but even then it would be unfeasable to rely on undocumented slots.
(I believe 107 will be documented in trunk or 3.2.0 )
« Last Edit: September 30, 2019, 09:19:22 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Hartmut

  • Hero Member
  • *****
  • Posts: 905
Re: How to convert all values of ioresult() into text messages?
« Reply #12 on: September 30, 2019, 09:17:44 pm »
The list IS complete for all standard error slots in Pascal.
Hello Thaddy, what do you mean with "slots"? I don't understand.

I want to contradict you, that the list in https://www.freepascal.org/docs-html/rtl/system/ioresult.html would be complete. In reply #3 I gave at least 6 examples of error codes, which are missing. All these error codes occured with a plain FPC 3.0.4 program, without manipulating 'InOutRes'. If you want to verify that and need more information than I gave in reply #3, please let me know.

Thaddy

  • Hero Member
  • *****
  • Posts: 16945
  • Ceterum censeo Trump esse delendam
Re: How to convert all values of ioresult() into text messages?
« Reply #13 on: September 30, 2019, 09:22:22 pm »
Think of a free slot as a free space within a certain range. See the types I added for explanation.
If a certain error fails into a certain category, but is not used, you can use it yourself.
If that is good practice is another matter. I would prefer custom errors in the range 1000 and above.

Example:
Code: Pascal  [Select][+][-]
  1. {$I-}
  2. var
  3.   s:string = ' Hartmut';
  4. begin
  5.   if s = ' Einstein' then
  6.     InOutRes := $dead;  // a free slot above 1000
  7.   writeln(IOResult,s);
  8. end.

I am aware the examples are rather similar......
« Last Edit: September 30, 2019, 09:28:48 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Hartmut

  • Hero Member
  • *****
  • Posts: 905
Re: How to convert all values of ioresult() into text messages?
« Reply #14 on: September 30, 2019, 09:30:20 pm »
Ok, Thaddy, now I understand what you mean with 'slots'. Thanks. But this thread is not for how to set custom error codes, but to get a list of the original FPC error codes which can be returned by ioresult() on Windows or Linux.

 

TinyPortal © 2005-2018