Projet-Systemes-Informatiques/linkedList.c
2023-05-12 15:37:57 +02:00

109 lines
No EOL
2.5 KiB
C

#include "LinkedList.h"
#include "table.h"
/*Add element in linked lis*/
llist addElementLinkedList(char *label, int numLine){
element* newElement = malloc(sizeof(element));
strcpy(newElement->val, label);
newElement->numLine = numLine;
newElement->nxt = NULL;
if (list == NULL) {
return newElement;
}
else {
element* temp=list;
while (temp->nxt != NULL) {
temp = temp->nxt;
}
temp->nxt = newElement;
return list;
}
}
/* Find the numero of the lines with a specific label*/
int findLineElement(char *label){
element *tmp=list;
while (tmp != NULL){
if (strcmp(tmp->val, label)){
return tmp->numLine;
}
tmp = tmp->nxt;
}
error("Label doesn't exist");
return NULL;
}
// TODO : ne pas incrementer les lignes avec LABEL
/* Count Lines and suppress Label */
void countLines(char *s){
FILE *fptr;
char * line = NULL;
size_t len = 0;
ssize_t read;
fptr = fopen(s, "r");
FILE* fOut;
fOut = fopen("asm2", "a");
if(fptr == NULL) {
error("Empty file.");
}
while ((read = getline(&line, &len, fptr)) != -1) {
if (IsInt(line[0])){
char label[LABEL_SIZE];
sprintf(label, "%d_LABEL", numberLines);
addElementLinkedList(label, numberLines);
}
else {
fputs(line, fOut);
numberLines ++;
}
printf("%s", line);
}
fclose(fptr);
fclose(fOut);
}
/* Replace label */
bool IsInt(char c){
for(int i = 0; i < 10; i ++){
if (c == i){
return true;
}
}
return false;
}
void detectLabel(char* s){
char label[LABEL_SIZE];
char numLabel[2];
if (s[0] == 'J'){
if (s[1] == 'M'){
int i = 4;
while (s[i] != '_'){
sprintf(numLabel, "%d", s[i]);
strcat(label, numLabel);
fputs(line, fp); // ajout de ce qu'il y a avant
i++;
}
}
else {
int i = 4 ;
while (s[i] != ' '){
NULL;
i ++;
} i ++;
while (s[i] != '_'){
sprintf(numLabel, "%d", s[i]);
strcat(label, numLabel);
fputs(line, fp); // ajout de ce qu'il y a avant
i++;
}
}
// ajout dans le table
}
} //JMP 12_LABEL
//JMF @cdcjdjk 12_LABEL