Robot Agent  1.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
file.c
Go to the documentation of this file.
1 
11 /* -- Includes -- */
12 /* system libraries */
13 #include <stdio.h> /* Standard input/output definitions */
14 #include <string.h> /* String function definitions */
15 #include <stdlib.h>
16 #include <time.h>
17 /* project libraries */
18 #include "file.h"
19 #include "def.h"
20 
21 /* -- Defines -- */
22 
23 /* -- Functions -- */
24 
31 file_t *file_open(const char *name, const char *mode)
32 {
33  file_t *fs = (file_t *) malloc(sizeof(file_t)); // File structure
34 
35  memset(fs->name, 0, sizeof(fs->name));
36  memcpy(fs->name, name, strlen(name)); // Set name
37 
38  // Open file
39  fs->fd = fopen(fs->name, mode);
40 
41  // Return file structure
42  return fs;
43 }
44 
52 file_t *file_open_time(const char *name, const char *ext, const char *mode)
53 {
54  file_t *fs = (file_t *) malloc(sizeof(file_t));; // File structure
55 
56  // Time variables
57  time_t now;
58  struct tm *gmt;
59  char formatted_gmt [32];
60 
61  // Get time
62  now = time (NULL);
63  gmt = gmtime (&now);
64 
65  // Format time
66  strftime(formatted_gmt, sizeof(formatted_gmt), "%d%m%y_%H%M%S", gmt);
67  // Set name
68  sprintf(fs->name,"%s_%s.%s", name, formatted_gmt, ext);
69 
70  // Open file
71  fs->fd = fopen(fs->name, mode);
72 
73  // Return file structure
74  return fs;
75 }
76 
83 {
84  int res;
85 
86  // Close File
87  res = fclose(fs->fd);
88  // Free memory
89  free(fs);
90 
91  return res;
92 }
93 
101 int file_write(file_t *fs, char *data, int len)
102 {
103  // Write to file
104  return fwrite(data, 1, len, fs->fd);;
105 }
106 
114 int file_write_direct(file_t *fs, char *data, int len)
115 {
116  int res;
117 
118  // Write to file
119  res = fwrite(data, 1, len, fs->fd);
120  // Flush data
121  if(fflush(fs->fd) == s_OK)
122  return res;
123  else
124  return s_ERROR;
125 }
126 
133 {
134  // Flush data
135  return fflush(fs->fd);
136 }
137 
145 int file_readln(file_t *fs, char *buffer, size_t buflen)
146 {
147  char *end = buffer + buflen - 1; // Allow space for null terminator
148  char *dst = buffer;
149  int c;
150 
151  while ((c = getc(fs->fd)) != EOF && c != '\n' && dst < end)
152  {
153  *dst++ = c;
154  }
155  *dst = '\0';
156 
157  return((c == EOF && dst == buffer) ? EOF : dst - buffer);
158 }
int file_write(file_t *fs, char *data, int len)
Definition: file.c:101
FILE * fd
Definition: file.h:25
file_t * file_open_time(const char *name, const char *ext, const char *mode)
Definition: file.c:52
int file_readln(file_t *fs, char *buffer, size_t buflen)
Definition: file.c:145
file_t * file_open(const char *name, const char *mode)
Definition: file.c:31
int file_close(file_t *fs)
Definition: file.c:82
File structure.
Definition: file.h:23
#define s_OK
Definition: def.h:64
char name[256]
Definition: file.h:26
#define s_ERROR
Definition: def.h:65
int file_flush(file_t *fs)
Definition: file.c:132
int file_write_direct(file_t *fs, char *data, int len)
Definition: file.c:114