Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move code in simgrid::mc
[simgrid.git] / src / xbt / xbt_os_file.c
1 /* xbt_os_file.c -- portable interface to file-related functions            */
2
3 /* Copyright (c) 2007-2010, 2012-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "xbt/sysdep.h"
10 #include "xbt/file.h"    /* this module */
11 #include "xbt/log.h"
12 #include "src/internal_config.h"
13
14 #ifdef _WIN32
15 #include <windows.h>
16 #endif
17
18 #ifndef _MSC_VER
19 #include "libgen.h" /* POSIX dirname */
20 #endif
21
22 /** @brief Get a single line from the stream (reimplementation of the GNU getline)
23  *
24  * This is a reimplementation of the GNU getline function, so that our code don't depends on the GNU libc.
25  *
26  * xbt_getline() reads an entire line from stream, storing the address of the
27  * buffer containing the text into *buf.  The buffer is null-terminated and
28  * includes the newline character, if one was found.
29  *
30  * If *buf is NULL, then xbt_getline() will allocate a buffer for storing the
31  * line, which should be freed by the user program.
32  *
33  * Alternatively, before calling xbt_getline(), *buf can contain a pointer to a
34  * malloc()-allocated buffer *n bytes in size.  If the buffer is not large
35  * enough to hold the line, xbt_getline() resizes it with realloc(), updating
36  * *buf and *n as necessary.
37  *
38  * In either case, on a successful call, *buf and *n will be updated to reflect
39  * the buffer address and allocated size respectively.
40  */
41 ssize_t xbt_getline(char **buf, size_t *n, FILE *stream)
42 {
43   ssize_t i;
44   int ch;
45
46   ch = getc(stream);
47   if (ferror(stream) || feof(stream))
48     return -1;
49
50   if (!*buf) {
51     *n = 512;
52     *buf = xbt_malloc(*n);
53   }
54
55   i = 0;
56   do {
57     if (i == *n)
58       *buf = xbt_realloc(*buf, *n += 512);
59     (*buf)[i++] = ch;
60   } while (ch != '\n' && (ch = getc(stream)) != EOF);
61
62   if (i == *n)
63     *buf = xbt_realloc(*buf, *n += 1);
64   (*buf)[i] = '\0';
65
66   return i;
67 }
68
69 /** @brief Returns the directory component of a path (reimplementation of POSIX dirname)
70  *
71  * The argument is never modified, and the returned value must be freed after use.
72  */
73 char *xbt_dirname(const char *path) {
74 #ifdef _MSC_VER
75     char drive[_MAX_DRIVE];
76     char dir[_MAX_DIR];
77     errno_t err;
78     err = _splitpath_s(path, drive, _MAX_DRIVE, dir, _MAX_DIR, NULL,0, NULL,0);
79     return bprintf("%s%s",drive,dir);
80 #else
81     char *tmp = xbt_strdup(path);
82     char *res = xbt_strdup(dirname(tmp));
83     free(tmp);
84     return res;
85 #endif
86 }
87 /** @brief Returns the file component of a path (reimplementation of POSIX basename)
88  *
89  * The argument is never modified, and the returned value must be freed after use.
90  */
91 char *xbt_basename(const char *path) {
92 #ifdef _MSC_VER
93     char file[1024];
94     char ext[1024];
95     errno_t err;
96     err = _splitpath_s(path, NULL,0, NULL,0, file,1024, ext,1024);
97     return bprintf("%s.%s",file,ext);
98 #else
99     char *tmp = xbt_strdup(path);
100     char *res = xbt_strdup(basename(tmp));
101     free(tmp);
102     return res;
103 #endif
104 }