0022972: Eliminate macro definitions that has compiler-provided analogs (WNT and...
[occt.git] / src / OSD / OSD_Error.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _WIN32
16
17
18 #include <OSD_Error.hxx>
19 #include <OSD_ErrorList.hxx>
20 #include <OSD_OSDError.hxx>
21 #include <TCollection_AsciiString.hxx>
22
23 #include <stdio.h>
24 /* Created by Stephan GARNAUD (ARM) 1992 for Matra Datavision */
25 OSD_Error::OSD_Error(){
26  myErrno = 0;
27 }
28
29
30 void OSD_Error::Reset(){
31  myErrno = 0;
32 }
33
34 Standard_Boolean OSD_Error::Failed()const{
35  if (myErrno == 0) return (Standard_False);
36               else return (Standard_True);
37 }
38
39
40
41 void OSD_Error::SetValue(const Standard_Integer errcode,
42                          const Standard_Integer from,
43                          const TCollection_AsciiString& message){
44  myErrno = errcode;
45  myCode  = (OSD_WhoAmI)from;
46  myMessage = message;
47 }
48
49
50
51 Standard_Integer OSD_Error::Error()const{
52  return(extCode);
53 }
54
55  
56 void OSD_Error::Perror() {
57  TCollection_AsciiString buffer;
58
59  if (myErrno == 0) return;
60
61  buffer += " :\n ";
62  extCode = ERR_SURPRISE;
63
64  switch (myErrno){
65   case EBADF  :
66    switch (myCode){
67     case OSD_WFile:
68       buffer += "Invalid file descriptor or bad mode";
69       extCode = ERR_FBADF;
70       break;
71     default:
72       break;
73     }
74     break;
75
76
77 #ifdef SUN
78   case EBADMSG:
79    switch (myCode){
80     case OSD_WFile:
81      buffer += 
82      "The message waiting to be read on stream is not a data message";
83      extCode = ERR_FBADMSG;
84      break;
85     default:
86       break;
87    }
88    break;
89 #endif
90
91   case EINVAL: 
92    switch (myCode){
93     case OSD_WFileNode:
94       buffer += "Can't unlink '.' or '..'";
95       extCode = ERR_FNINVAL;
96       break;
97     case OSD_WFile:
98       buffer += "Invalid file descriptor";
99       extCode = ERR_FINVAL;
100       break;
101     default :
102       buffer += "User error : Bad parameter";
103       extCode = ERR_INVAL;
104       break;
105    }
106    break;
107 #if !defined(sun) && !defined(SOLARIS)
108   case EDQUOT :
109    switch (myCode){
110     case OSD_WDirectory:
111     case OSD_WFileNode:
112     case OSD_WFile :
113      buffer += "Exceed quota of disk blocks";
114      extCode = ERR_QUOT;
115      break;
116     default:
117       break;
118    }
119    break;
120 #endif
121
122 #ifdef SUN
123   case EDEADLK:
124    switch (myCode){
125     case OSD_WFile:
126      buffer += "Lock is already blocked by another process";
127      extCode = ERR_FDEADLK;
128      break;
129     default:
130       break;
131    }
132    break;
133 #endif
134
135   case ENOLCK:
136    switch (myCode){
137     case OSD_WFile:
138      buffer += "No more file lock entries available";
139      extCode = ERR_FNOLCK;
140      break;
141     default:
142       break;
143    }
144    break;
145   case EOPNOTSUPP:
146    switch (myCode){
147     case OSD_WFile:
148      buffer += "File descriptor doesn't refer to a file";
149      extCode = ERR_FWFD;
150      break;
151     default:
152      break;
153    }
154    break;
155   case EACCES: 
156     buffer += "Permission denied";
157     extCode = ERR_ACCESS;
158     break;
159   case EBUSY:
160    switch (myCode){
161     case OSD_WFileNode:
162      buffer += "Still used by system or a process";
163      extCode = ERR_FNBUSY;
164      break;
165     default:
166       break;
167    }
168    break;
169   case ERANGE:
170    switch (myCode){
171     case OSD_WFile:
172      buffer += "Not enough or too many bytes written";
173      extCode = ERR_FRANGE;
174      break;
175     default:
176       break;
177    }
178    break;
179   case EPERM: 
180    switch (myCode){
181     case OSD_WPackage:
182      buffer += "Permission denied";
183      extCode = ERR_PPERM;
184      break;
185     case OSD_WFileNode:
186      buffer += "Permission denied or can't unlink directory";
187      extCode = ERR_FPERM;
188      break;
189     default : 
190      buffer += "abnormal error : you modified OSD library";
191      extCode = ERR_PERM;
192      break;
193     }
194     break;
195   case EROFS:
196    switch (myCode){
197     case OSD_WFileNode:
198     case OSD_WFile:
199      buffer += "Read only file system";
200      extCode = ERR_ROFS;
201      break;
202     default:
203      break;
204    }
205    break;
206   case ENXIO:
207   case EIO :
208    switch (myCode){
209     case OSD_WDirectory:
210     case OSD_WFileNode:
211      buffer += "I/O error";
212      extCode = ERR_IO;
213      break;
214     case OSD_WFile :
215      buffer += "I/O error or Hang up from terminal";
216      extCode = ERR_FIO;
217      break;
218     default:
219      break;
220    }
221    break;
222   case EISDIR :
223    switch (myCode){
224     case OSD_WFileNode:
225     case OSD_WFile :
226      buffer += "The File is a Directory";
227      extCode = ERR_ISDIR;
228      break;
229     default:
230      break;
231     }
232     break;
233
234 #ifdef SUN
235   case EWOULDBLOCK:
236    switch (myCode){
237     case OSD_WFile:
238      buffer += "File is locked";
239      extCode = ERR_FLOCKED;
240      break;
241     default:
242      break;
243    }
244    break;
245 #endif
246
247 #ifdef IRIX4
248   case EWOULDBLOCK:
249    switch (myCode){
250     case OSD_WFile:
251      buffer += "File is locked";
252      extCode = ERR_FLOCKED;
253      break;
254     default:
255      break;
256    }
257    break;
258 #endif
259
260   case EAGAIN:
261    switch (myCode){
262     case OSD_WFile:
263      buffer += "No data ready to be read/written";
264      extCode = ERR_FAGAIN;
265      break;
266     default:
267      break;
268    }
269    break;
270   case ENOTDIR:
271    switch(myCode){
272     case OSD_WDirectory:
273     case OSD_WFileNode:
274     case OSD_WFile:
275      buffer += "A component of path is not a Directory";
276      extCode = ERR_NOTDIR;
277      break;
278     default:
279      break;
280    }
281    break;
282   case EMLINK:
283    switch (myCode){
284     case OSD_WDirectory:
285     buffer += "Too many links";
286     extCode = ERR_DMLINK;
287     break;
288     default:
289      break;
290    }
291    break;
292   case ELOOP :
293    switch (myCode){
294     case OSD_WDirectory:
295     case OSD_WFileNode:
296     case OSD_WFile :
297      buffer += "Too many symbolic links";
298      break;
299     default:
300      break;
301    }
302    break;
303   case EFAULT:
304    buffer += "User error : arguments point to an illegal address";
305    extCode = ERR_FAULT;
306    break;
307   case EFBIG:
308    switch (myCode){
309     case OSD_WFile:
310      buffer += "Exceed process's file size limit or the maximum file size";
311      extCode = ERR_FFBIG; 
312      break;
313     default:
314      break;
315    }
316    break;
317   case EINTR:
318    buffer += "operation breaked by a signal";
319    extCode = ERR_INTR;
320    break;
321   case ENOMEM: 
322    buffer += "Not enough memory";
323    extCode = ERR_NOMEM;
324    break;
325   case EMFILE : 
326    switch(myCode){   
327     case OSD_WFile :
328        buffer += "Too many file descriptors are currently in use by this process";
329        extCode = ERR_FMFILE;
330        break;
331     default:
332      break;
333     }
334     break;
335   case ENAMETOOLONG :
336    buffer += "File name too long";
337    extCode = ERR_NAMETOOLONG;
338    break;
339   case ENFILE :
340    switch (myCode){
341     case OSD_WFile:
342      buffer += "Too many files are currently open in the system";
343      extCode = ERR_FNFILE;
344      break;
345     default:
346      break;
347    }
348    break;
349   case EXDEV:
350    switch (myCode){
351     case OSD_WFileNode:
352      buffer += "The link named by path2 and the file named by path1 are\n";
353      buffer += "on different logical devices (file systems)";
354      extCode = ERR_FNXDEV;
355      break;
356     default:
357      break;
358    }
359    break;
360   case ENOENT:
361    switch (myCode){
362     case OSD_WFileNode:
363     case OSD_WFile:
364      if (myMessage != "Open") buffer += "File doesn't exist or";
365       buffer += "Invalid path (empty string)";
366      extCode = ERR_NOENT;
367      break;
368     case OSD_WDirectory:
369      buffer += "A component of the path prefix of path does not exist";
370      extCode = ERR_DNOENT;
371      break;
372     default:
373      break;
374     }
375     break;
376   case ENOSPC: {
377    switch (myCode){
378     case OSD_WDirectory:
379     case OSD_WFile:
380      buffer += "No more free space on file system";
381      extCode = ERR_FNOSPC;
382      break;
383     default:
384      break;
385    }
386    break;
387  }
388
389 //
390 // AIX maps ENOTEMPTY to EEXIST.  Move this case block to
391 // the EEXIST case block.
392 //
393 #if (!defined(_AIX)) && (!defined(AIX))
394   case ENOTEMPTY:
395    switch (myCode){
396     case OSD_WFileNode:
397      buffer += "Directory not empty";
398      extCode = ERR_FNNOTEMPTY;
399      break;
400     default:
401      break;
402    }
403    break;
404 #endif
405
406   case EEXIST: 
407    switch(myCode){
408     case OSD_WFileNode:
409      buffer += "Directory not empty";
410      extCode = ERR_FNNOTEMPTY;
411      break;
412     case OSD_WFile:
413      buffer += "OSD_Create and OSD_Exclude are set and the named file exists";
414      extCode = ERR_FEXIST;
415      break;
416     default:
417      buffer += "Identifier already exists for this key";
418      extCode = ERR_EXIST;
419      break;
420    }
421    break;
422   case E2BIG: 
423    buffer += "Too many Semaphore/Shared memory for a process.";
424    buffer += "Reconfigure Kernel with greater values";
425    extCode = ERR_TOOBIG;
426    break;
427    default: {
428      Standard_Character buf[255];
429      //
430      sprintf(buf,"%sUnknowm error #%d",buffer.ToCString(),myErrno);
431      TCollection_AsciiString interm(buf);
432      buffer = interm;
433      extCode = ERR_UNKNOWN;
434    }
435  }
436  buffer += ".\n\n";
437  OSD_OSDError::Raise (buffer.ToCString());
438 }
439
440 #else
441
442 //------------------------------------------------------------------------
443 //-------------------  Windows NT sources for OSD_Error ------------------
444 //------------------------------------------------------------------------
445
446 #define STRICT
447 #include <OSD_Error.hxx>
448 #include <OSD_ErrorList.hxx>
449 #include <TCollection_ExtendedString.hxx>
450
451 #include <windows.h>
452
453 typedef struct _error_table {
454
455                 DWORD            wnt_error;
456                 Standard_Integer csf_error;
457
458                } ERROR_TABLE;
459
460 static int      fPrefix     = 1;
461 static ostream* errorStream = &cerr;
462
463 static ERROR_TABLE commErrorTable [] = {
464
465  { ERROR_INVALID_FUNCTION,      ERR_INVAL       },
466  { ERROR_FILE_NOT_FOUND,        ERR_NOENT       },
467  { ERROR_PATH_NOT_FOUND,        ERR_NOENT       },
468  { ERROR_ACCESS_DENIED,         ERR_ACCESS      },
469  { ERROR_ARENA_TRASHED,         ERR_NOMEM       },
470  { ERROR_NOT_ENOUGH_MEMORY,     ERR_NOMEM       },
471  { ERROR_INVALID_BLOCK,         ERR_NOMEM       },
472  { ERROR_BAD_ENVIRONMENT,       ERR_TOOBIG      },
473  { ERROR_INVALID_ACCESS,        ERR_INVAL       },
474  { ERROR_INVALID_DATA,          ERR_INVAL       },
475  { ERROR_INVALID_DRIVE,         ERR_NOENT       },
476  { ERROR_CURRENT_DIRECTORY,     ERR_ACCESS      },
477  { ERROR_NO_MORE_FILES,         ERR_NOENT       },
478  { ERROR_LOCK_VIOLATION,        ERR_ACCESS      },
479  { ERROR_SHARING_VIOLATION,     ERR_ACCESS      },
480  { ERROR_BAD_NETPATH,           ERR_NOENT       },
481  { ERROR_NETWORK_ACCESS_DENIED, ERR_ACCESS      },
482  { ERROR_BAD_NET_NAME,          ERR_NOENT       },
483  { ERROR_FILE_EXISTS,           ERR_EXIST       },
484  { ERROR_CANNOT_MAKE,           ERR_ACCESS      },
485  { ERROR_FAIL_I24,              ERR_ACCESS      },
486  { ERROR_INVALID_PARAMETER,     ERR_INVAL       },
487  { ERROR_DRIVE_LOCKED,          ERR_ACCESS      },
488  { ERROR_INVALID_HANDLE,        ERR_INVAL       },
489  { ERROR_NEGATIVE_SEEK,         ERR_INVAL       },
490  { ERROR_SEEK_ON_DEVICE,        ERR_ACCESS      },
491  { ERROR_NOT_LOCKED,            ERR_ACCESS      },
492  { ERROR_BAD_PATHNAME,          ERR_NOENT       },
493  { ERROR_LOCK_FAILED,           ERR_ACCESS      },
494  { ERROR_ALREADY_EXISTS,        ERR_EXIST       },
495  { ERROR_FILENAME_EXCED_RANGE,  ERR_NOENT       },
496  { ERROR_NOT_ENOUGH_QUOTA,      ERR_QUOT        },
497  { ERROR_IO_DEVICE,             ERR_IO          },
498  { ERROR_INVALID_BLOCK,         ERR_FAULT       },
499  { ERROR_BAD_THREADID_ADDR,     ERR_FAULT       },
500  { ERROR_INVALID_ADDRESS,       ERR_FAULT       },
501  { ERROR_MAPPED_ALIGNMENT,      ERR_FAULT       },
502  { ERROR_BUFFER_OVERFLOW,       ERR_NAMETOOLONG }
503
504 };
505
506 #define COMM_ERR_TABLE_SIZE (int)(sizeof(commErrorTable) / sizeof(commErrorTable[0]))
507
508 static ERROR_TABLE dirErrorTable[] = {
509
510  { ERROR_FILE_NOT_FOUND,       ERR_NOENT },
511  { ERROR_PATH_NOT_FOUND,       ERR_NOENT },
512  { ERROR_INVALID_DRIVE,        ERR_NOENT },
513  { ERROR_NO_MORE_FILES,        ERR_NOENT },
514  { ERROR_BAD_NETPATH,          ERR_NOENT },
515  { ERROR_BAD_NET_NAME,         ERR_NOENT },
516  { ERROR_BAD_PATHNAME,         ERR_NOENT },
517  { ERROR_FILENAME_EXCED_RANGE, ERR_NOENT }
518
519 };
520
521 #define DIR_ERR_TABLE_SIZE (int)(sizeof(dirErrorTable) / sizeof(dirErrorTable[0]))
522
523 static ERROR_TABLE fileErrorTable[] = {
524
525  { ERROR_INVALID_HANDLE,        ERR_FBADF   },
526  { ERROR_INVALID_TARGET_HANDLE, ERR_FBADF   },
527  { ERROR_DIRECT_ACCESS_HANDLE,  ERR_FBADF   },
528  { ERROR_FILE_EXISTS,           ERR_EXIST   },
529  { ERROR_ALREADY_EXISTS,        ERR_EXIST   },
530  { ERROR_TOO_MANY_OPEN_FILES,   ERR_FMFILE  },
531  { ERROR_INVALID_FUNCTION,      ERR_FINVAL  },
532  { ERROR_INVALID_ACCESS,        ERR_FINVAL  },
533  { ERROR_INVALID_DATA,          ERR_FINVAL  },
534  { ERROR_INVALID_PARAMETER,     ERR_FINVAL  },
535  { ERROR_INVALID_HANDLE,        ERR_FINVAL  },
536  { ERROR_NEGATIVE_SEEK,         ERR_FINVAL  },
537  { ERROR_IO_PENDING,            ERR_FAGAIN  },
538  { ERROR_WRITE_FAULT,           ERR_FIO     },
539  { ERROR_READ_FAULT,            ERR_FIO     },
540  { ERROR_NET_WRITE_FAULT,       ERR_FIO     },
541  { ERROR_IO_DEVICE,             ERR_FIO     },
542  { ERROR_LOCK_VIOLATION,        ERR_FLOCKED },
543  { ERROR_LOCK_FAILED,           ERR_FLOCKED }
544
545 };
546
547 #define FILE_ERR_TABLE_SIZE (int)(sizeof(fileErrorTable) / sizeof(fileErrorTable[0]))
548
549 static ERROR_TABLE fileNodeErrorTable[] = {
550
551  { ERROR_INVALID_FUNCTION,      ERR_FNINVAL    },
552  { ERROR_INVALID_ACCESS,        ERR_FNINVAL    },
553  { ERROR_INVALID_DATA,          ERR_FNINVAL    },
554  { ERROR_INVALID_PARAMETER,     ERR_FNINVAL    },
555  { ERROR_INVALID_HANDLE,        ERR_FNINVAL    },
556  { ERROR_NEGATIVE_SEEK,         ERR_FNINVAL    },
557  { ERROR_DISK_FULL,             ERR_FNOSPC     },
558  { ERROR_DIR_NOT_EMPTY,         ERR_FNNOTEMPTY },
559  { ERROR_NOT_SAME_DEVICE,       ERR_FNXDEV     }
560
561 };
562
563 #define FILE_NODE_ERR_TABLE_SIZE (int)(sizeof(fileNodeErrorTable) / sizeof(fileNodeErrorTable[0]))
564
565 static Standard_Integer _get_comm_error ( DWORD );
566
567 OSD_Error :: OSD_Error () : 
568    myCode((OSD_WhoAmI)0),
569    extCode(0)
570 {
571  Reset ();
572 }  // end constructor ( 1 )
573
574 void OSD_Error :: Perror () {
575
576   if (errorStream == NULL)
577     return;
578
579  Standard_Character buff[ 32 ];
580  Standard_CString   ptr;
581
582  if ( fPrefix ) {
583  
584   lstrcpy (  buff, "Error ( "  );
585
586   switch ( myCode ) {
587   
588    case OSD_WDirectoryIterator:
589
590     ptr = "OSD_DirectoryIterator";
591
592    break;
593
594    case OSD_WDirectory:
595
596     ptr = "OSD_Directory";
597
598    break;
599
600    case OSD_WFileIterator:
601
602     ptr = "OSD_FileIterator";
603
604    break;
605
606    case OSD_WFile:
607   
608     ptr = "OSD_File";
609
610    break;
611
612    case OSD_WFileNode:
613
614     ptr = "OSD_FileNode";
615
616    break;
617
618    case OSD_WHost:
619
620     ptr = "OSD_Host";
621
622    break;
623
624    case OSD_WProcess:
625
626     ptr = "OSD_Environment";
627
628    break;
629
630    case OSD_WEnvironmentIterator:
631
632     ptr = "OSD_EnvironmentIterator";
633
634    break;
635
636    case OSD_WEnvironment:
637
638     ptr = "OSD_Environment";
639
640    break;
641
642    case OSD_WDisk:
643
644     ptr = "OSD_Disk";
645
646    break;
647
648    default:
649
650     ptr = "Unknown";
651
652   }  // end switch
653
654   lstrcat ( buff, ptr );
655   lstrcat (  buff, " )"  );
656   ( *errorStream ) << buff;
657  
658  }  // end if ( fPrefix . . . )
659
660  TCollection_ExtendedString aMessageW(myMessage);
661  ( *errorStream ) << L": " << (const wchar_t*)aMessageW.ToExtString () << endl << flush;
662
663 }  // end OSD_Error :: Perror
664
665 void OSD_Error :: SetValue (
666                    const Standard_Integer Errcode,
667                    const Standard_Integer From,
668                    const TCollection_AsciiString& Message
669                   ) {
670
671  int i;
672
673  myErrno   = Errcode;
674  myCode    = ( OSD_WhoAmI )From;
675  myMessage = Message;
676
677  switch ( From ) {
678  
679   case OSD_WDirectory:
680
681    for ( i = 0; i < DIR_ERR_TABLE_SIZE; ++i )
682
683     if ( dirErrorTable[ i ].wnt_error == ( DWORD )Errcode ) {
684
685      extCode = dirErrorTable[ i ].csf_error;
686      break;
687
688    }  // end if
689
690   if ( i == DIR_ERR_TABLE_SIZE ) extCode = _get_comm_error ( Errcode );
691
692   break;
693
694   case OSD_WFile:
695
696    for ( i = 0; i < FILE_ERR_TABLE_SIZE; ++i )
697
698     if ( fileErrorTable[ i ].wnt_error == ( DWORD )Errcode ) {
699
700      extCode = fileErrorTable[ i ].csf_error;
701      break;
702
703    }  // end if
704
705   if ( i == FILE_ERR_TABLE_SIZE ) extCode = _get_comm_error ( Errcode );
706
707   break;
708  
709   case OSD_WFileNode:
710
711    for ( i = 0; i < FILE_NODE_ERR_TABLE_SIZE; ++i )
712
713     if ( fileNodeErrorTable[ i ].wnt_error == ( DWORD )Errcode ) {
714
715      extCode = fileNodeErrorTable[ i ].csf_error;
716      break;
717
718    }  // end if
719
720   if ( i == FILE_NODE_ERR_TABLE_SIZE ) extCode = _get_comm_error ( Errcode );
721
722   break;
723
724   default:
725
726    extCode = _get_comm_error ( Errcode );
727
728  }  // end switch
729
730 }  // end OSD_Error :: SetValue
731
732 Standard_Integer OSD_Error :: Error () const {
733
734  return extCode;
735
736 }  // end OSD_Error :: Error
737
738 Standard_Boolean OSD_Error :: Failed () const {
739
740  return myErrno == ERROR_SUCCESS ? Standard_False : Standard_True;
741
742 }  // end OSD_Error :: Failed
743
744 void OSD_Error :: Reset () {
745
746  myErrno = ERROR_SUCCESS; 
747  if (errorStream != NULL)
748  {
749    ( *errorStream ).clear ();
750    ( *errorStream ).seekp ( 0 );
751    ( *errorStream ).clear ();
752  }
753
754 }  // end OSD_Error :: Reset
755
756 void SetErrorStream ( ostream* errStream ) {
757
758  errorStream = errStream;
759
760 }  // end SetErrorStream
761
762 void EnablePrefix ( int fEnable ) {
763
764  fPrefix = fEnable;
765
766 }  // end EnablePrefix
767
768 int ErrorPrefix ( void ) {
769
770  return fPrefix;
771
772 }  // end ErrorPrefix
773
774 ostream* ErrorStream ( void ) {
775
776  return errorStream;
777
778 }  // end ErrorStream
779
780 static Standard_Integer _get_comm_error ( DWORD dwCode ) {
781
782  int              i;
783  Standard_Integer retVal = ERR_SURPRISE;
784
785  for ( i = 0; i < COMM_ERR_TABLE_SIZE; ++i )
786
787   if ( commErrorTable[ i ].wnt_error == ( DWORD )dwCode ) {
788
789    retVal = commErrorTable[ i ].csf_error;
790    break;
791
792   }  // end if
793
794  return retVal;
795
796 }  // end _get_comm_error
797
798 #endif