Choco OS  V.0.16.9.0
Join to the chocolate world
oc_stdio.c
Go to the documentation of this file.
1 
26 #include <oc_stdio.h>
27 #include <oc_stdlib.h>
28 #include <oc_kprint.h>
29 #include <oc_streamman.h>
30 #include <oc_system_cfg.h>
31 #include <oc_processman.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <oc_debug.h>
35 #include <oc_fs.h>
36 #include <oc_vfs.h>
37 
43 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________
44 
45 static const oC_Allocator_t Allocator = {
46  .Name = "stdio module"
47 };
48 
49 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________
50 
51 
57 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
58 
59 //==========================================================================================================================================
60 //==========================================================================================================================================
61 int vprintf( const char * Format , va_list ArgumentList )
62 {
63  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
64  oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
65  bool bufferAllocated = false;
66 
67  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
68  {
69  char * buffer = NULL;
70 
71  if(!oC_AssignErrorCode(&errorCode , oC_Process_LockStdioBuffer(process,&buffer,ms(10))))
72  {
73  oC_SaveError("vprintf:Cannot lock stdio buffer: " , errorCode);
74 
75  buffer = ksmartalloc(CFG_BYTES_STDIO_BUFFER_SIZE,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
76  bufferAllocated = true;
77  }
78 
79  if(buffer)
80  {
81  errorCode = oC_KPrint_VPrintf(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,oC_Process_GetIoFlags(process),Format,ArgumentList);
82 
83  if(!bufferAllocated)
84  {
85  errorCode = oC_Process_UnlockStdioBuffer(process);
86  }
87  else if(ksmartfree(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,AllocationFlags_CanWaitForever))
88  {
89  errorCode = oC_ErrorCode_None;
90  }
91  else
92  {
93  errorCode = oC_ErrorCode_ReleaseError;
94  }
95  }
96  else
97  {
98  errorCode = oC_ErrorCode_AllocationError;
99  }
100  }
101 
102  return errorCode;
103 }
104 
105 //==========================================================================================================================================
106 //==========================================================================================================================================
107 int printf( const char * Format , ... )
108 {
109  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
110  oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
111  bool bufferAllocated = false;
112 
113  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
114  {
115  char * buffer = NULL;
116 
117  if(!oC_AssignErrorCode(&errorCode , oC_Process_LockStdioBuffer(process,&buffer,ms(10))))
118  {
119  oC_SaveError("Cannot lock stdio buffer" , errorCode);
120 
121  buffer = ksmartalloc(CFG_BYTES_STDIO_BUFFER_SIZE,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
122  bufferAllocated = true;
123  }
124 
125  if(buffer)
126  {
127  va_list argumentList;
128  va_start(argumentList, Format);
129  errorCode = oC_KPrint_VPrintf(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,oC_Process_GetIoFlags(process),Format,argumentList);
130  va_end(argumentList);
131 
132  if(!bufferAllocated)
133  {
134  errorCode = oC_Process_UnlockStdioBuffer(process);
135  }
136  else if(ksmartfree(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,AllocationFlags_CanWaitForever))
137  {
138  errorCode = oC_ErrorCode_None;
139  }
140  else
141  {
142  errorCode = oC_ErrorCode_ReleaseError;
143  }
144  }
145  else
146  {
147  errorCode = oC_ErrorCode_AllocationError;
148  }
149  }
150 
151  return errorCode;
152 }
153 
154 //==========================================================================================================================================
155 //==========================================================================================================================================
156 int puts( const char * String )
157 {
158  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
159  oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
160  bool bufferAllocated = false;
161  oC_UInt_t bufferSize = 0;
162 
163  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
164  {
165  char * buffer = NULL;
166 
167  bufferSize = strlen(String) + strlen("\n\r") + 1;
168 
169  if(!oC_AssignErrorCode(&errorCode , oC_Process_LockStdioBuffer(process,&buffer,ms(10))))
170  {
171  oC_SaveError("printf:Cannot lock stdio buffer: " , errorCode);
172 
173  buffer = ksmartalloc(bufferSize,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
174  bufferAllocated = true;
175  }
176 
177  if(buffer)
178  {
179  strcpy(buffer,String);
180  strcpy(&buffer[strlen(String)],"\n\r");
181 
182  errorCode = oC_KPrint_WriteToStdOut(oC_Process_GetIoFlags(process),buffer,bufferSize-1);
183 
184  if(!bufferAllocated)
185  {
186  errorCode = oC_Process_UnlockStdioBuffer(process);
187  }
188  else if(ksmartfree(buffer,bufferSize,AllocationFlags_CanWaitForever))
189  {
190  errorCode = oC_ErrorCode_None;
191  }
192  else
193  {
194  errorCode = oC_ErrorCode_ReleaseError;
195  }
196  }
197  else
198  {
199  errorCode = oC_ErrorCode_AllocationError;
200  }
201  }
202 
203  return errorCode;
204 }
205 
206 //==========================================================================================================================================
207 //==========================================================================================================================================
208 int putc(int C, FILE *stream)
209 {
210  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
211 
212  if(stream == stdout || stream == stderr)
213  {
214  errorCode = oC_KPrint_WriteToStdOut(oC_Process_GetIoFlags(oC_ProcessMan_GetCurrentProcess()),(char*)&C,1);
215  }
216  else
217  {
218  errorCode = oC_VirtualFileSystem_putc(C,stream);
219  }
220 
221  return errorCode;
222 }
223 
224 //==========================================================================================================================================
225 //==========================================================================================================================================
226 int sprintf_s( char * outString , oC_UInt_t Size , const char * Format, ... )
227 {
228  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
229 
230  if(
231  ErrorCondition(isram(outString) , oC_ErrorCode_OutputAddressNotInRAM) &&
232  ErrorCondition(isaddresscorrect(Format) , oC_ErrorCode_WrongAddress)
233  )
234  {
235  va_list argumentList;
236  va_start(argumentList, Format);
237  errorCode = oC_KPrint_Format(outString,Size,Format,argumentList);
238  va_end(argumentList);
239  }
240 
241  return errorCode;
242 }
243 
244 //==========================================================================================================================================
245 //==========================================================================================================================================
246 int scanf( const char * Format , ... )
247 {
248  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
249  oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
250 
251  if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
252  {
253  char * buffer = ksmartalloc(CFG_BYTES_STDIO_BUFFER_SIZE,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
254 
255  if(buffer)
256  {
257  va_list argumentList;
258  va_start(argumentList, Format);
259  errorCode = oC_KPrint_VScanf(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,oC_Process_GetIoFlags(process),Format,argumentList);
260  va_end(argumentList);
261 
262  if(!ksmartfree(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,AllocationFlags_CanWaitForever))
263  {
264  errorCode = oC_ErrorCode_ReleaseError;
265  }
266  }
267  else
268  {
269  errorCode = oC_ErrorCode_AllocationError;
270  }
271  }
272 
273  return errorCode;
274 }
275 
276 //==========================================================================================================================================
277 //==========================================================================================================================================
278 extern int sscanf( const char * String , const char * Format , ...)
279 {
280  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
281 
282  va_list argumentList;
283  va_start(argumentList, Format);
284  errorCode = oC_KPrint_FormatScanf(String,Format,argumentList);
285  va_end(argumentList);
286 
287  return errorCode;
288 }
289 
290 //==========================================================================================================================================
291 // -1 if didn't occur
292 //==========================================================================================================================================
293 int oC_GetArgumentNumber( int Argc , char ** Argv , const char * Argument )
294 {
295  bool argumentOccur = false;
296  int index = 0;
297 
298  if(isaddresscorrect(Argv) || isaddresscorrect(Argument) )
299  {
300  int argumentLength = strlen(Argument);
301 
302  for( index = 0 ; index < Argc && !argumentOccur ; index++)
303  {
304  int length = strlen(Argv[index]);
305 
306  argumentOccur = length == argumentLength;
307 
308  for(int signIndex = 0; signIndex < length && argumentOccur ; signIndex++)
309  {
310  argumentOccur = tolower((int)Argv[index][signIndex]) == tolower((int)Argument[signIndex]);
311  }
312  }
313 
314  }
315 
316  return (argumentOccur == true) ? index - 1 : -1;
317 }
318 
319 //==========================================================================================================================================
320 //==========================================================================================================================================
321 bool oC_ArgumentOccur( int Argc , char ** Argv , const char * Argument )
322 {
323  return oC_GetArgumentNumber(Argc, Argv, Argument) >= 0;
324 }
325 
326 //==========================================================================================================================================
327 //==========================================================================================================================================
328 bool oC_IsMemSetTo( void * Buffer , oC_UInt_t Size , uint8_t Value )
329 {
330  bool set = true;
331  uint8_t* buffer = Buffer;
332 
333  for(oC_UInt_t i = 0 ; i < Size ; i++ )
334  {
335  if(buffer[i] != Value)
336  {
337  set = false;
338  break;
339  }
340  }
341 
342  return set;
343 }
344 
345 //==========================================================================================================================================
346 //==========================================================================================================================================
347 FILE * fopen ( const char * FileName, const char * Mode )
348 {
349  FILE * file = NULL;
350  oC_FileSystem_ModeFlags_t mode = 0;
351  oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
352  bool modeCorrect = false;
353 
354  if(
355  ErrorCondition(isaddresscorrect(FileName) , oC_ErrorCode_WrongAddress)
356  && ErrorCondition(isaddresscorrect(Mode) , oC_ErrorCode_WrongAddress)
357  )
358  {
359  if(strcmp(Mode,"r") == 0)
360  {
363  modeCorrect = true;
364  }
365  else if(strcmp(Mode,"w") == 0)
366  {
369  modeCorrect = true;
370  }
371  else if(strcmp(Mode,"a") == 0)
372  {
376  modeCorrect = true;
377  }
378  else if(strcmp(Mode,"r+") == 0)
379  {
383  modeCorrect = true;
384  }
385  else if(strcmp(Mode,"w+") == 0)
386  {
390  modeCorrect = true;
391  }
392  else if(strcmp(Mode,"a+") == 0)
393  {
397  modeCorrect = true;
398  }
399  else
400  {
401  kdebuglog(oC_LogType_Error , "fopen: mode %s is not correct" , Mode);
402  }
403 
404  if(modeCorrect)
405  {
406  errorCode = oC_VirtualFileSystem_fopen((oC_File_t*)&file,FileName,mode,0);
407  if(oC_ErrorOccur(errorCode))
408  {
409  kdebuglog(oC_LogType_Error,"fopen: Cannot open file %s - %s" , FileName , oC_GetErrorString(errorCode));
410  file = NULL;
411  }
412  }
413  }
414  else
415  {
416  oC_SaveError("fopen: cannot open file - " , errorCode);
417  }
418 
419  return file;
420 }
421 
422 //==========================================================================================================================================
423 //==========================================================================================================================================
424 int fclose ( FILE * stream )
425 {
426  return oC_VirtualFileSystem_fclose(stream);
427 }
428 
429 //==========================================================================================================================================
430 //==========================================================================================================================================
431 size_t fread ( void * Buffer, size_t ElementSize, size_t Count, FILE * File )
432 {
433  uint32_t readSize = ElementSize * Count;
434 
435  oC_SaveIfErrorOccur("fread: cannot read file" , oC_VirtualFileSystem_fread(File,Buffer,&readSize));
436 
437  return (size_t)readSize;
438 }
439 
440 //==========================================================================================================================================
441 //==========================================================================================================================================
442 size_t fwrite(const void * Buffer, size_t ElementSize, size_t Count, FILE * File )
443 {
444  uint32_t writtenSize = ElementSize * Count;
445 
446  oC_SaveIfErrorOccur("fread: cannot read file" , oC_VirtualFileSystem_fwrite(File,Buffer,&writtenSize));
447 
448  return (size_t)writtenSize;
449 }
450 
451 //==========================================================================================================================================
452 //==========================================================================================================================================
453 int feof( FILE * File )
454 {
455  return (int)oC_VirtualFileSystem_eof(File);
456 }
457 
458 
459 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________
460 
461 
open file (create new if it does not exist)
Definition: oc_fs.h:65
specifies read access to the file
Definition: oc_fs.h:56
FILE__DESCRIPTION
const char * Name
Definition: oc_stdlib.h:161
identifier for allocations
Definition: oc_stdlib.h:159
The file with interface for process manager.
#define ms(time)
Number of ms.
Definition: oc_cfg.h:128
The file with interface for stream manager.
The file with interface for Virtual File System.
The file with interface for kernel print operations.
oC_FileSystem_ModeFlags_t
Definition: oc_fs.h:54
The file with interface for file systems.
create new file (overwrite if exists)
Definition: oc_fs.h:69
specifies write access to the file
Definition: oc_fs.h:57
open file (fails if file does not exist)
Definition: oc_fs.h:63
const char * oC_GetErrorString(oC_ErrorCode_t ErrorCode)
Definition: oc_errors.c:115
moves offset pointer to the end of the file
Definition: oc_fs.h:58
oC_ErrorCode_t oC_KPrint_Format(char *outBuffer, oC_UInt_t Size, const char *Format, va_list ArgumentList)
Definition: oc_kprint.c:196
The file with standard input/output operations.
#define NULL
pointer to a zero
Definition: oc_null.h:37