Logo AND Algorithmique Numérique Distribuée

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