summaryrefslogtreecommitdiff
path: root/modules/scicos_blocks/src/c/writec.c
blob: 0f972f82bc02dd80f4fd81d1e126b2faf3132d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*  Scicos
*
*  Copyright (C) INRIA - METALAU Project <scicos@inria.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* See the file ./license.txt
*/
/*--------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "scicos_print.h"
#include "machine.h"
#include "charEncoding.h"
#include "cvstr.h"
#include "mput.h"
#include "localization.h"
#include "MALLOC.h"
#include "dynlib_scicos_blocks.h"
/*--------------------------------------------------------------------------*/
#include <sys/time.h>
/**
 * Returns the current time in microseconds.
 */
long getMicrotime(){
	struct timeval currentTime;
	gettimeofday(&currentTime, NULL);
	return currentTime.tv_sec * (int)1e6 + currentTime.tv_usec;
}
SCICOS_BLOCKS_IMPEXP void writec(int *flag, int *nevprt,
                                 double *t, double xd[],
                                 double x[], int *nx,
                                 double z[], int *nz,
                                 double tvec[], int *ntvec,
                                 double rpar[], int *nrpar,
                                 int ipar[], int *nipar,
                                 double *inptr[], int insz[],
                                 int *nin, double *outptr[],
                                 int outsz[], int *nout)
/*
ipar[1]   = lfil : file name length
ipar[2:4] = fmt  : numbers type ascii code
ipar[5]   = n : buffer length in number of records
ipar[6]   = swap
ipar[7:6+lfil] = character codes for file name
*/

{
   FILE* filePointer;
	int processId;
	char fileName[25];
	char line[100];
	filePointer = NULL;
	processId = 0;
	processId = getpid(); // On Linux
	sprintf(fileName, "scilab-log-%d.txt", processId); 
	filePointer = fopen(fileName, "a");
    char str[100], type[4];
    int job = 1, three = 3;
    FILE *fd = NULL;
    int n = 0, k = 0, i = 0, ierr = 0;
    double *buffer = NULL, *record = NULL;

    --ipar;
    --z;
    fd = (FILE *)(long)z[2];
    buffer = (z + 3);
    ierr = 0;
    /*
    k    : record counter within the buffer
    */

    if (*flag == 2 && *nevprt > 0) /* add a new record to the buffer */
    {
        n    = ipar[5];
        k    = (int)z[1];
        /* copy current record to output */
        record = buffer + (k - 1) * (insz[0]);

        for (i = 0; i < insz[0]; i++)
        {
            record[i] = *(inptr[0] + i);
        }
        if (k < n)
        {
            z[1] = z[1] + 1.0;
        }
        else  /* buffer is full write it to the file */
        {
            F2C(cvstr)(&three, &(ipar[2]), type, &job, sizeof(type));
            for (i = 2; i >= 0; i--)
                if (type[i] != ' ')
                {
                    type[i + 1] = '\0';
                    break;
                }
            mput2(fd, ipar[6], buffer, ipar[5]*insz[0], type, &ierr);
            if (ierr != 0)
            {
                *flag = -3;
                return;
            }
            z[1] = 1.0;
        }
    }
    else if (*flag == 4)
    {
        F2C(cvstr)(&(ipar[1]), &(ipar[7]), str, &job, sizeof(str));
        str[ipar[1]] = '\0';
        //writing to the log file
        int block_id=21;
        long rand=getMicrotime();
        sprintf(str,"%s%d%ld", str,processId,rand);
        fprintf(filePointer, "%d || Initialization %d\n", processId, get_block_number());//-1 as no figure uid
        fprintf(filePointer,"%d %d || %d || %s\n",block_id,processId, get_block_number(),str);
        wcfopen(fd, str, "wb");
        if (!fd )
        {
            scicos_print(_("Could not open the file!\n"));
            *flag = -3;
            return;
        }
        z[2] = (long)fd;
        z[1] = 1.0;
    }
    else if (*flag == 5)
    {
        
        if (z[2] == 0)
        {
            return;
        }
        k    = (int) z[1];
        if (k >= 1) /* flush rest of buffer */
        {
            F2C(cvstr)(&three, &(ipar[2]), type, &job, sizeof(type));
            for (i = 2; i >= 0; i--)
                if (type[i] != ' ')
                {
                    type[i + 1] = '\0';
                    break;
                }
            mput2(fd, ipar[6], buffer, (k - 1)*insz[0], type, &ierr);
            if (ierr != 0)
            {
                *flag = -3;
                return;
            }
        }
        fclose(fd);
        fprintf(filePointer, "%d || Ending %d\n", processId,  get_block_number());
        z[2] = 0.0;
    }
   fclose(filePointer);
    return;
}
/*--------------------------------------------------------------------------*/