NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
FileInput.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* Author: Shashi kant suman
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "NetSim_utility.h"
16#include <signal.h>
17#include "CLI.h"
18
19static ptrCLIENTINFO fileClientInfo = NULL;
20
21void set_fileClientInfo(ptrCLIENTINFO info)
22{
23 fileClientInfo = info;
24}
25
26ptrCLIENTINFO get_fileClientInfo()
27{
28 return fileClientInfo;
29}
30
31void read_file_and_execute(FILE* fp)
32{
33 double time = 0;
34 NETSIM_ID dev = 0;
35
36 char input[BUFSIZ];
37 NetSim_EVENTDETAILS pevent;
38 memset(&pevent, 0, sizeof pevent);
39 pevent.nEventType = TIMER_EVENT;
40 pevent.nProtocolId = PROTOCOL_CLI;
41 pevent.nSubEventType = SUBEVENT_EXECUTECOMMAND;
42 while (fgets(input, BUFSIZ, fp) != NULL)
43 {
44 char* s = input;
45 s = lskip(s);
46 if (!*s || *s == '#')
47 continue; //Empty or comment line
48
49 _strupr(s);
50
51 size_t len = strlen(s);
52 if (len > 0 && s[len - 1] == '\n') s[--len] = '\0';
53
54 if (*s == 'T')
55 {
56 char* f = strtok(s, "=");
57 if (!_stricmp(f, "time"))
58 {
59 char* l = strtok(NULL, "=\n");
60 time = atof(l)*SECOND;
61 }
62 else
63 {
64 goto CMDINPUT;
65 }
66 }
67 else if (*s == 'D')
68 {
69 char* f = strtok(s, "=");
70 if (!_stricmp(f, "device"))
71 {
72 char* l = strtok(NULL, "=\n");
73 dev = fn_NetSim_Stack_GetDeviceId_asName(l);
74 }
75 else
76 {
77 goto CMDINPUT;
78 }
79 }
80 else
81 {
82 CMDINPUT:
83 pevent.dEventTime = time;
84 pevent.nDeviceId = dev;
85 pevent.nDeviceType = DEVICE_TYPE(dev);
86 ptrCOMMANDARRAY cmd = get_commandArray(s);
87 pevent.szOtherDetails = FORM_CLI_HANDLE(cmd, get_fileClientInfo());
88 fnpAddEvent(&pevent);
89 }
90 }
91}
92
93void write_to_file(ptrCLIENTINFO info, char* msg, int len)
94{
95 fwrite(msg, sizeof(char), len, info->CLIENT.fileClient.fpOutputFile);
96 fflush(info->CLIENT.fileClient.fpOutputFile);
97}