C:/Users/vincent/Data/Perso/dev/ocilib/ocilib/src/file.c

00001 /*
00002    +----------------------------------------------------------------------+   
00003    |                                                                      |
00004    |                     OCILIB - C Driver for Oracle                     |
00005    |                                                                      |
00006    |                      (C Wrapper for Oracle OCI)                      |
00007    |                                                                      |
00008    +----------------------------------------------------------------------+
00009    |                      Website : http://www.ocilib.net                 |
00010    +----------------------------------------------------------------------+
00011    |               Copyright (c) 2007-2009 Vincent ROGIER                 |
00012    +----------------------------------------------------------------------+
00013    | This library is free software; you can redistribute it and/or        |
00014    | modify it under the terms of the GNU Lesser General Public           |
00015    | License as published by the Free Software Foundation; either         |
00016    | version 2 of the License, or (at your option) any later version.     |
00017    |                                                                      |
00018    | This library is distributed in the hope that it will be useful,      |
00019    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
00020    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
00021    | Lesser General Public License for more details.                      |
00022    |                                                                      |
00023    | You should have received a copy of the GNU Lesser General Public     |
00024    | License along with this library; if not, write to the Free           |
00025    | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   |
00026    +----------------------------------------------------------------------+
00027    |          Author: Vincent ROGIER <vince.rogier@gmail.com>             |
00028    +----------------------------------------------------------------------+ 
00029 */
00030 
00031 /* ------------------------------------------------------------------------ *
00032  * $Id: file.c, v 3.4.0 2009-07-30 17:40 Vince $
00033  * ------------------------------------------------------------------------ */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ************************************************************************ *
00038  *                             PRIVATE FUNCTIONS
00039  * ************************************************************************ */
00040 
00041 /* ------------------------------------------------------------------------ *
00042  * OCI_FileInit
00043  * ------------------------------------------------------------------------ */
00044 
00045  OCI_File * OCI_FileInit(OCI_Connection *con, OCI_File **pfile, 
00046                          OCILobLocator *handle, ub4 type)
00047 {
00048     OCI_File *file = NULL;
00049     boolean res    = TRUE;
00050 
00051     OCI_CHECK(pfile == NULL, NULL);
00052 
00053     if (*pfile == NULL)
00054         *pfile = (OCI_File *) OCI_MemAlloc(OCI_IPC_FILE, sizeof(*file), 1, TRUE);
00055 
00056     if (*pfile != NULL)
00057     {
00058         file = *pfile;
00059     
00060         file->type   = type;
00061         file->con    = con;
00062         file->handle = handle;
00063         file->offset = 1;
00064 
00065         /* reset file info */
00066 
00067         if (file->dir != NULL)
00068             file->dir[0] = 0;
00069 
00070         if (file->name != NULL)
00071             file->name[0] = 0;
00072 
00073         if (file->handle == NULL)
00074         {
00075             /* allocate handle for non fetched file (local file object) */
00076 
00077             file->hstate = OCI_OBJECT_ALLOCATED;
00078 
00079             res = (OCI_SUCCESS == OCI_DescriptorAlloc((dvoid *) OCILib.env, 
00080                                                      (dvoid **) (void *) &file->handle,
00081                                                      (ub4) OCI_DTYPE_LOB, 
00082                                                      (size_t) 0, (dvoid **) NULL));
00083         }
00084         else
00085             file->hstate = OCI_OBJECT_FETCHED_CLEAN;
00086     }
00087     else
00088         res = FALSE;
00089 
00090     /* check for failure */
00091 
00092     if (res == FALSE)
00093     {
00094         OCI_FileFree(file);
00095         file = NULL;
00096     }
00097 
00098     return file;
00099 }
00100 
00101 /* ------------------------------------------------------------------------ *
00102  * OCI_FileGetInfo
00103  * ------------------------------------------------------------------------ */
00104 
00105 boolean OCI_FileGetInfo(OCI_File *file)
00106 {
00107     boolean res  = TRUE;
00108     void *ostr1  = NULL;
00109     void *ostr2  = NULL;
00110     int  osize1  = 0;
00111     int  osize2  = 0;
00112     ub2  usize1  = 0;
00113     ub2  usize2  = 0;
00114 
00115     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00116 
00117     /* directory name */
00118 
00119     if (file->dir == NULL)
00120     {
00121         if (res == TRUE)
00122         {
00123             file->dir = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
00124                                                OCI_SIZE_DIRECTORY + 1, TRUE);
00125 
00126             res = (file->dir != NULL);
00127          }
00128     }
00129     else
00130         file->dir[0] = 0;
00131 
00132     /* file name */
00133 
00134     if (file->name == NULL)
00135     {
00136         if (res == TRUE)
00137         {
00138             file->name = (mtext *) OCI_MemAlloc(OCI_IPC_STRING, sizeof(mtext),
00139                                                 OCI_SIZE_FILENAME + 1, TRUE);
00140 
00141             res = (file->name != NULL);
00142         }
00143     }
00144     else
00145         file->name[0] = 0;
00146 
00147     /* retrieve name */
00148 
00149     if (res == TRUE)
00150     {
00151         osize1 = OCI_SIZE_DIRECTORY  * sizeof(mtext);
00152         ostr1  = OCI_GetInputMetaString(file->dir, &osize1);
00153 
00154         osize2 = OCI_SIZE_FILENAME  * sizeof(mtext);
00155         ostr2  = OCI_GetInputMetaString(file->name, &osize1);
00156      
00157         usize1 = (ub2) osize1;
00158         usize2 = (ub2) osize2;
00159 
00160         OCI_CALL2
00161         (
00162             res, file->con, 
00163             
00164             OCILobFileGetName(OCILib.env, file->con->err, file->handle,
00165                               (OraText *) ostr1, (ub2*) &usize1,
00166                               (OraText *) ostr2, (ub2*) &usize2)
00167         )
00168 
00169         osize1 = (int) usize1;
00170         osize2 = (int) usize2;
00171 
00172         OCI_GetOutputMetaString(ostr1, file->dir,  &osize1);
00173         OCI_GetOutputMetaString(ostr2, file->name, &osize2);
00174 
00175         OCI_ReleaseMetaString(ostr1);
00176         OCI_ReleaseMetaString(ostr2);
00177     }
00178 
00179     return res;
00180 }
00181 
00182 /* ************************************************************************ *
00183  *                            PUBLIC FUNCTIONS
00184  * ************************************************************************ */
00185 
00186 /* ------------------------------------------------------------------------ *
00187  * OCI_FileCreate
00188  * ------------------------------------------------------------------------ */
00189 
00190 OCI_File * OCI_API OCI_FileCreate(OCI_Connection *con, unsigned int type)
00191 {
00192     OCI_File *file = NULL;
00193 
00194     OCI_CHECK_INITIALIZED(NULL);
00195 
00196     OCI_CHECK_PTR(OCI_IPC_CONNECTION, con, NULL);
00197 
00198     file = OCI_FileInit(con, &file, NULL, type);
00199 
00200     OCI_RESULT(file != NULL);
00201 
00202     return file;
00203 }
00204 
00205 /* ------------------------------------------------------------------------ *
00206  * OCI_FileFree
00207  * ------------------------------------------------------------------------ */
00208 
00209 boolean OCI_API OCI_FileFree(OCI_File *file)
00210 {
00211     boolean res = TRUE;
00212 
00213     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00214     OCI_CHECK_OBJECT_FETCHED(file, FALSE);
00215 
00216     OCI_FREE(file->dir);
00217     OCI_FREE(file->name);
00218 
00219     if (file->hstate == OCI_OBJECT_ALLOCATED)
00220     {
00221         OCI_DescriptorFree((dvoid *) file->handle, (ub4) OCI_DTYPE_LOB);
00222     }
00223 
00224     OCI_FREE(file);
00225 
00226     OCI_RESULT(res);
00227 
00228     return res;
00229 }
00230 
00231 /* ------------------------------------------------------------------------ *
00232  * OCI_FileSeek
00233  * ------------------------------------------------------------------------ */
00234 
00235 boolean OCI_API OCI_FileSeek(OCI_File *file, big_uint offset, unsigned int mode)
00236 {
00237     boolean res   = TRUE;
00238     big_uint size = 0;
00239 
00240     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00241 
00242     size = OCI_FileGetSize(file);
00243 
00244     if ((mode == OCI_SEEK_CUR && (offset + file->offset-1) > size))
00245         res = FALSE;
00246     else if (mode == OCI_SEEK_SET)
00247         file->offset  = offset + 1;
00248     else if (mode == OCI_SEEK_END)
00249         file->offset  = size-offset + 1;
00250     else if (mode == OCI_SEEK_CUR)
00251         file->offset += offset;
00252     else
00253         res = FALSE;
00254 
00255     OCI_RESULT(res);
00256 
00257     return res;
00258 }
00259 
00260 /* ------------------------------------------------------------------------ *
00261  * OCI_FileGetOffset
00262  * ------------------------------------------------------------------------ */
00263 
00264 big_uint OCI_API OCI_FileGetOffset(OCI_File *file)
00265 {
00266     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00267 
00268     OCI_RESULT(TRUE);
00269 
00270     return file->offset - 1;
00271 }
00272 
00273 /* ------------------------------------------------------------------------ *
00274  * OCI_FileRead
00275  * ------------------------------------------------------------------------ */
00276 
00277 unsigned int OCI_API OCI_FileRead(OCI_File *file, void *buffer, unsigned int len)
00278 {
00279     boolean res  = TRUE;
00280     ub4 size_in  = 0;
00281     ub4 size_out = 0;
00282 
00283     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00284     OCI_CHECK_MIN(file->con, NULL, len, 1, 0);
00285 
00286     size_out = size_in = len;
00287 
00288 #ifdef OCI_LOB2_API_ENABLED
00289 
00290     if (OCILib.use_lob_ub8)
00291     {
00292         ub8 size_char = (ub8) len;
00293         ub8 size_byte = (ub8) size_in;
00294         
00295         OCI_CALL2
00296         (
00297             res, file->con, 
00298             
00299             OCILobRead2(file->con->cxt, file->con->err,
00300                         file->handle, &size_byte,
00301                         &size_char, (ub8) file->offset, 
00302                         buffer, (ub8) size_in, 
00303                         (ub1) OCI_ONE_PIECE, (dvoid *) NULL,
00304                         NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT)
00305         )
00306     }
00307 
00308     else
00309 
00310 #endif
00311 
00312     {
00313         ub4 offset = (ub4) file->offset;
00314 
00315         OCI_CALL2
00316         (
00317             res, file->con, 
00318             
00319             OCILobRead(file->con->cxt, file->con->err,
00320                        file->handle,  &size_out, offset, 
00321                        buffer, size_in, (dvoid *) NULL, 
00322                        NULL, (ub2) 0, (ub1) SQLCS_IMPLICIT)
00323         )
00324     }
00325 
00326     if (res == TRUE)
00327         file->offset += (big_uint) size_out;
00328 
00329     OCI_RESULT(res);
00330 
00331     return size_out;
00332 }
00333 
00334 /* ------------------------------------------------------------------------ *
00335  * OCI_FileGetType
00336  * ------------------------------------------------------------------------ */
00337 
00338 unsigned int OCI_API OCI_FileGetType(OCI_File *file)
00339 {
00340     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00341 
00342     OCI_RESULT(TRUE);
00343 
00344     return file->type;
00345 }
00346 
00347 /* ------------------------------------------------------------------------ *
00348  * OCI_FileGetSize
00349  * ------------------------------------------------------------------------ */
00350 
00351 big_uint OCI_API OCI_FileGetSize(OCI_File *file)
00352 {
00353     boolean res   = TRUE;
00354     big_uint size = 0;
00355 
00356     OCI_CHECK_PTR(OCI_IPC_FILE, file, 0);
00357  
00358 #ifdef OCI_LOB2_API_ENABLED
00359 
00360     if (OCILib.use_lob_ub8)
00361     {
00362         OCI_CALL2
00363         (
00364             res, file->con, 
00365             
00366             OCILobGetLength2(file->con->cxt, file->con->err,
00367                              file->handle, (ub8 *) &size)
00368         )
00369 
00370     }
00371     else
00372 
00373 #endif
00374  
00375     {
00376         ub4 size32 = (ub4) size;
00377    
00378         OCI_CALL2
00379         (
00380             res, file->con, 
00381             
00382             OCILobGetLength(file->con->cxt, file->con->err,
00383                             file->handle, &size32)
00384         )
00385 
00386         size = (big_uint) size32;
00387     }
00388 
00389     OCI_RESULT(res);
00390 
00391     return size;
00392 }
00393 
00394 /* ------------------------------------------------------------------------ *
00395  * OCI_LobFileExists
00396  * ------------------------------------------------------------------------ */
00397 
00398 boolean OCI_API OCI_FileExists(OCI_File *file)
00399 {
00400     boolean res   = TRUE;
00401     boolean value = FALSE;
00402 
00403     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00404 
00405     OCI_CALL2
00406     (
00407         res, file->con, 
00408         
00409         OCILobFileExists(file->con->cxt, file->con->err, file->handle, &value)
00410     )
00411 
00412     OCI_RESULT(res);
00413 
00414     return value;
00415 }
00416 
00417 /* ------------------------------------------------------------------------ *
00418  * OCI_FileSetName
00419  * ------------------------------------------------------------------------ */
00420 
00421 boolean OCI_API OCI_FileSetName(OCI_File *file, const mtext *dir, 
00422                                 const mtext *name)
00423 {
00424     void *ostr1 = NULL;
00425     void *ostr2 = NULL;
00426     int osize1  = -1;
00427     int osize2  = -1;
00428     boolean res  = TRUE;
00429 
00430     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00431 
00432     ostr1 = OCI_GetInputMetaString(dir,  &osize1);
00433     ostr2 = OCI_GetInputMetaString(name, &osize2);
00434 
00435     OCI_CALL2
00436     (
00437         res, file->con, 
00438         
00439         OCILobFileSetName(OCILib.env, file->con->err,
00440                                           &file->handle,
00441                                           (OraText *) ostr1, (ub2) osize1,
00442                                           (OraText *) ostr2, (ub2) osize2)
00443     )
00444 
00445     OCI_ReleaseMetaString(ostr1);
00446     OCI_ReleaseMetaString(ostr2);
00447  
00448     if (res == TRUE)
00449         res = OCI_FileGetInfo(file);
00450 
00451     OCI_RESULT(res);
00452 
00453     return res;
00454 }
00455 
00456 /* ------------------------------------------------------------------------ *
00457  * OCI_FileGetDirectory
00458  * ------------------------------------------------------------------------ */
00459 
00460 const mtext * OCI_API OCI_FileGetDirectory(OCI_File *file)
00461 {
00462     boolean res = TRUE;
00463 
00464     OCI_CHECK_PTR(OCI_IPC_FILE, file, NULL);
00465 
00466     if ((file->dir == NULL) || (file->dir[0] == 0))
00467         res = OCI_FileGetInfo(file);
00468 
00469 
00470     return file->dir;
00471 }
00472 
00473 /* ------------------------------------------------------------------------ *
00474  * OCI_FileGetName
00475  * ------------------------------------------------------------------------ */
00476 
00477 const mtext * OCI_API OCI_FileGetName(OCI_File *file)
00478 {
00479     boolean res = TRUE;
00480 
00481     OCI_CHECK_PTR(OCI_IPC_FILE, file, NULL);
00482 
00483     if ((file->name == NULL) || (file->name[0] == 0))
00484         res = OCI_FileGetInfo(file);
00485 
00486     OCI_RESULT(res);
00487 
00488     return file->name;
00489 }
00490 
00491 /* ------------------------------------------------------------------------ *
00492  * OCI_FileOpen
00493  * ------------------------------------------------------------------------ */
00494 
00495 boolean OCI_API OCI_FileOpen(OCI_File *file)
00496 {
00497     boolean res = TRUE;
00498 
00499     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00500 
00501     OCI_CALL2
00502     (
00503         res, file->con, 
00504         
00505         OCILobFileOpen(file->con->cxt, file->con->err,
00506                        file->handle, (ub1) OCI_LOB_READONLY)
00507     )
00508     
00509     if (res == TRUE)
00510         file->con->nb_files++;
00511 
00512     OCI_RESULT(res);
00513 
00514     return res;
00515 }
00516 
00517 /* ------------------------------------------------------------------------ *
00518  * OCI_LobFileIsOpen
00519  * ------------------------------------------------------------------------ */
00520 
00521 boolean OCI_API OCI_FileIsOpen(OCI_File *file)
00522 {
00523     boolean res   = TRUE;
00524     boolean value = FALSE;
00525 
00526     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00527 
00528     OCI_CALL2
00529     (
00530         res, file->con, 
00531         
00532         OCILobFileIsOpen(file->con->cxt, file->con->err, file->handle, &value)
00533     )
00534 
00535     OCI_RESULT(res);
00536 
00537     return value;
00538 }
00539 
00540 /* ------------------------------------------------------------------------ *
00541  * OCI_FileClose
00542  * ------------------------------------------------------------------------ */
00543 
00544 boolean OCI_API OCI_FileClose(OCI_File *file)
00545 {
00546     boolean res = TRUE;
00547 
00548     OCI_CHECK_PTR(OCI_IPC_FILE, file, FALSE);
00549 
00550     OCI_CALL2
00551     (
00552         res, file->con, 
00553         
00554         OCILobFileClose(file->con->cxt, file->con->err, file->handle)
00555     )
00556 
00557     if (res == TRUE)
00558         file->con->nb_files--;
00559 
00560     OCI_RESULT(res);
00561 
00562     return res;
00563 }
00564 
00565 /* ------------------------------------------------------------------------ *
00566  * OCI_FileIsEqual
00567  * ------------------------------------------------------------------------ */
00568 
00569 boolean OCI_API OCI_FileIsEqual(OCI_File *file, OCI_File *file2)
00570 {
00571     boolean res   = TRUE;
00572     boolean value = FALSE;
00573 
00574     OCI_CHECK_PTR(OCI_IPC_FILE, file,  FALSE);
00575     OCI_CHECK_PTR(OCI_IPC_FILE, file2, FALSE);
00576 
00577     OCI_CALL2
00578     (
00579         res, file->con, 
00580         
00581         OCILobIsEqual(OCILib.env, file->handle, file2->handle, &value)
00582     )
00583 
00584     OCI_RESULT(res);
00585 
00586     return value;
00587 }
00588 
00589 /* ------------------------------------------------------------------------ *
00590  * OCI_FileAssign
00591  * ------------------------------------------------------------------------ */
00592 
00593 boolean OCI_API OCI_FileAssign(OCI_File *file, OCI_File *file_src)
00594 {
00595     boolean res = TRUE;
00596 
00597     OCI_CHECK_PTR(OCI_IPC_FILE, file,     FALSE);
00598     OCI_CHECK_PTR(OCI_IPC_FILE, file_src, FALSE);
00599 
00600     if (file->hstate == OCI_OBJECT_ALLOCATED)
00601     {
00602         OCI_CALL2
00603         (
00604             res, file->con, 
00605             
00606             OCILobLocatorAssign(file->con->cxt, file->con->err,
00607                                 file_src->handle, &file->handle)
00608         )
00609   
00610     }
00611     else
00612     {
00613         OCI_CALL2
00614         (
00615             res, file->con, 
00616             
00617             OCILobAssign(OCILib.env, file->con->err,
00618                          file_src->handle, &file->handle)
00619         )
00620     }
00621 
00622     if (res == TRUE)
00623         OCI_FileGetInfo(file);
00624 
00625     OCI_RESULT(res);
00626 
00627     return res;
00628 }

Generated on Thu Jul 30 17:41:52 2009 for OCILIB (C Driver for Oracle) by  doxygen 1.5.4