• Main Page
  • Modules
  • Data Structures
  • Files
  • File List

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

Generated on Sun Oct 24 2010 22:02:54 for OCILIB (C Driver for Oracle) by  doxygen 1.7.1