-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patholdmatformatter.c
More file actions
242 lines (209 loc) · 6.08 KB
/
oldmatformatter.c
File metadata and controls
242 lines (209 loc) · 6.08 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
// Goes through an integer array of memory keys and gets rid of the shared memory segments
void releaseSharedMemory(int *keys, int numKeys) {
int i;
for (i = 0; i < numKeys; i++) {
shmctl(keys[i], IPC_RMID, NULL);
}
return;
}
// Deallocates memory
void freeMem(char **stringArray, int numRows) {
int i = 0;
for (i = 0; i < numRows; i++) {
free(stringArray[i]);
}
free(stringArray);
return;
}
// Returns 0 on success, -1 on failure, or 1 if it only read in ONE matrix (e.g. only one aws input), leaving the fields
// null.
int readTwoMatrices(int *leftMatrix, int *rightMatrix, int *rows1, int *cols1, int *rows2, int *cols2) {
//printf("here\n");
char **temp = calloc(MAX_MATRIX_DIMENSION, sizeof(int));
char **temp2 = calloc(MAX_MATRIX_DIMENSION, sizeof(int));
int oneMatrix = 0;
// Read in the first line as a special case so I can know how many columns matrix 1 will be. Need to also count the max size of a number to know
// how big this will be. ALSO check to make sure something is input at all; if not it causes it to freeze up
int colSize = (MAX_DIGITS_INT + 1)*MAX_MATRIX_DIMENSION;
int numRows1 = 0;
while (numRows1 < MAX_MATRIX_DIMENSION) {
temp[numRows1] = calloc(colSize, 1);
fgets(temp[numRows1], colSize, stdin);
int tempVal = temp[numRows1][0] == '\n';
int tempVal2 = temp[numRows1][0] == EOF || temp[numRows1][0] == '\0';
if (tempVal2) {
oneMatrix = 1;
break;
} if (tempVal) {
break;
}
numRows1++;
}
int numRows2 = 0;
while (numRows2 < MAX_MATRIX_DIMENSION && !oneMatrix) {
temp2[numRows2] = calloc(colSize, 1);
fgets(temp2[numRows2], colSize, stdin);
int tempVal = (temp2[numRows2][0] == '\n' || temp2[numRows2][0] == EOF || temp2[numRows2][0] == '\0');
if (tempVal) {
break;
}
numRows2++;
}
int itemsCol1 = 1;
int i;
// ItemsInCol1 starts at 1 b/c will run off edge of buffer and stop loop on last item
for (i = 0; temp[0][i] != '\0'; i++) {
if (temp[0][i] == ' ') {
itemsCol1++;
}
}
int itemsCol2 = 1;
for (i = 0; !oneMatrix && temp2[0][i] != '\0'; i++) {
if (temp2[0][i] == ' ') {
itemsCol2++;
}
}
if ((itemsCol1 != numRows2) && !oneMatrix) {
printf("%d\t%d\t%d\t%d\n", numRows1, itemsCol1, numRows2, itemsCol2);
printf("Invalid input matrices. Matrixes must be of A x B and B x C size.\n");
freeMem(temp, numRows1);
freeMem(temp2, numRows2);
return -1;
}
int leftArrayKey = shmget(0, itemsCol1*numRows1*sizeof(int), IPC_CREAT | S_IRUSR | S_IWUSR);
if (leftArrayKey < 0) {
printf("%d\t%d\n", itemsCol1, numRows1);
perror("shmget failed: ");
printf("Error allocating shared memory. Aborting\n");
return -1;
}
void *leftArrayVoid = shmat(leftArrayKey, NULL, 0);
if (leftArrayVoid < 0) {
printf("Error allocating shared memory. Aborting\n");
shmctl(leftArrayKey, IPC_RMID, NULL);
return -1;
}
int *leftArray = (int *)leftArrayVoid;
for (i = 0; i < numRows1; i++) {
int j = 0;
int k = 0;
char *num = calloc(MAX_DIGITS_INT, 1);
for (j = 0; temp[i][j] != '\n' && temp[i][j] != EOF && temp[i][j] != '\0'; j++) {
if (temp[i][j] == ' ') {
leftArray[i*itemsCol1 + k] = atoi(num);
//printf("%d\t", leftArray[i*itemsCol1 + k]);
k++;
free(num);
num = calloc(MAX_DIGITS_INT, 1);
}
char *tempStore = calloc(2, 1);
tempStore[0] = temp[i][j];
strncat(num, tempStore, 2);
free(tempStore);
}
leftArray[i*itemsCol1 + k] = atoi(num);
//printf("%d\n", leftArray[i*itemsCol1 + k]);
free(num);
}
if (oneMatrix) {
freeMem(temp, numRows1);
freeMem(temp2, numRows2);
*leftMatrix = leftArrayKey;
*rows1 = numRows1;
*cols1 = itemsCol1;
return 1;
}
// Sets up the "right" (secodn array)s
int rightArrayKey = shmget(IPC_PRIVATE, itemsCol2*numRows2*sizeof(int), IPC_CREAT | S_IRUSR | S_IWUSR);
if (rightArrayKey < 0) {
printf("Error allocating shared memory. Aborting\n");
shmctl(leftArrayKey, IPC_RMID, NULL);
return -1;
}
void *rightArrayVoid = shmat(rightArrayKey, NULL, 0);
if (rightArrayVoid < 0) {
printf("Error allocating shared memory. Aborting\n");
shmctl(leftArrayKey, IPC_RMID, NULL);
shmctl(rightArrayKey, IPC_RMID, NULL);
return -1;
}
int *rightArray = (int *)rightArrayVoid;
for (i = 0; i < numRows2; i++) {
int j = 0;
int k = 0;
char *num = calloc(MAX_DIGITS_INT, 1);
for (j = 0; temp2[i][j] != '\n' || temp2[i][j] == EOF; j++) {
if (temp2[i][j] == ' ') {
rightArray[i*itemsCol2 + k] = atoi(num);
//printf("%d\t", rightArray[i*itemsCol2 + k]);
k++;
free(num);
num = calloc(MAX_DIGITS_INT, 1);
}
char *tempStore = calloc(2, 1);
tempStore[0] = temp2[i][j];
strncat(num, tempStore, 2);
free(tempStore);
}
rightArray[i*itemsCol2 + k] = atoi(num);
//printf("%d\n", rightArray[i*itemsCol2 + k]);
free(num);
}
//printf("last element first row: %d\n", rightArray[24]);
freeMem(temp, numRows1);
freeMem(temp2, numRows2);
// Storing everything away.
*leftMatrix = leftArrayKey;
*rightMatrix = rightArrayKey;
*rows1 = numRows1;
*cols1 = itemsCol1;
*rows2 = numRows2;
*cols2 = itemsCol2;
return 0;
}
int main(int argc, char **argv) {
int i;
int leftArrayKey;
int rightArrayKey;
int numRows1;
int itemsCol1;
int numRows2 = 8675309;
int itemsCol2;
int val = readTwoMatrices(&leftArrayKey, &rightArrayKey, &numRows1, &itemsCol1, &numRows2, &itemsCol2);
if (val != 1) {
return -1;
}
int *array = (int *)shmat(leftArrayKey, NULL, 0);
if (array == (void *) -1) {
printf("error accessing shared memory\n");
return -1;
}
int transpose[itemsCol1][numRows1];
for (i = 0; i < itemsCol1; i++) {
int j;
for (j = 0; j < numRows1; j++) {
transpose[i][j] = array[j*itemsCol1 + i];
printf("%d", transpose[i][j]);
if (j != (numRows1 - 1)) {
printf(" ");
}
}
/*
if (i != (itemsCol1 - 1)) {
printf("\n");
}
*/
printf("\n");
}
int keys[1];
keys[0] = leftArrayKey;
releaseSharedMemory(keys,1);
return 0;
}