27 #include <oc_stdlib.h> 30 #include <oc_system_cfg.h> 43 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 46 .
Name =
"stdio module" 49 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 57 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 61 int vprintf(
const char * Format , va_list ArgumentList )
63 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
64 oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
65 bool bufferAllocated =
false;
67 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
71 if(!oC_AssignErrorCode(&errorCode , oC_Process_LockStdioBuffer(process,&buffer,
ms(10))))
73 oC_SaveError(
"vprintf:Cannot lock stdio buffer: " , errorCode);
75 buffer = ksmartalloc(CFG_BYTES_STDIO_BUFFER_SIZE,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
76 bufferAllocated =
true;
81 errorCode = oC_KPrint_VPrintf(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,oC_Process_GetIoFlags(process),Format,ArgumentList);
85 errorCode = oC_Process_UnlockStdioBuffer(process);
87 else if(ksmartfree(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,AllocationFlags_CanWaitForever))
89 errorCode = oC_ErrorCode_None;
93 errorCode = oC_ErrorCode_ReleaseError;
98 errorCode = oC_ErrorCode_AllocationError;
107 int printf(
const char * Format , ... )
109 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
110 oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
111 bool bufferAllocated =
false;
113 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
115 char * buffer =
NULL;
117 if(!oC_AssignErrorCode(&errorCode , oC_Process_LockStdioBuffer(process,&buffer,
ms(10))))
119 oC_SaveError(
"Cannot lock stdio buffer" , errorCode);
121 buffer = ksmartalloc(CFG_BYTES_STDIO_BUFFER_SIZE,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
122 bufferAllocated =
true;
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);
134 errorCode = oC_Process_UnlockStdioBuffer(process);
136 else if(ksmartfree(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,AllocationFlags_CanWaitForever))
138 errorCode = oC_ErrorCode_None;
142 errorCode = oC_ErrorCode_ReleaseError;
147 errorCode = oC_ErrorCode_AllocationError;
156 int puts(
const char * String )
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;
163 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
165 char * buffer =
NULL;
167 bufferSize = strlen(String) + strlen(
"\n\r") + 1;
169 if(!oC_AssignErrorCode(&errorCode , oC_Process_LockStdioBuffer(process,&buffer,
ms(10))))
171 oC_SaveError(
"printf:Cannot lock stdio buffer: " , errorCode);
173 buffer = ksmartalloc(bufferSize,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
174 bufferAllocated =
true;
179 strcpy(buffer,String);
180 strcpy(&buffer[strlen(String)],
"\n\r");
182 errorCode = oC_KPrint_WriteToStdOut(oC_Process_GetIoFlags(process),buffer,bufferSize-1);
186 errorCode = oC_Process_UnlockStdioBuffer(process);
188 else if(ksmartfree(buffer,bufferSize,AllocationFlags_CanWaitForever))
190 errorCode = oC_ErrorCode_None;
194 errorCode = oC_ErrorCode_ReleaseError;
199 errorCode = oC_ErrorCode_AllocationError;
208 int putc(
int C, FILE *stream)
210 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
212 if(stream == stdout || stream == stderr)
214 errorCode = oC_KPrint_WriteToStdOut(oC_Process_GetIoFlags(oC_ProcessMan_GetCurrentProcess()),(
char*)&C,1);
218 errorCode = oC_VirtualFileSystem_putc(C,stream);
226 int sprintf_s(
char * outString , oC_UInt_t Size ,
const char * Format, ... )
228 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
231 ErrorCondition(isram(outString) , oC_ErrorCode_OutputAddressNotInRAM) &&
232 ErrorCondition(isaddresscorrect(Format) , oC_ErrorCode_WrongAddress)
235 va_list argumentList;
236 va_start(argumentList, Format);
238 va_end(argumentList);
246 int scanf(
const char * Format , ... )
248 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
249 oC_Process_t process = oC_ProcessMan_GetCurrentProcess();
251 if(oC_AssignErrorCodeIfFalse(&errorCode , oC_Process_IsCorrect(process) , oC_ErrorCode_ProcessNotCorrect))
253 char * buffer = ksmartalloc(CFG_BYTES_STDIO_BUFFER_SIZE,&Allocator,AllocationFlags_CanWaitForever | AllocationFlags_ZeroFill );
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);
262 if(!ksmartfree(buffer,CFG_BYTES_STDIO_BUFFER_SIZE,AllocationFlags_CanWaitForever))
264 errorCode = oC_ErrorCode_ReleaseError;
269 errorCode = oC_ErrorCode_AllocationError;
278 extern int sscanf(
const char * String ,
const char * Format , ...)
280 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
282 va_list argumentList;
283 va_start(argumentList, Format);
284 errorCode = oC_KPrint_FormatScanf(String,Format,argumentList);
285 va_end(argumentList);
293 int oC_GetArgumentNumber(
int Argc ,
char ** Argv ,
const char * Argument )
295 bool argumentOccur =
false;
298 if(isaddresscorrect(Argv) || isaddresscorrect(Argument) )
300 int argumentLength = strlen(Argument);
302 for( index = 0 ; index < Argc && !argumentOccur ; index++)
304 int length = strlen(Argv[index]);
306 argumentOccur = length == argumentLength;
308 for(
int signIndex = 0; signIndex < length && argumentOccur ; signIndex++)
310 argumentOccur = tolower((
int)Argv[index][signIndex]) == tolower((
int)Argument[signIndex]);
316 return (argumentOccur ==
true) ? index - 1 : -1;
321 bool oC_ArgumentOccur(
int Argc ,
char ** Argv ,
const char * Argument )
323 return oC_GetArgumentNumber(Argc, Argv, Argument) >= 0;
328 bool oC_IsMemSetTo(
void * Buffer , oC_UInt_t Size , uint8_t Value )
331 uint8_t* buffer = Buffer;
333 for(oC_UInt_t i = 0 ; i < Size ; i++ )
335 if(buffer[i] != Value)
347 FILE * fopen (
const char * FileName,
const char * Mode )
351 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
352 bool modeCorrect =
false;
355 ErrorCondition(isaddresscorrect(FileName) , oC_ErrorCode_WrongAddress)
356 && ErrorCondition(isaddresscorrect(Mode) , oC_ErrorCode_WrongAddress)
359 if(strcmp(Mode,
"r") == 0)
365 else if(strcmp(Mode,
"w") == 0)
371 else if(strcmp(Mode,
"a") == 0)
378 else if(strcmp(Mode,
"r+") == 0)
385 else if(strcmp(Mode,
"w+") == 0)
392 else if(strcmp(Mode,
"a+") == 0)
401 kdebuglog(oC_LogType_Error ,
"fopen: mode %s is not correct" , Mode);
406 errorCode = oC_VirtualFileSystem_fopen((
oC_File_t*)&file,FileName,mode,0);
407 if(oC_ErrorOccur(errorCode))
409 kdebuglog(oC_LogType_Error,
"fopen: Cannot open file %s - %s" , FileName ,
oC_GetErrorString(errorCode));
416 oC_SaveError(
"fopen: cannot open file - " , errorCode);
424 int fclose ( FILE * stream )
426 return oC_VirtualFileSystem_fclose(stream);
431 size_t fread (
void * Buffer,
size_t ElementSize,
size_t Count, FILE * File )
433 uint32_t readSize = ElementSize * Count;
435 oC_SaveIfErrorOccur(
"fread: cannot read file" , oC_VirtualFileSystem_fread(File,Buffer,&readSize));
437 return (
size_t)readSize;
442 size_t fwrite(
const void * Buffer,
size_t ElementSize,
size_t Count, FILE * File )
444 uint32_t writtenSize = ElementSize * Count;
446 oC_SaveIfErrorOccur(
"fread: cannot read file" , oC_VirtualFileSystem_fwrite(File,Buffer,&writtenSize));
448 return (
size_t)writtenSize;
453 int feof( FILE * File )
455 return (
int)oC_VirtualFileSystem_eof(File);
459 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ open file (create new if it does not exist)
specifies read access to the file
identifier for allocations
The file with interface for process manager.
#define ms(time)
Number of ms.
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
The file with interface for file systems.
create new file (overwrite if exists)
specifies write access to the file
open file (fails if file does not exist)
const char * oC_GetErrorString(oC_ErrorCode_t ErrorCode)
moves offset pointer to the end of the file
oC_ErrorCode_t oC_KPrint_Format(char *outBuffer, oC_UInt_t Size, const char *Format, va_list ArgumentList)
The file with standard input/output operations.
#define NULL
pointer to a zero