Changeset 1190:eb9a8f15de24


Ignore:
Timestamp:
03/23/09 09:58:31 (3 years ago)
Author:
niam
Branch:
default
Message:

use internal type file for file handle

Location:
trunk
Files:
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/include/libdodo/ioFileFifo.h

    r1183 r1190  
    4040        namespace io 
    4141        { 
     42                struct __file; 
     43 
    4244                namespace file 
    4345                { 
     
    180182                                short mode;         ///< file open mode[see fileOpenmodeEnum] 
    181183 
    182                                 void *handler;      ///< file handler 
     184                                __file *handle;      ///< file handle 
    183185 
    184186                                bool blocked;       ///< true if stream is blocked 
  • trunk/include/libdodo/ioFileRegular.h

    r1183 r1190  
    4040        namespace io 
    4141        { 
     42                struct __file; 
     43 
    4244                namespace file 
    4345                { 
     
    185187                                short mode;         ///< file open mode[see fileOpenmodeEnum] 
    186188 
    187                                 void *handler;      ///< file handler 
     189                                __file *handle;      ///< file handle 
    188190                        }; 
    189191                }; 
  • trunk/include/libdodo/ioFileTemp.h

    r1183 r1190  
    3939        namespace io 
    4040        { 
     41                struct __file; 
     42 
    4143                namespace file 
    4244                { 
     
    159161                          private: 
    160162 
    161                                 void *handler; ///< file handler 
     163                                __file *handle; ///< file handle 
    162164                        }; 
    163165                }; 
  • trunk/include/libdodo/ioPipe.h

    r1189 r1190  
    4343                        struct __peerInfo; 
    4444                }; 
     45 
     46                struct __file; 
    4547 
    4648                /** 
     
    160162                        virtual void _writeStream(const char * const data); 
    161163 
    162                         void *in;     ///< input stream descriptor 
    163                         void *out;    ///< output stream descriptor 
     164                        __file *in;     ///< input stream descriptor 
     165                        __file *out;    ///< output stream descriptor 
    164166 
    165167                  private: 
  • trunk/include/libdodo/ioStdio.h

    r1166 r1190  
    3131#define _IOSTDIO_H_ 1 
    3232 
    33 #include <fcntl.h> 
    34 #include <sys/un.h> 
    35 #include <sys/socket.h> 
    36 #include <netinet/in.h> 
    37 #include <arpa/inet.h> 
    38 #include <netdb.h> 
    39  
    4033#include <libdodo/directives.h> 
    4134 
    42 #include <libdodo/types.h> 
    4335#include <libdodo/ioPipe.h> 
    4436 
  • trunk/src/ioFile.inline

    r1183 r1190  
    1  
    21/*************************************************************************** 
    3  *            ioFileRegular.cc 
     2 *            ioFile.inline 
    43 * 
    5  *  Wed Oct 8 01:44:18 2005 
    6  *  Copyright  2005  Ni@m 
     4 *  Mon Mar 23 10:23:18 2009 
     5 *  Copyright  2009  Ni@m 
    76 *  niam.niam@gmail.com 
    87 ****************************************************************************/ 
     
    3130#include <libdodo/directives.h> 
    3231 
    33 #include <unistd.h> 
    34 #include <sys/types.h> 
    35 #include <sys/stat.h> 
     32#include <stdio.h> 
    3633 
    37 #include <libdodo/ioFileRegular.h> 
    38 #include <libdodo/toolsFilesystem.h> 
    39 #include <libdodo/ioFileRegularEx.h> 
    40 #include <libdodo/types.h> 
    41 #include <libdodo/pcSyncProtector.h> 
    42 #include <libdodo/ioChannel.h> 
    43  
    44 using namespace dodo::io::file; 
    45  
    46 regular::regular(short protection) : overwrite(false), 
    47                                                                          pos(0), 
    48                                                                          blockOffset(true), 
    49                                                                          append(false), 
    50                                                                          handler(NULL), 
    51                                                                          channel(protection) 
     34namespace dodo 
    5235{ 
    53 #ifndef IO_WO_XEXEC 
    54         collectedData.setExecObject(XEXEC_OBJECT_IOFILEREGULAR); 
    55 #endif 
    56 } 
    57  
    58 //------------------------------------------------------------------- 
    59  
    60 regular::regular(const dodoString &a_path, 
    61                                  short            a_mode, 
    62                                  short            protection) : overwrite(false), 
    63                                                                                                 pos(0), 
    64                                                                                                 blockOffset(true), 
    65                                                                                                 append(false), 
    66                                                                                                 handler(NULL), 
    67                                                                                                 path(a_path), 
    68                                                                                                 mode(a_mode), 
    69                                                                                                 channel(protection) 
    70 { 
    71 #ifndef IO_WO_XEXEC 
    72         collectedData.setExecObject(XEXEC_OBJECT_IOFILEREGULAR); 
    73 #endif 
    74  
    75         bool exists(false); 
    76  
    77         if (path.size() == 0) 
     36        namespace io 
    7837        { 
    79                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_LIBDODO, REGULAREX_WRONGFILENAME, IOFILEREGULAREX_WRONGFILENAME_STR, __LINE__, __FILE__, path); 
    80         } 
    81         else 
    82         { 
    83                 struct stat st; 
    84  
    85                 if (::lstat(path.c_str(), &st) == -1) 
     38                /** 
     39                 * @struct __file 
     40                 * @brief defines file handle 
     41                 */ 
     42                struct __file 
    8643                { 
    87                         if (errno != ENOENT) 
     44                        /** 
     45                         * constructor 
     46                         */ 
     47                        __file() : file(NULL) 
    8848                        { 
    89                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    90                         } 
    91                 } 
    92                 else 
    93                 { 
    94                         exists = true; 
    95                 } 
    96  
    97                 if (exists && !S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode)) 
    98                 { 
    99                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_LIBDODO, REGULAREX_WRONGFILENAME, IOFILEREGULAREX_WRONGFILENAME_STR, __LINE__, __FILE__, path); 
    100                 } 
    101  
    102                 switch (mode) 
    103                 { 
    104                         case REGULAR_OPENMODE_READ_WRITE: 
    105  
    106                                 handler = fopen(path.c_str(), "r+"); 
    107                                 if (handler == NULL) 
    108                                 { 
    109                                         handler = fopen(path.c_str(), "w+"); 
    110                                 } 
    111  
    112                                 break; 
    113  
    114                         case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    115  
    116                                 handler = fopen(path.c_str(), "w+"); 
    117  
    118                                 break; 
    119  
    120                         case REGULAR_OPENMODE_APPEND: 
    121  
    122                                 handler = fopen(path.c_str(), "a"); 
    123  
    124                                 break; 
    125  
    126                         case REGULAR_OPENMODE_READ_ONLY: 
    127                         default: 
    128  
    129                                 handler = fopen(path.c_str(), "r"); 
    130                 } 
    131         } 
    132  
    133         if (handler == NULL) 
    134         { 
    135                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    136         } 
    137  
    138         if (!exists) 
    139         { 
    140                 tools::filesystem::chmod(path, DEFAULT_FILE_PERM); 
    141         } 
    142 } 
    143  
    144 //------------------------------------------------------------------- 
    145  
    146 regular::regular(const regular &fd) : overwrite(fd.overwrite), 
    147                                                                           path(fd.path), 
    148                                                                           pos(fd.pos), 
    149                                                                           blockOffset(fd.blockOffset), 
    150                                                                           append(fd.append), 
    151                                                                           mode(fd.mode), 
    152                                                                           handler(NULL), 
    153                                                                           channel(fd.protection) 
    154  
    155 { 
    156 #ifndef IO_WO_XEXEC 
    157         collectedData.setExecObject(XEXEC_OBJECT_IOFILEREGULAR); 
    158 #endif 
    159  
    160         inSize = fd.inSize; 
    161         outSize = fd.outSize; 
    162  
    163         if (fd.handler !=  NULL) 
    164         { 
    165                 int oldDesc, newDesc; 
    166  
    167                 oldDesc = fileno((FILE *)fd.handler); 
    168                 if (oldDesc == -1) 
    169                 { 
    170                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    171                 } 
    172  
    173                 newDesc = dup(oldDesc); 
    174                 if (newDesc == -1) 
    175                 { 
    176                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    177                 } 
    178  
    179                 switch (mode) 
    180                 { 
    181                         case REGULAR_OPENMODE_READ_WRITE: 
    182                         case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    183  
    184                                 handler = fdopen(newDesc, "r+"); 
    185  
    186                                 break; 
    187  
    188                         case REGULAR_OPENMODE_APPEND: 
    189  
    190                                 handler = fdopen(newDesc, "a"); 
    191  
    192                                 break; 
    193  
    194                         case REGULAR_OPENMODE_READ_ONLY: 
    195                         default: 
    196  
    197                                 handler = fdopen(newDesc, "r"); 
    198                 } 
    199  
    200                 if (handler == NULL) 
    201                 { 
    202                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    203                 } 
    204         } 
    205 } 
    206  
    207 //------------------------------------------------------------------- 
    208  
    209 regular::~regular() 
    210 { 
    211         if (handler !=  NULL) 
    212         { 
    213                 fclose((FILE *)handler); 
    214         } 
    215 } 
    216  
    217 //------------------------------------------------------------------- 
    218  
    219 int 
    220 regular::getInDescriptor() const 
    221 { 
    222         pc::sync::protector pg(keeper); 
    223  
    224         if (handler == NULL) 
    225         { 
    226                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_GETINDESCRIPTOR, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    227         } 
    228  
    229         return fileno((FILE *)handler); 
    230 } 
    231  
    232 //------------------------------------------------------------------- 
    233  
    234 int 
    235 regular::getOutDescriptor() const 
    236 { 
    237         pc::sync::protector pg(keeper); 
    238  
    239         if (handler == NULL) 
    240         { 
    241                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_GETOUTDESCRIPTOR, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    242         } 
    243  
    244         return fileno((FILE *)handler); 
    245 } 
    246  
    247 //------------------------------------------------------------------- 
    248  
    249 void 
    250 regular::clone(const regular &fd) 
    251 { 
    252         pc::sync::protector pg(keeper); 
    253  
    254         if (handler !=  NULL) 
    255         { 
    256                 if (fclose((FILE *)handler) != 0) 
    257                 { 
    258                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    259                 } 
    260  
    261                 handler = NULL; 
    262         } 
    263  
    264         overwrite = fd.overwrite; 
    265         path = fd.path; 
    266         mode = fd.mode; 
    267         pos = fd.pos; 
    268         blockOffset = fd.blockOffset; 
    269         append = fd.append; 
    270         inSize = fd.inSize; 
    271         outSize = fd.outSize; 
    272  
    273         if (fd.handler !=  NULL) 
    274         { 
    275                 int oldDesc, newDesc; 
    276  
    277                 oldDesc = fileno((FILE *)fd.handler); 
    278                 if (oldDesc == -1) 
    279                 { 
    280                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    281                 } 
    282  
    283                 newDesc = dup(oldDesc); 
    284                 if (newDesc == -1) 
    285                 { 
    286                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    287                 } 
    288  
    289                 switch (mode) 
    290                 { 
    291                         case REGULAR_OPENMODE_READ_WRITE: 
    292                         case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    293  
    294                                 handler = fdopen(newDesc, "r+"); 
    295  
    296                                 break; 
    297  
    298                         case REGULAR_OPENMODE_APPEND: 
    299  
    300                                 handler = fdopen(newDesc, "a"); 
    301  
    302                                 break; 
    303  
    304                         case REGULAR_OPENMODE_READ_ONLY: 
    305                         default: 
    306  
    307                                 handler = fdopen(newDesc, "r"); 
    308                 } 
    309  
    310                 if (handler == NULL) 
    311                 { 
    312                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    313                 } 
    314         } 
    315 } 
    316  
    317 //------------------------------------------------------------------- 
    318  
    319 void 
    320 regular::close() 
    321 { 
    322         pc::sync::protector pg(keeper); 
    323  
    324 #ifndef IO_WO_XEXEC 
    325         operType = REGULAR_OPERATION_CLOSE; 
    326         performXExec(preExec); 
    327 #endif 
    328  
    329         if (handler !=  NULL) 
    330         { 
    331                 if (fclose((FILE *)handler) != 0) 
    332                 { 
    333                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLOSE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    334                 } 
    335  
    336                 handler = NULL; 
    337         } 
    338  
    339 #ifndef IO_WO_XEXEC 
    340         performXExec(postExec); 
    341 #endif 
    342 } 
    343  
    344 //------------------------------------------------------------------- 
    345  
    346 void 
    347 regular::open(const dodoString &a_path, 
    348                           short            a_mode) 
    349 { 
    350         pc::sync::protector pg(keeper); 
    351  
    352 #ifndef IO_WO_XEXEC 
    353         operType = REGULAR_OPERATION_OPEN; 
    354         performXExec(preExec); 
    355 #endif 
    356  
    357         path = a_path; 
    358         mode = a_mode; 
    359  
    360         if (handler !=  NULL) 
    361         { 
    362                 if (fclose((FILE *)handler) != 0) 
    363                 { 
    364                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    365                 } 
    366  
    367                 handler = NULL; 
    368         } 
    369  
    370         bool exists(false); 
    371  
    372         if (path.size() == 0) 
    373         { 
    374                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_LIBDODO, REGULAREX_WRONGFILENAME, IOFILEREGULAREX_WRONGFILENAME_STR, __LINE__, __FILE__, path); 
    375         } 
    376         else 
    377         { 
    378                 struct stat st; 
    379  
    380                 if (::lstat(path.c_str(), &st) == -1) 
    381                 { 
    382                         if (errno != ENOENT) 
    383                         { 
    384                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    385                         } 
    386                 } 
    387                 else 
    388                 { 
    389                         exists = true; 
    390                 } 
    391  
    392                 if (exists && !S_ISREG(st.st_mode) && !S_ISBLK(st.st_mode)) 
    393                 { 
    394                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_LIBDODO, REGULAREX_WRONGFILENAME, IOFILEREGULAREX_WRONGFILENAME_STR, __LINE__, __FILE__, path); 
    395                 } 
    396  
    397                 switch (mode) 
    398                 { 
    399                         case REGULAR_OPENMODE_READ_WRITE: 
    400  
    401                                 handler = fopen(path.c_str(), "r+"); 
    402                                 if (handler == NULL) 
    403                                 { 
    404                                         handler = fopen(path.c_str(), "w+"); 
    405                                 } 
    406  
    407                                 break; 
    408  
    409                         case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    410  
    411                                 handler = fopen(path.c_str(), "w+"); 
    412  
    413                                 break; 
    414  
    415                         case REGULAR_OPENMODE_APPEND: 
    416  
    417                                 handler = fopen(path.c_str(), "a"); 
    418  
    419                                 break; 
    420  
    421                         case REGULAR_OPENMODE_READ_ONLY: 
    422                         default: 
    423  
    424                                 handler = fopen(path.c_str(), "r"); 
    425                 } 
    426         } 
    427  
    428         if (handler == NULL) 
    429         { 
    430                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    431         } 
    432  
    433         if (!exists) 
    434         { 
    435                 tools::filesystem::chmod(path, DEFAULT_FILE_PERM); 
    436         } 
    437  
    438 #ifndef IO_WO_XEXEC 
    439         performXExec(postExec); 
    440 #endif 
    441 } 
    442  
    443 //------------------------------------------------------------------- 
    444  
    445 void 
    446 regular::_read(char * const a_data) 
    447 { 
    448         if (handler == NULL) 
    449         { 
    450                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READ, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    451         } 
    452  
    453         unsigned long pos = blockOffset ? this->pos * inSize : this->pos; 
    454  
    455         if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
    456         { 
    457                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    458         } 
    459  
    460         memset(a_data, '\0', inSize); 
    461  
    462         while (true) 
    463         { 
    464                 if (fread(a_data, inSize, 1, (FILE *)handler) == 0) 
    465                 { 
    466                         if (feof((FILE *)handler) != 0 || errno == EAGAIN) 
    467                         { 
    468                                 break; 
    46949                        } 
    47050 
    471                         if (errno == EINTR) 
    472                         { 
    473                                 continue; 
    474                         } 
    475  
    476                         if (ferror((FILE *)handler) != 0) 
    477                         { 
    478                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    479                         } 
    480                 } 
    481  
    482                 break; 
    483         } 
    484 } 
    485  
    486 //------------------------------------------------------------------- 
    487  
    488 void 
    489 regular::_write(const char *const a_data) 
    490 { 
    491         if (handler == NULL) 
    492         { 
    493                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    494         } 
    495  
    496         if (mode != REGULAR_OPENMODE_APPEND) 
    497         { 
    498                 if (append) 
    499                 { 
    500                         if (fseek((FILE *)handler, 0, SEEK_END) == -1) 
    501                         { 
    502                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    503                         } 
    504                 } 
    505                 else 
    506                 { 
    507                         unsigned long pos = blockOffset ? this->pos * outSize : this->pos; 
    508                         if (!overwrite) 
    509                         { 
    510                                 if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
    511                                 { 
    512                                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    513                                 } 
    514  
    515                                 char *t_buf = new char[outSize]; 
    516  
    517                                 size_t read = fread(t_buf, outSize, 1, (FILE *)handler); 
    518  
    519                                 delete [] t_buf; 
    520  
    521                                 if (read != 0) 
    522                                 { 
    523                                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_LIBDODO, REGULAREX_CANNOTOVEWRITE, IOFILEREGULAREX_CANNOTOVEWRITE_STR, __LINE__, __FILE__, path); 
    524                                 } 
    525                         } 
    526  
    527                         if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
    528                         { 
    529                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    530                         } 
    531                 } 
    532         } 
    533  
    534         while (true) 
    535         { 
    536                 if (fwrite(a_data, outSize, 1, (FILE *)handler) == 0) 
    537                 { 
    538                         if (errno == EINTR) 
    539                         { 
    540                                 continue; 
    541                         } 
    542  
    543                         if (errno == EAGAIN) 
    544                         { 
    545                                 break; 
    546                         } 
    547  
    548                         if (ferror((FILE *)handler) != 0) 
    549                         { 
    550                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    551                         } 
    552                 } 
    553  
    554                 break; 
    555         } 
    556 } 
    557  
    558 //------------------------------------------------------------------- 
    559  
    560 void 
    561 regular::erase() 
    562 { 
    563         pc::sync::protector pg(keeper); 
    564  
    565         char *empty = new char[outSize]; 
    566  
    567         memset(empty, 0, outSize); 
    568  
    569         bool _overwrite = overwrite; 
    570         overwrite = true; 
    571  
    572         try 
    573         { 
    574                 _write(empty); 
    575  
    576                 overwrite = _overwrite; 
    577         } 
    578         catch (...) 
    579         { 
    580                 overwrite = _overwrite; 
    581  
    582                 delete [] empty; 
    583  
    584                 throw; 
    585         } 
    586  
    587         delete [] empty; 
    588 } 
    589  
    590 //------------------------------------------------------------------- 
    591  
    592 void 
    593 regular::flush() 
    594 { 
    595         pc::sync::protector pg(keeper); 
    596  
    597         if (handler == NULL) 
    598         { 
    599                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_FLUSH, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    600         } 
    601  
    602         if (fflush((FILE *)handler) != 0) 
    603         { 
    604                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_FLUSH, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    605         } 
    606 } 
    607  
    608 //------------------------------------------------------------------- 
    609  
    610 unsigned long 
    611 regular::_readStream(char * const a_data) 
    612 { 
    613         if (handler == NULL) 
    614         { 
    615                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    616         } 
    617  
    618         unsigned long readSize = inSize + 1; 
    619  
    620         if (blockOffset) 
    621         { 
    622                 if (fseek((FILE *)handler, 0, SEEK_SET) == -1) 
    623                 { 
    624                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    625                 } 
    626  
    627                 for (unsigned long i = 0; i < pos; ++i) 
    628                 { 
    629                         if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
    630                         { 
    631                                 switch (errno) 
    632                                 { 
    633                                         case EIO: 
    634                                         case EINTR: 
    635                                         case EBADF: 
    636                                         case EOVERFLOW: 
    637                                         case ENOMEM: 
    638                                         case ENXIO: 
    639  
    640                                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    641                                 } 
    642  
    643                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_LIBDODO, REGULAREX_FILEISSHORTERTHANGIVENPOSITION, IOFILEREGULAREX_FILEISSHORTERTHANGIVENPOSITION_STR, __LINE__, __FILE__, path); 
    644                         } 
    645                 } 
    646         } 
    647         else if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
    648         { 
    649                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    650         } 
    651  
    652         memset(a_data, '\0', readSize); 
    653  
    654         while (true) 
    655         { 
    656                 if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
    657                 { 
    658                         if (errno == EINTR) 
    659                         { 
    660                                 continue; 
    661                         } 
    662  
    663                         if (errno == EAGAIN) 
    664                         { 
    665                                 break; 
    666                         } 
    667  
    668                         if (ferror((FILE *)handler) != 0) 
    669                         { 
    670                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    671                         } 
    672                 } 
    673  
    674                 break; 
    675         } 
    676  
    677         return strlen(a_data); 
    678 } 
    679  
    680 //------------------------------------------------------------------- 
    681  
    682 void 
    683 regular::_writeStream(const char *const a_data) 
    684 { 
    685         if (handler == NULL) 
    686         { 
    687                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITESTREAM, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    688         } 
    689  
    690         if (mode != REGULAR_OPENMODE_APPEND) 
    691         { 
    692                 if (fseek((FILE *)handler, 0, SEEK_END) == -1) 
    693                 { 
    694                         throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    695                 } 
    696         } 
    697  
    698         unsigned int bufSize = strlen(a_data); 
    699  
    700         unsigned long _outSize = outSize; 
    701  
    702         if (bufSize < _outSize) 
    703         { 
    704                 _outSize = bufSize; 
    705         } 
    706  
    707         while (true) 
    708         { 
    709                 if (fwrite(a_data, _outSize, 1, (FILE *)handler) == 0) 
    710                 { 
    711                         if (errno == EINTR) 
    712                         { 
    713                                 continue; 
    714                         } 
    715  
    716                         if (ferror((FILE *)handler) != 0) 
    717                         { 
    718                                 throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    719                         } 
    720                 } 
    721  
    722                 break; 
    723         } 
    724 } 
    725  
    726 //------------------------------------------------------------------- 
    727  
     51                        FILE *file; 
     52                }; 
     53        }; 
     54}; 
  • trunk/src/ioFileFifo.cc

    r1183 r1190  
    3434#include <sys/stat.h> 
    3535 
     36#include "ioFile.inline" 
     37 
    3638#include <libdodo/ioFileFifo.h> 
    3739#include <libdodo/toolsFilesystem.h> 
     
    4648                                                           outFileFifoBuffer(IOPIPE_OUTSIZE), 
    4749                                                           blocked(true), 
    48                                                            handler(NULL), 
     50                                                           handle(new io::__file), 
    4951                                                           channel(protection) 
    5052{ 
     
    6163                                                                                  outFileFifoBuffer(IOPIPE_OUTSIZE), 
    6264                                                                                  blocked(true), 
    63                                                                                   handler(NULL), 
     65                                                                                  handle(new io::__file), 
    6466                                                                                  path(path), 
    6567                                                                                  mode(mode), 
     
    101103                        case FIFO_OPENMODE_WRITE: 
    102104 
    103                                 handler = fopen(path.c_str(), "w"); 
     105                                handle->file = fopen(path.c_str(), "w"); 
    104106 
    105107                                break; 
     
    126128                                } 
    127129 
    128                                 handler = fdopen(fd, "r"); 
     130                                handle->file = fdopen(fd, "r"); 
    129131 
    130132                                break; 
     
    134136                        default: 
    135137 
    136                                 handler = fopen(path.c_str(), "r"); 
    137                 } 
    138         } 
    139  
    140         if (handler == NULL) 
     138                                handle->file = fopen(path.c_str(), "r"); 
     139                } 
     140        } 
     141 
     142        if (handle->file == NULL) 
    141143        { 
    142144                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_FIFO, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    151153                                                         mode(fd.mode), 
    152154                                                         path(fd.path), 
    153                                                          handler(NULL), 
     155                                                         handle(new __file), 
    154156                                                         channel(fd.protection) 
    155157{ 
     
    161163        outSize = fd.outSize; 
    162164 
    163         if (fd.handler != NULL) 
     165        if (fd.handle->file != NULL) 
    164166        { 
    165167                int oldDesc, newDesc; 
    166168 
    167                 oldDesc = fileno((FILE *)fd.handler); 
     169                oldDesc = fileno(fd.handle->file); 
    168170                if (oldDesc == -1) 
    169171                { 
     
    181183                        case FIFO_OPENMODE_WRITE: 
    182184 
    183                                 handler = fdopen(newDesc, "w"); 
     185                                handle->file = fdopen(newDesc, "w"); 
    184186 
    185187                                break; 
     
    188190                        default: 
    189191 
    190                                 handler = fdopen(newDesc, "r"); 
    191                 } 
    192  
    193                 if (handler == NULL) 
     192                                handle->file = fdopen(newDesc, "r"); 
     193                } 
     194 
     195                if (handle->file == NULL) 
    194196                { 
    195197                        throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_FIFO, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    202204fifo::~fifo() 
    203205{ 
    204         if (handler != NULL) 
    205         { 
    206                 fclose((FILE *)handler); 
    207         } 
     206        if (handle->file != NULL) 
     207        { 
     208                fclose(handle->file); 
     209        } 
     210 
     211        delete handle; 
    208212} 
    209213 
     
    215219        pc::sync::protector pg(keeper); 
    216220 
    217         if (handler == NULL) 
     221        if (handle->file == NULL) 
    218222        { 
    219223                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_GETINDESCRIPTOR, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    220224        } 
    221225 
    222         return fileno((FILE *)handler); 
     226        return fileno(handle->file); 
    223227} 
    224228 
     
    230234        pc::sync::protector pg(keeper); 
    231235 
    232         if (handler == NULL) 
     236        if (handle->file == NULL) 
    233237        { 
    234238                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_GETOUTDESCRIPTOR, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    235239        } 
    236240 
    237         return fileno((FILE *)handler); 
     241        return fileno(handle->file); 
    238242} 
    239243 
     
    245249        pc::sync::protector pg(keeper); 
    246250 
    247         if (handler != NULL) 
    248         { 
    249                 if (fclose((FILE *)handler) != 0) 
     251        if (handle->file != NULL) 
     252        { 
     253                if (fclose(handle->file) != 0) 
    250254                { 
    251255                        throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    252256                } 
    253257 
    254                 handler = NULL; 
     258                handle->file = NULL; 
    255259        } 
    256260 
     
    263267        outSize = fd.outSize; 
    264268 
    265         if (fd.handler != NULL) 
     269        if (fd.handle->file != NULL) 
    266270        { 
    267271                int oldDesc, newDesc; 
    268272 
    269                 oldDesc = fileno((FILE *)fd.handler); 
     273                oldDesc = fileno(fd.handle->file); 
    270274                if (oldDesc == -1) 
    271275                { 
     
    283287                        case FIFO_OPENMODE_WRITE: 
    284288 
    285                                 handler = fdopen(newDesc, "w"); 
     289                                handle->file = fdopen(newDesc, "w"); 
    286290 
    287291                                break; 
     
    290294                        default: 
    291295 
    292                                 handler = fdopen(newDesc, "r"); 
    293                 } 
    294  
    295                 if (handler == NULL) 
     296                                handle->file = fdopen(newDesc, "r"); 
     297                } 
     298 
     299                if (handle->file == NULL) 
    296300                { 
    297301                        throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    312316#endif 
    313317 
    314         if (handler != NULL) 
    315         { 
    316                 if (fclose((FILE *)handler) != 0) 
     318        if (handle->file != NULL) 
     319        { 
     320                if (fclose(handle->file) != 0) 
    317321                { 
    318322                        throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_CLOSE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    319323                } 
    320324 
    321                 handler = NULL; 
     325                handle->file = NULL; 
    322326        } 
    323327 
     
    343347        mode = a_mode; 
    344348 
    345         if (handler != NULL) 
    346         { 
    347                 if (fclose((FILE *)handler) != 0) 
     349        if (handle->file != NULL) 
     350        { 
     351                if (fclose(handle->file) != 0) 
    348352                { 
    349353                        throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    350354                } 
    351355 
    352                 handler = NULL; 
     356                handle->file = NULL; 
    353357        } 
    354358 
     
    388392                        case FIFO_OPENMODE_WRITE: 
    389393 
    390                                 handler = fopen(path.c_str(), "w"); 
     394                                handle->file = fopen(path.c_str(), "w"); 
    391395 
    392396                                break; 
     
    413417                                } 
    414418 
    415                                 handler = fdopen(fd, "r"); 
     419                                handle->file = fdopen(fd, "r"); 
    416420 
    417421                                break; 
     
    421425                        default: 
    422426 
    423                                 handler = fopen(path.c_str(), "r"); 
    424                 } 
    425         } 
    426  
    427         if (handler == NULL) 
     427                                handle->file = fopen(path.c_str(), "r"); 
     428                } 
     429        } 
     430 
     431        if (handle->file == NULL) 
    428432        { 
    429433                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    452456        pc::sync::protector pg(keeper); 
    453457 
    454         if (handler == NULL) 
     458        if (handle->file == NULL) 
    455459        { 
    456460                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_BLOCK, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__); 
     
    464468        int blockFlag; 
    465469 
    466         int desc = fileno((FILE *)handler); 
     470        int desc = fileno(handle->file); 
    467471        if (desc == -1) 
    468472        { 
     
    498502fifo::_read(char * const a_data) 
    499503{ 
    500         if (handler == NULL) 
     504        if (handle->file == NULL) 
    501505        { 
    502506                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__READ, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    519523                        while (true) 
    520524                        { 
    521                                 if ((n = fread(data, 1, batch, (FILE *)handler)) == 0) 
    522                                 { 
    523                                         if (feof((FILE *)handler) != 0 || errno == EAGAIN) 
     525                                if ((n = fread(data, 1, batch, handle->file)) == 0) 
     526                                { 
     527                                        if (feof(handle->file) != 0 || errno == EAGAIN) 
    524528                                        { 
    525529                                                break; 
     
    531535                                        } 
    532536 
    533                                         if (ferror((FILE *)handler) != 0) 
     537                                        if (ferror(handle->file) != 0) 
    534538                                        { 
    535539                                                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    552556                        while (true) 
    553557                        { 
    554                                 if ((n = fread(data, 1, batch, (FILE *)handler)) == 0) 
    555                                 { 
    556                                         if (feof((FILE *)handler) != 0 || errno == EAGAIN) 
     558                                if ((n = fread(data, 1, batch, handle->file)) == 0) 
     559                                { 
     560                                        if (feof(handle->file) != 0 || errno == EAGAIN) 
    557561                                        { 
    558562                                                break; 
     
    564568                                        } 
    565569 
    566                                         if (ferror((FILE *)handler) != 0) 
     570                                        if (ferror(handle->file) != 0) 
    567571                                        { 
    568572                                                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    584588fifo::_write(const char *const a_data) 
    585589{ 
    586         if (handler == NULL) 
     590        if (handle->file == NULL) 
    587591        { 
    588592                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__WRITE, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    603607                        while (true) 
    604608                        { 
    605                                 if ((n = fwrite(data, 1, batch, (FILE *)handler)) == 0) 
     609                                if ((n = fwrite(data, 1, batch, handle->file)) == 0) 
    606610                                { 
    607611                                        if (errno == EINTR) 
     
    615619                                        } 
    616620 
    617                                         if (ferror((FILE *)handler) != 0) 
     621                                        if (ferror(handle->file) != 0) 
    618622                                        { 
    619623                                                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    636640                        while (true) 
    637641                        { 
    638                                 if ((n = fwrite(data, 1, batch, (FILE *)handler)) == 0) 
     642                                if ((n = fwrite(data, 1, batch, handle->file)) == 0) 
    639643                                { 
    640644                                        if (errno == EINTR) 
     
    648652                                        } 
    649653 
    650                                         if (ferror((FILE *)handler) != 0) 
     654                                        if (ferror(handle->file) != 0) 
    651655                                        { 
    652656                                                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    670674        pc::sync::protector pg(keeper); 
    671675 
    672         if (handler == NULL) 
     676        if (handle->file == NULL) 
    673677        { 
    674678                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_FLUSH, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    675679        } 
    676680 
    677         if (fflush((FILE *)handler) != 0) 
     681        if (fflush(handle->file) != 0) 
    678682        { 
    679683                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX_FLUSH, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    686690fifo::_readStream(char * const a_data) 
    687691{ 
    688         if (handler == NULL) 
     692        if (handle->file == NULL) 
    689693        { 
    690694                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__READSTREAM, exception::ERRNO_LIBDODO, FIFOEX_NOTOPENED, IOFILEFIFOEX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    697701        while (true) 
    698702        { 
    699                 if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
     703                if (fgets(a_data, readSize, handle->file) == NULL) 
    700704                { 
    701705                        if (errno == EINTR) 
     
    709713                        } 
    710714 
    711                         if (ferror((FILE *)handler) != 0) 
     715                        if (ferror(handle->file) != 0) 
    712716                        { 
    713717                                throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    741745                while (true) 
    742746                { 
    743                         if (fputc('\n', (FILE *)handler) == EOF) 
     747                        if (fputc('\n', handle->file) == EOF) 
    744748                        { 
    745749                                if (errno == EINTR) 
     
    753757                                } 
    754758 
    755                                 if (ferror((FILE *)handler) != 0) 
     759                                if (ferror(handle->file) != 0) 
    756760                                { 
    757761                                        throw exception::basic(exception::ERRMODULE_IOFILEFIFO, FIFOEX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
  • trunk/src/ioFileRegular.cc

    r1183 r1190  
    3535#include <sys/stat.h> 
    3636 
     37#include "ioFile.inline" 
     38 
    3739#include <libdodo/ioFileRegular.h> 
    3840#include <libdodo/toolsFilesystem.h> 
     
    4850                                                                         blockOffset(true), 
    4951                                                                         append(false), 
    50                                                                          handler(NULL), 
     52                                                                         handle(new io::__file), 
    5153                                                                         channel(protection) 
    5254{ 
     
    6466                                                                                                blockOffset(true), 
    6567                                                                                                append(false), 
    66                                                                                                 handler(NULL), 
     68                                                                                                handle(new io::__file), 
    6769                                                                                                path(a_path), 
    6870                                                                                                mode(a_mode), 
     
    104106                        case REGULAR_OPENMODE_READ_WRITE: 
    105107 
    106                                 handler = fopen(path.c_str(), "r+"); 
    107                                 if (handler == NULL) 
     108                                handle->file = fopen(path.c_str(), "r+"); 
     109                                if (handle->file == NULL) 
    108110                                { 
    109                                         handler = fopen(path.c_str(), "w+"); 
     111                                        handle->file = fopen(path.c_str(), "w+"); 
    110112                                } 
    111113 
     
    114116                        case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    115117 
    116                                 handler = fopen(path.c_str(), "w+"); 
     118                                handle->file = fopen(path.c_str(), "w+"); 
    117119 
    118120                                break; 
     
    120122                        case REGULAR_OPENMODE_APPEND: 
    121123 
    122                                 handler = fopen(path.c_str(), "a"); 
     124                                handle->file = fopen(path.c_str(), "a"); 
    123125 
    124126                                break; 
     
    127129                        default: 
    128130 
    129                                 handler = fopen(path.c_str(), "r"); 
    130                 } 
    131         } 
    132  
    133         if (handler == NULL) 
     131                                handle->file = fopen(path.c_str(), "r"); 
     132                } 
     133        } 
     134 
     135        if (handle->file == NULL) 
    134136        { 
    135137                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    150152                                                                          append(fd.append), 
    151153                                                                          mode(fd.mode), 
    152                                                                           handler(NULL), 
     154                                                                          handle(new io::__file), 
    153155                                                                          channel(fd.protection) 
    154156 
     
    161163        outSize = fd.outSize; 
    162164 
    163         if (fd.handler !=  NULL) 
     165        if (fd.handle->file !=  NULL) 
    164166        { 
    165167                int oldDesc, newDesc; 
    166168 
    167                 oldDesc = fileno((FILE *)fd.handler); 
     169                oldDesc = fileno(fd.handle->file); 
    168170                if (oldDesc == -1) 
    169171                { 
     
    182184                        case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    183185 
    184                                 handler = fdopen(newDesc, "r+"); 
     186                                handle->file = fdopen(newDesc, "r+"); 
    185187 
    186188                                break; 
     
    188190                        case REGULAR_OPENMODE_APPEND: 
    189191 
    190                                 handler = fdopen(newDesc, "a"); 
     192                                handle->file = fdopen(newDesc, "a"); 
    191193 
    192194                                break; 
     
    195197                        default: 
    196198 
    197                                 handler = fdopen(newDesc, "r"); 
    198                 } 
    199  
    200                 if (handler == NULL) 
     199                                handle->file = fdopen(newDesc, "r"); 
     200                } 
     201 
     202                if (handle->file == NULL) 
    201203                { 
    202204                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_REGULAR, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    209211regular::~regular() 
    210212{ 
    211         if (handler !=  NULL) 
    212         { 
    213                 fclose((FILE *)handler); 
    214         } 
     213        if (handle->file !=  NULL) 
     214        { 
     215                fclose(handle->file); 
     216        } 
     217 
     218        delete handle; 
    215219} 
    216220 
     
    222226        pc::sync::protector pg(keeper); 
    223227 
    224         if (handler == NULL) 
     228        if (handle->file == NULL) 
    225229        { 
    226230                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_GETINDESCRIPTOR, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    227231        } 
    228232 
    229         return fileno((FILE *)handler); 
     233        return fileno(handle->file); 
    230234} 
    231235 
     
    237241        pc::sync::protector pg(keeper); 
    238242 
    239         if (handler == NULL) 
     243        if (handle->file == NULL) 
    240244        { 
    241245                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_GETOUTDESCRIPTOR, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    242246        } 
    243247 
    244         return fileno((FILE *)handler); 
     248        return fileno(handle->file); 
    245249} 
    246250 
     
    252256        pc::sync::protector pg(keeper); 
    253257 
    254         if (handler !=  NULL) 
    255         { 
    256                 if (fclose((FILE *)handler) != 0) 
     258        if (handle->file !=  NULL) 
     259        { 
     260                if (fclose(handle->file) != 0) 
    257261                { 
    258262                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    259263                } 
    260264 
    261                 handler = NULL; 
     265                handle->file = NULL; 
    262266        } 
    263267 
     
    271275        outSize = fd.outSize; 
    272276 
    273         if (fd.handler !=  NULL) 
     277        if (fd.handle->file !=  NULL) 
    274278        { 
    275279                int oldDesc, newDesc; 
    276280 
    277                 oldDesc = fileno((FILE *)fd.handler); 
     281                oldDesc = fileno(fd.handle->file); 
    278282                if (oldDesc == -1) 
    279283                { 
     
    292296                        case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    293297 
    294                                 handler = fdopen(newDesc, "r+"); 
     298                                handle->file = fdopen(newDesc, "r+"); 
    295299 
    296300                                break; 
     
    298302                        case REGULAR_OPENMODE_APPEND: 
    299303 
    300                                 handler = fdopen(newDesc, "a"); 
     304                                handle->file = fdopen(newDesc, "a"); 
    301305 
    302306                                break; 
     
    305309                        default: 
    306310 
    307                                 handler = fdopen(newDesc, "r"); 
    308                 } 
    309  
    310                 if (handler == NULL) 
     311                                handle->file = fdopen(newDesc, "r"); 
     312                } 
     313 
     314                if (handle->file == NULL) 
    311315                { 
    312316                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    327331#endif 
    328332 
    329         if (handler !=  NULL) 
    330         { 
    331                 if (fclose((FILE *)handler) != 0) 
     333        if (handle->file !=  NULL) 
     334        { 
     335                if (fclose(handle->file) != 0) 
    332336                { 
    333337                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_CLOSE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    334338                } 
    335339 
    336                 handler = NULL; 
     340                handle->file = NULL; 
    337341        } 
    338342 
     
    358362        mode = a_mode; 
    359363 
    360         if (handler !=  NULL) 
    361         { 
    362                 if (fclose((FILE *)handler) != 0) 
     364        if (handle->file !=  NULL) 
     365        { 
     366                if (fclose(handle->file) != 0) 
    363367                { 
    364368                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
    365369                } 
    366370 
    367                 handler = NULL; 
     371                handle->file = NULL; 
    368372        } 
    369373 
     
    399403                        case REGULAR_OPENMODE_READ_WRITE: 
    400404 
    401                                 handler = fopen(path.c_str(), "r+"); 
    402                                 if (handler == NULL) 
     405                                handle->file = fopen(path.c_str(), "r+"); 
     406                                if (handle->file == NULL) 
    403407                                { 
    404                                         handler = fopen(path.c_str(), "w+"); 
     408                                        handle->file = fopen(path.c_str(), "w+"); 
    405409                                } 
    406410 
     
    409413                        case REGULAR_OPENMODE_READ_WRITE_TRUNCATE: 
    410414 
    411                                 handler = fopen(path.c_str(), "w+"); 
     415                                handle->file = fopen(path.c_str(), "w+"); 
    412416 
    413417                                break; 
     
    415419                        case REGULAR_OPENMODE_APPEND: 
    416420 
    417                                 handler = fopen(path.c_str(), "a"); 
     421                                handle->file = fopen(path.c_str(), "a"); 
    418422 
    419423                                break; 
     
    422426                        default: 
    423427 
    424                                 handler = fopen(path.c_str(), "r"); 
    425                 } 
    426         } 
    427  
    428         if (handler == NULL) 
     428                                handle->file = fopen(path.c_str(), "r"); 
     429                } 
     430        } 
     431 
     432        if (handle->file == NULL) 
    429433        { 
    430434                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    446450regular::_read(char * const a_data) 
    447451{ 
    448         if (handler == NULL) 
     452        if (handle->file == NULL) 
    449453        { 
    450454                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READ, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    453457        unsigned long pos = blockOffset ? this->pos * inSize : this->pos; 
    454458 
    455         if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     459        if (fseek(handle->file, pos, SEEK_SET) == -1) 
    456460        { 
    457461                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    462466        while (true) 
    463467        { 
    464                 if (fread(a_data, inSize, 1, (FILE *)handler) == 0) 
    465                 { 
    466                         if (feof((FILE *)handler) != 0 || errno == EAGAIN) 
     468                if (fread(a_data, inSize, 1, handle->file) == 0) 
     469                { 
     470                        if (feof(handle->file) != 0 || errno == EAGAIN) 
    467471                        { 
    468472                                break; 
     
    474478                        } 
    475479 
    476                         if (ferror((FILE *)handler) != 0) 
     480                        if (ferror(handle->file) != 0) 
    477481                        { 
    478482                                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    489493regular::_write(const char *const a_data) 
    490494{ 
    491         if (handler == NULL) 
     495        if (handle->file == NULL) 
    492496        { 
    493497                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    498502                if (append) 
    499503                { 
    500                         if (fseek((FILE *)handler, 0, SEEK_END) == -1) 
     504                        if (fseek(handle->file, 0, SEEK_END) == -1) 
    501505                        { 
    502506                                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    508512                        if (!overwrite) 
    509513                        { 
    510                                 if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     514                                if (fseek(handle->file, pos, SEEK_SET) == -1) 
    511515                                { 
    512516                                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    515519                                char *t_buf = new char[outSize]; 
    516520 
    517                                 size_t read = fread(t_buf, outSize, 1, (FILE *)handler); 
     521                                size_t read = fread(t_buf, outSize, 1, handle->file); 
    518522 
    519523                                delete [] t_buf; 
     
    525529                        } 
    526530 
    527                         if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     531                        if (fseek(handle->file, pos, SEEK_SET) == -1) 
    528532                        { 
    529533                                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    534538        while (true) 
    535539        { 
    536                 if (fwrite(a_data, outSize, 1, (FILE *)handler) == 0) 
     540                if (fwrite(a_data, outSize, 1, handle->file) == 0) 
    537541                { 
    538542                        if (errno == EINTR) 
     
    546550                        } 
    547551 
    548                         if (ferror((FILE *)handler) != 0) 
     552                        if (ferror(handle->file) != 0) 
    549553                        { 
    550554                                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    595599        pc::sync::protector pg(keeper); 
    596600 
    597         if (handler == NULL) 
     601        if (handle->file == NULL) 
    598602        { 
    599603                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_FLUSH, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
    600604        } 
    601605 
    602         if (fflush((FILE *)handler) != 0) 
     606        if (fflush(handle->file) != 0) 
    603607        { 
    604608                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX_FLUSH, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    611615regular::_readStream(char * const a_data) 
    612616{ 
    613         if (handler == NULL) 
     617        if (handle->file == NULL) 
    614618        { 
    615619                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    620624        if (blockOffset) 
    621625        { 
    622                 if (fseek((FILE *)handler, 0, SEEK_SET) == -1) 
     626                if (fseek(handle->file, 0, SEEK_SET) == -1) 
    623627                { 
    624628                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    627631                for (unsigned long i = 0; i < pos; ++i) 
    628632                { 
    629                         if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
     633                        if (fgets(a_data, readSize, handle->file) == NULL) 
    630634                        { 
    631635                                switch (errno) 
     
    645649                } 
    646650        } 
    647         else if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     651        else if (fseek(handle->file, pos, SEEK_SET) == -1) 
    648652        { 
    649653                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    654658        while (true) 
    655659        { 
    656                 if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
     660                if (fgets(a_data, readSize, handle->file) == NULL) 
    657661                { 
    658662                        if (errno == EINTR) 
     
    666670                        } 
    667671 
    668                         if (ferror((FILE *)handler) != 0) 
     672                        if (ferror(handle->file) != 0) 
    669673                        { 
    670674                                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    683687regular::_writeStream(const char *const a_data) 
    684688{ 
    685         if (handler == NULL) 
     689        if (handle->file == NULL) 
    686690        { 
    687691                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITESTREAM, exception::ERRNO_LIBDODO, REGULAREX_NOTOPENED, IOFILEREGULAREX_NOTOPENED_STR, __LINE__, __FILE__, path); 
     
    690694        if (mode != REGULAR_OPENMODE_APPEND) 
    691695        { 
    692                 if (fseek((FILE *)handler, 0, SEEK_END) == -1) 
     696                if (fseek(handle->file, 0, SEEK_END) == -1) 
    693697                { 
    694698                        throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__, path); 
     
    707711        while (true) 
    708712        { 
    709                 if (fwrite(a_data, _outSize, 1, (FILE *)handler) == 0) 
     713                if (fwrite(a_data, _outSize, 1, handle->file) == 0) 
    710714                { 
    711715                        if (errno == EINTR) 
     
    714718                        } 
    715719 
    716                         if (ferror((FILE *)handler) != 0) 
     720                        if (ferror(handle->file) != 0) 
    717721                        { 
    718722                                throw exception::basic(exception::ERRMODULE_IOFILEREGULAR, REGULAREX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
  • trunk/src/ioFileTemp.cc

    r1183 r1190  
    3434#include <sys/stat.h> 
    3535 
     36#include "ioFile.inline" 
     37 
    3638#include <libdodo/ioFileTemp.h> 
    3739#include <libdodo/ioFileTempEx.h> 
     
    4749                                                           blockOffset(true), 
    4850                                                           append(false), 
    49                                                            handler(NULL), 
     51                                                           handle(new io::__file), 
    5052                                                           channel(protection) 
    5153{ 
     
    5658        if (open) 
    5759        { 
    58                 handler = tmpfile(); 
    59                 if (handler == NULL) 
     60                handle->file = tmpfile(); 
     61                if (handle->file == NULL) 
    6062                { 
    6163                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_TEMP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    7072                                                         blockOffset(fd.blockOffset), 
    7173                                                         append(fd.append), 
    72                                                          handler(NULL), 
     74                                                         handle(new io::__file), 
    7375                                                         channel(protection) 
    7476 
     
    8183        outSize = fd.outSize; 
    8284 
    83         if (fd.handler != NULL) 
     85        if (fd.handle->file != NULL) 
    8486        { 
    8587                int oldDesc, newDesc; 
    8688 
    87                 oldDesc = fileno((FILE *)fd.handler); 
     89                oldDesc = fileno(fd.handle->file); 
    8890                if (oldDesc == -1) 
    8991                { 
     
    9799                } 
    98100 
    99                 handler = fdopen(newDesc, "r+"); 
    100  
    101                 if (handler == NULL) 
     101                handle->file = fdopen(newDesc, "r+"); 
     102 
     103                if (handle->file == NULL) 
    102104                { 
    103105                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_TEMP, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    110112temp::~temp() 
    111113{ 
    112         if (handler != NULL) 
    113         { 
    114                 fclose((FILE *)handler); 
    115         } 
     114        if (handle->file != NULL) 
     115        { 
     116                fclose(handle->file); 
     117        } 
     118 
     119        delete handle; 
    116120} 
    117121 
     
    123127        pc::sync::protector pg(keeper); 
    124128 
    125         if (handler == NULL) 
     129        if (handle->file == NULL) 
    126130        { 
    127131                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_GETINDESCRIPTOR, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
    128132        } 
    129133 
    130         return fileno((FILE *)handler); 
     134        return fileno(handle->file); 
    131135} 
    132136 
     
    138142        pc::sync::protector pg(keeper); 
    139143 
    140         if (handler == NULL) 
     144        if (handle->file == NULL) 
    141145        { 
    142146                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_GETOUTDESCRIPTOR, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
    143147        } 
    144148 
    145         return fileno((FILE *)handler); 
     149        return fileno(handle->file); 
    146150} 
    147151 
     
    153157        pc::sync::protector pg(keeper); 
    154158 
    155         if (handler != NULL) 
    156         { 
    157                 if (fclose((FILE *)handler) != 0) 
     159        if (handle->file != NULL) 
     160        { 
     161                if (fclose(handle->file) != 0) 
    158162                { 
    159163                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    160164                } 
    161165 
    162                 handler = NULL; 
     166                handle->file = NULL; 
    163167        } 
    164168 
     
    170174        outSize = fd.outSize; 
    171175 
    172         if (fd.handler != NULL) 
     176        if (fd.handle->file != NULL) 
    173177        { 
    174178                int oldDesc, newDesc; 
    175179 
    176                 oldDesc = fileno((FILE *)fd.handler); 
     180                oldDesc = fileno(fd.handle->file); 
    177181                if (oldDesc == -1) 
    178182                { 
     
    186190                } 
    187191 
    188                 handler = fdopen(newDesc, "r+"); 
    189  
    190                 if (handler == NULL) 
     192                handle->file = fdopen(newDesc, "r+"); 
     193 
     194                if (handle->file == NULL) 
    191195                { 
    192196                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    207211#endif 
    208212 
    209         if (handler != NULL) 
    210         { 
    211                 if (fclose((FILE *)handler) != 0) 
     213        if (handle->file != NULL) 
     214        { 
     215                if (fclose(handle->file) != 0) 
    212216                { 
    213217                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_CLOSE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    214218                } 
    215219 
    216                 handler = NULL; 
     220                handle->file = NULL; 
    217221        } 
    218222 
     
    234238#endif 
    235239 
    236         if (handler != NULL) 
    237         { 
    238                 if (fclose((FILE *)handler) != 0) 
     240        if (handle->file != NULL) 
     241        { 
     242                if (fclose(handle->file) != 0) 
    239243                { 
    240244                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    241245                } 
    242246 
    243                 handler = NULL; 
    244         } 
    245  
    246         handler = tmpfile(); 
    247         if (handler == NULL) 
     247                handle->file = NULL; 
     248        } 
     249 
     250        handle->file = tmpfile(); 
     251        if (handle->file == NULL) 
    248252        { 
    249253                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    260264temp::_read(char * const a_data) 
    261265{ 
    262         if (handler == NULL) 
     266        if (handle->file == NULL) 
    263267        { 
    264268                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READ, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
     
    267271        unsigned long pos = blockOffset ? this->pos * inSize : this->pos; 
    268272 
    269         if (fseek((FILE *)handler, pos * inSize, SEEK_SET) == -1) 
     273        if (fseek(handle->file, pos * inSize, SEEK_SET) == -1) 
    270274        { 
    271275                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    276280        while (true) 
    277281        { 
    278                 if (fread(a_data, inSize, 1, (FILE *)handler) == 0) 
    279                 { 
    280                         if (feof((FILE *)handler) != 0 || errno == EAGAIN) 
     282                if (fread(a_data, inSize, 1, handle->file) == 0) 
     283                { 
     284                        if (feof(handle->file) != 0 || errno == EAGAIN) 
    281285                        { 
    282286                                break; 
     
    288292                        } 
    289293 
    290                         if (ferror((FILE *)handler) != 0) 
     294                        if (ferror(handle->file) != 0) 
    291295                        { 
    292296                                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    303307temp::_write(const char *const a_data) 
    304308{ 
    305         if (handler == NULL) 
     309        if (handle->file == NULL) 
    306310        { 
    307311                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITE, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
     
    310314        if (append) 
    311315        { 
    312                 if (fseek((FILE *)handler, 0, SEEK_END) == -1) 
     316                if (fseek(handle->file, 0, SEEK_END) == -1) 
    313317                { 
    314318                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    320324                if (!overwrite) 
    321325                { 
    322                         if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     326                        if (fseek(handle->file, pos, SEEK_SET) == -1) 
    323327                        { 
    324328                                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    327331                        char *t_buf = new char[outSize]; 
    328332 
    329                         size_t read = fread(t_buf, outSize, 1, (FILE *)handler); 
     333                        size_t read = fread(t_buf, outSize, 1, handle->file); 
    330334 
    331335                        delete [] t_buf; 
     
    337341                } 
    338342 
    339                 if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     343                if (fseek(handle->file, pos, SEEK_SET) == -1) 
    340344                { 
    341345                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    345349        while (true) 
    346350        { 
    347                 if (fwrite(a_data, outSize, 1, (FILE *)handler) == 0) 
     351                if (fwrite(a_data, outSize, 1, handle->file) == 0) 
    348352                { 
    349353                        if (errno == EINTR) 
     
    357361                        } 
    358362 
    359                         if (ferror((FILE *)handler) != 0) 
     363                        if (ferror(handle->file) != 0) 
    360364                        { 
    361365                                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    406410        pc::sync::protector pg(keeper); 
    407411 
    408         if (handler == NULL) 
     412        if (handle->file == NULL) 
    409413        { 
    410414                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_FLUSH, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
    411415        } 
    412416 
    413         if (fflush((FILE *)handler) != 0) 
     417        if (fflush(handle->file) != 0) 
    414418        { 
    415419                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX_FLUSH, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    422426temp::_readStream(char * const a_data) 
    423427{ 
    424         if (handler == NULL) 
     428        if (handle->file == NULL) 
    425429        { 
    426430                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READSTREAM, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
     
    431435        if (blockOffset) 
    432436        { 
    433                 if (fseek((FILE *)handler, 0, SEEK_SET) == -1) 
     437                if (fseek(handle->file, 0, SEEK_SET) == -1) 
    434438                { 
    435439                        throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    438442                for (unsigned long i = 0; i < pos; ++i) 
    439443                { 
    440                         if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
     444                        if (fgets(a_data, readSize, handle->file) == NULL) 
    441445                        { 
    442446                                switch (errno) 
     
    456460                } 
    457461        } 
    458         else if (fseek((FILE *)handler, pos, SEEK_SET) == -1) 
     462        else if (fseek(handle->file, pos, SEEK_SET) == -1) 
    459463        { 
    460464                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    465469        while (true) 
    466470        { 
    467                 if (fgets(a_data, readSize, (FILE *)handler) == NULL) 
     471                if (fgets(a_data, readSize, handle->file) == NULL) 
    468472                { 
    469473                        if (errno == EINTR) 
     
    477481                        } 
    478482 
    479                         if (ferror((FILE *)handler) != 0) 
     483                        if (ferror(handle->file) != 0) 
    480484                        { 
    481485                                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__READSTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    494498temp::_writeStream(const char *const a_data) 
    495499{ 
    496         if (handler == NULL) 
     500        if (handle->file == NULL) 
    497501        { 
    498502                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITESTREAM, exception::ERRNO_LIBDODO, TEMPEX_NOTOPENED, IOFILETEMPEX_NOTOPENED_STR, __LINE__, __FILE__); 
    499503        } 
    500504 
    501         if (fseek((FILE *)handler, 0, SEEK_END) == -1) 
     505        if (fseek(handle->file, 0, SEEK_END) == -1) 
    502506        { 
    503507                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    515519        while (true) 
    516520        { 
    517                 if (fwrite(a_data, _outSize, 1, (FILE *)handler) == 0) 
     521                if (fwrite(a_data, _outSize, 1, handle->file) == 0) 
    518522                { 
    519523                        if (errno == EINTR) 
     
    522526                        } 
    523527 
    524                         if (ferror((FILE *)handler) != 0) 
     528                        if (ferror(handle->file) != 0) 
    525529                        { 
    526530                                throw exception::basic(exception::ERRMODULE_IOFILETEMP, TEMPEX__WRITESTREAM, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
  • trunk/src/ioPipe.cc

    r1189 r1190  
    3434#include <fcntl.h> 
    3535 
     36#include "ioFile.inline" 
     37 
    3638#include <libdodo/ioPipe.h> 
    3739#include <libdodo/ioPipeEx.h> 
     
    4648                                                                   outPipeBuffer(IOPIPE_OUTSIZE), 
    4749                                                                   blocked(true), 
    48                                                                    in(NULL), 
    49                                                                    out(NULL), 
     50                                                                   in(new io::__file), 
     51                                                                   out(new io::__file), 
    5052                                                                   channel(protection) 
    5153{ 
     
    6365                } 
    6466 
    65                 in = fdopen(pipefd[0], "r"); 
    66                 if (in == NULL) 
    67                 { 
    68                         throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_PIPE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    69                 } 
    70  
    71                 out = fdopen(pipefd[1], "w"); 
    72                 if (out == NULL) 
     67                in->file = fdopen(pipefd[0], "r"); 
     68                if (in->file == NULL) 
     69                { 
     70                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_PIPE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     71                } 
     72 
     73                out->file = fdopen(pipefd[1], "w"); 
     74                if (out->file == NULL) 
    7375                { 
    7476                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_PIPE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    8284                                                                 outPipeBuffer(fd.outPipeBuffer), 
    8385                                                                 blocked(fd.blocked), 
    84                                                                  in(NULL), 
    85                                                                  out(NULL), 
     86                                                                 in(new io::__file), 
     87                                                                 out(new io::__file), 
    8688                                                                 channel(protection) 
    8789{ 
     
    9395        outSize = fd.outSize; 
    9496 
    95         if (fd.in != NULL && fd.out != NULL) 
     97        if (fd.in->file != NULL && fd.out->file != NULL) 
    9698        { 
    9799                int oldDesc, newDesc; 
    98100 
    99                 oldDesc = fileno((FILE *)fd.in); 
     101                oldDesc = fileno(fd.in->file); 
    100102                if (oldDesc == -1) 
    101103                { 
     
    109111                } 
    110112 
    111                 in = fdopen(newDesc, "r"); 
    112                 if (in == NULL) 
    113                 { 
    114                         throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_PIPE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    115                 } 
    116  
    117                 oldDesc = fileno((FILE *)fd.out); 
     113                in->file = fdopen(newDesc, "r"); 
     114                if (in->file == NULL) 
     115                { 
     116                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_PIPE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     117                } 
     118 
     119                oldDesc = fileno(fd.out->file); 
    118120                if (oldDesc == -1) 
    119121                { 
     
    127129                } 
    128130 
    129                 out = fdopen(newDesc, "w"); 
    130                 if (out == NULL) 
     131                out->file = fdopen(newDesc, "w"); 
     132                if (out->file == NULL) 
    131133                { 
    132134                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_PIPE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    139141io::pipe::~pipe() 
    140142{ 
    141         if (in != NULL) 
    142         { 
    143                 fclose((FILE *)in); 
    144         } 
    145  
    146         if (out != NULL) 
    147         { 
    148                 fclose((FILE *)out); 
    149         } 
     143        if (in->file != NULL) 
     144        { 
     145                fclose(in->file); 
     146        } 
     147 
     148        if (out->file != NULL) 
     149        { 
     150                fclose(out->file); 
     151        } 
     152 
     153        delete in; 
     154        delete out; 
    150155} 
    151156 
     
    157162        pc::sync::protector pg(keeper); 
    158163 
    159         if (in != NULL) 
    160         { 
    161                 if (fclose((FILE *)in) != 0) 
     164        if (in->file != NULL) 
     165        { 
     166                if (fclose(in->file) != 0) 
    162167                { 
    163168                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    164169                } 
    165170 
    166                 in = NULL; 
    167         } 
    168  
    169         if (out != NULL) 
    170         { 
    171                 if (fclose((FILE *)out) != 0) 
     171                in->file = NULL; 
     172        } 
     173 
     174        if (out->file != NULL) 
     175        { 
     176                if (fclose(out->file) != 0) 
    172177                { 
    173178                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    174179                } 
    175180 
    176                 out = NULL; 
     181                out->file = NULL; 
    177182        } 
    178183 
     
    183188        outSize = fd.outSize; 
    184189 
    185         if (fd.in != NULL && fd.out != NULL) 
     190        if (fd.in->file != NULL && fd.out->file != NULL) 
    186191        { 
    187192                int oldDesc, newDesc; 
    188193 
    189                 oldDesc = fileno((FILE *)fd.in); 
     194                oldDesc = fileno(fd.in->file); 
    190195                if (oldDesc == -1) 
    191196                { 
     
    199204                } 
    200205 
    201                 in = fdopen(newDesc, "r"); 
    202                 if (in == NULL) 
     206                in->file = fdopen(newDesc, "r"); 
     207                if (in->file == NULL) 
    203208                { 
    204209                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    205210                } 
    206211 
    207                 oldDesc = fileno((FILE *)fd.out); 
     212                oldDesc = fileno(fd.out->file); 
    208213                if (oldDesc == -1) 
    209214                { 
     
    217222                } 
    218223 
    219                 out = fdopen(newDesc, "w"); 
    220                 if (out == NULL) 
     224                out->file = fdopen(newDesc, "w"); 
     225                if (out->file == NULL) 
    221226                { 
    222227                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_CLONE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    232237        pc::sync::protector pg(keeper); 
    233238 
    234         if (in == NULL) 
     239        if (in->file == NULL) 
    235240        { 
    236241                throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_GETINDESCRIPTOR, exception::ERRNO_LIBDODO, PIPEEX_PIPENOTOPENED, IOPIPEEX_NOTOPENED_STR, __LINE__, __FILE__); 
    237242        } 
    238243 
    239         return fileno((FILE *)in); 
     244        return fileno(in->file); 
    240245} 
    241246 
     
    247252        pc::sync::protector pg(keeper); 
    248253 
    249         if (out == NULL) 
     254        if (out->file == NULL) 
    250255        { 
    251256                throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_GETOUTDESCRIPTOR, exception::ERRNO_LIBDODO, PIPEEX_PIPENOTOPENED, IOPIPEEX_NOTOPENED_STR, __LINE__, __FILE__); 
    252257        } 
    253258 
    254         return fileno((FILE *)out); 
     259        return fileno(out->file); 
    255260} 
    256261 
     
    267272#endif 
    268273 
    269         if (in != NULL) 
    270         { 
    271                 if (fclose((FILE *)in) != 0) 
     274        if (in->file != NULL) 
     275        { 
     276                if (fclose(in->file) != 0) 
    272277                { 
    273278                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_CLOSE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    274279                } 
    275280 
    276                 in = NULL; 
    277         } 
    278  
    279         if (out != NULL) 
    280         { 
    281                 if (fclose((FILE *)out) != 0) 
     281                in->file = NULL; 
     282        } 
     283 
     284        if (out->file != NULL) 
     285        { 
     286                if (fclose(out->file) != 0) 
    282287                { 
    283288                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_CLOSE, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    284289                } 
    285290 
    286                 out = NULL; 
     291                out->file = NULL; 
    287292        } 
    288293 
     
    304309#endif 
    305310 
    306         if (in != NULL) 
    307         { 
    308                 if (fclose((FILE *)in) != 0) 
     311        if (in->file != NULL) 
     312        { 
     313                if (fclose(in->file) != 0) 
    309314                { 
    310315                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    311316                } 
    312317 
    313                 in = NULL; 
    314         } 
    315  
    316         if (out != NULL) 
    317         { 
    318                 if (fclose((FILE *)out) != 0) 
     318                in->file = NULL; 
     319        } 
     320 
     321        if (out->file != NULL) 
     322        { 
     323                if (fclose(out->file) != 0) 
    319324                { 
    320325                        throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    321326                } 
    322327 
    323                 out = NULL; 
     328                out->file = NULL; 
    324329        } 
    325330 
     
    331336        } 
    332337 
    333         in = fdopen(pipefd[0], "r"); 
    334         if (in == NULL) 
     338        in->file = fdopen(pipefd[0], "r"); 
     339        if (in->file == NULL) 
    335340        { 
    336341                throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
    337342        } 
    338343 
    339         out = fdopen(pipefd[1], "w"); 
    340         if (out == NULL) 
     344        out->file = fdopen(pipefd[1], "w"); 
     345        if (out->file == NULL) 
    341346        { 
    342347                throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX_OPEN, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    353358io::pipe::_read(char * const a_data) 
    354359{ 
    355         if (in == NULL) 
     360        if (in->file == NULL) 
    356361        { 
    357362                throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX__READ, exception::ERRNO_LIBDODO, PIPEEX_PIPENOTOPENED, IOPIPEEX_NOTOPENED_STR, __LINE__, __FILE__); 
     
    374379                        while (true) 
    375380                        { 
    376                                 if ((n = fread(data, 1, batch, (FILE *)in)) == 0) 
     381                                if ((n = fread(data, 1, batch, in->file)) == 0) 
    377382                                { 
    378                                         if (feof((FILE *)in) != 0 || errno == EAGAIN) 
     383                                        if (feof(in->file) != 0 || errno == EAGAIN) 
    379384                                        { 
    380385                                                break; 
     
    386391                                        } 
    387392 
    388                                         if (ferror((FILE *)in) != 0) 
     393                                        if (ferror(in->file) != 0) 
    389394                                        { 
    390395                                                throw exception::basic(exception::ERRMODULE_IOPIPE, PIPEEX__READ, exception::ERRNO_ERRNO, errno, strerror(errno), __LINE__, __FILE__); 
     
    407412                        while (true) 
    408413                        { 
    409                                 if ((n = fread(data, 1, batch, (FILE *)in)) == 0) 
     414                                if ((n = fread(data, 1, batch, in->file)) == 0) 
    410415                                { 
    411                                         if (feof((FILE *)in) != 0 || errno == EAGAIN) 
     416                                        if (feof(in->file) != 0 || errno == EAGAIN) 
    412417                                    &nb