Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[simgrid.git] / src / msg / msg_io.c
1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012. The SimGrid Team.
2  * All rights reserved.                                                                  */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package.              */
6
7 #include "msg_private.h"
8 #include "xbt/log.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, simix,
11                                 "Logging specific to MSG (io)");
12
13 /** @addtogroup m_file_management
14  *     \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="File" --> \endhtmlonly
15  * (#msg_file_t) and the functions for managing it.
16  *
17  *  \see #msg_file_t
18  */
19
20 /********************************* File **************************************/
21
22 /** \ingroup m_file_management
23  * \brief Read elements of a file
24  *
25  * \param storage is the name where find the stream
26  * \param ptr buffer to where the data is copied
27  * \param size of each element
28  * \param nmemb is the number of elements of data to read
29  * \param stream to read
30  * \return the number of items successfully read
31  */
32 size_t MSG_file_read(const char* storage, void* ptr, size_t size, size_t nmemb,  msg_file_t stream)
33 {
34   return simcall_file_read(storage, ptr, size, nmemb, stream->simdata->smx_file);
35 }
36
37 /** \ingroup m_file_management
38  * \brief Write elements into a file
39  *
40  * \param storage is the name where find the stream
41  * \param ptr buffer from where the data is copied
42  * \param size of each element
43  * \param nmemb is the number of elements of data to write
44  * \param stream to write
45  * \return the number of items successfully write
46  */
47 size_t MSG_file_write(const char* storage, const void* ptr, size_t size, size_t nmemb, msg_file_t stream)
48 {
49   return simcall_file_write(storage, ptr, size, nmemb, stream->simdata->smx_file);
50 }
51
52 /** \ingroup m_file_management
53  * \brief Opens the file whose name is the string pointed to by path
54  *
55  * \param storage is the name where find the file to open
56  * \param path is the file location on the storage
57  * \param mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
58  *      r      Open text file for reading.  The stream is positioned at the beginning of the file.
59  *      r+     Open for reading and writing.  The stream is positioned at the beginning of the file.
60  *      w      Truncate file to zero length or create text file for writing.  The stream is positioned at the beginning of the file.
61  *      w+     Open for reading and writing.  The file is created if it does not exist, otherwise it is truncated.  The stream is positioned at the
62  *             beginning of the file.
63  *      a      Open for appending (writing at end of file).  The file is created if it does not exist.  The stream is positioned at the end of the file.
64  *      a+     Open for reading and appending (writing at end of file).  The file is created if it does not exist.  The initial file position for  reading
65  *             is at the beginning of the file, but output is always appended to the end of the file.
66  *
67  * \return An #msg_file_t associated to the file
68  */
69 msg_file_t MSG_file_open(const char* storage, const char* path, const char* mode)
70 {
71   msg_file_t file = xbt_new(s_msg_file_t,1);
72   file->name = strdup(path);
73   file->simdata = xbt_new0(s_simdata_file_t,1);
74   file->simdata->smx_file = simcall_file_open(storage, path, mode);
75   return file;
76 }
77
78 /** \ingroup m_file_management
79  * \brief Close the file
80  *
81  * \param storage is the name where find the stream
82  * \param fp is the file to close
83  * \return 0 on success or 1 on error
84  */
85 int MSG_file_close(const char* storage, msg_file_t fp)
86 {
87   return simcall_file_close(storage, fp->simdata->smx_file);
88 }
89
90 /** \ingroup m_file_management
91  * \brief Stats the file pointed by fd
92  *
93  * \param storage is the name where find the stream
94  * \param fd is the file descriptor (#msg_file_t)
95  * \param buf is the return structure with informations
96  * \return 0 on success or 1 on error
97  */
98 int MSG_file_stat(const char* storage, msg_file_t fd, s_msg_stat_t *buf)
99 {
100   int res;
101   res = simcall_file_stat(storage, fd->simdata->smx_file, buf);
102   return res;
103 }