changes on checkPointer to add message
This commit is contained in:
1 parent
d90e7f0e10
commit
b514e98e70
4 files changed
+11
-12
No files matched your search
+2
-2
@@ -7,9 +7,9 @@ int max(int a, int b) {
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkPointer(void* p) {
|
void checkPointer(void* p, char* errMessage) {
|
||||||
if (!p) {
|
if (!p) {
|
||||||
fprintf(stderr, "Error on given pointer.\n");
|
fprintf(stderr, "%s", errMessage);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -26,6 +26,6 @@ int max(int a, int b);
|
|||||||
*
|
*
|
||||||
* @param p any pointer
|
* @param p any pointer
|
||||||
*/
|
*/
|
||||||
void checkPointer(void* p);
|
void checkPointer(void* p, char* errMessage);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+2
-3
@@ -13,7 +13,6 @@ void usage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
// check if nb of param is correct
|
// check if nb of param is correct
|
||||||
if (argc != 3 && argc != 4) {
|
if (argc != 3 && argc != 4) {
|
||||||
// nb param is not correct
|
// nb param is not correct
|
||||||
@@ -48,8 +47,8 @@ int main(int argc, char* argv[]) {
|
|||||||
if (argc == 4) {
|
if (argc == 4) {
|
||||||
char* filename = argv[3];
|
char* filename = argv[3];
|
||||||
FILE* f = fopen(filename, "w");
|
FILE* f = fopen(filename, "w");
|
||||||
checkPointer((void*)f);
|
checkPointer((void*)f, "Couldn't open file\n");
|
||||||
test_output(fprintf(f, "%u\n", nbsolutions), 2, "Error in scan of file");
|
test_output(fprintf(f, "%u\n", nbsolutions), 2, "Error in scan of file\n");
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-6
@@ -14,17 +14,17 @@ void test_output(int i, int excepted, char* message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
game game_load(char* filename) {
|
game game_load(char* filename) {
|
||||||
checkPointer((void*)filename);
|
checkPointer((void*)filename, "NULL pointer filename on game_load\n");
|
||||||
// ouvrons le fichier
|
// ouvrons le fichier
|
||||||
FILE* f = fopen(filename, "r");
|
FILE* f = fopen(filename, "r");
|
||||||
checkPointer((void*)f);
|
checkPointer((void*)f, "Couldn't open file\n");
|
||||||
// récuperer les valeurs dont on a beson pour créer le jeu initail
|
// récuperer les valeurs dont on a beson pour créer le jeu initail
|
||||||
uint nb_rows, nb_cols;
|
uint nb_rows, nb_cols;
|
||||||
int wrapping;
|
int wrapping;
|
||||||
test_output(fscanf(f, "%u %u %d\n", &nb_rows, &nb_cols, &wrapping), 3, "Error in scan of file");
|
test_output(fscanf(f, "%u %u %d\n", &nb_rows, &nb_cols, &wrapping), 3, "Error in scan of file");
|
||||||
// créer le jeu initail
|
// créer le jeu initail
|
||||||
game g1 = game_new_empty_ext(nb_rows, nb_cols, wrapping);
|
game g1 = game_new_empty_ext(nb_rows, nb_cols, wrapping);
|
||||||
checkPointer((void*)g1);
|
checkPointer((void*)g1, "Couldn't create game\n");
|
||||||
// récuperer les coups à jouer
|
// récuperer les coups à jouer
|
||||||
for (uint i = 0; i < nb_rows; i++) {
|
for (uint i = 0; i < nb_rows; i++) {
|
||||||
for (uint j = 0; j < nb_cols; j++) {
|
for (uint j = 0; j < nb_cols; j++) {
|
||||||
@@ -77,11 +77,11 @@ char get_char_from_state(cgame g, uint i, uint j) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void game_save(cgame g, char* filename) {
|
void game_save(cgame g, char* filename) {
|
||||||
checkPointer((void*)filename);
|
checkPointer((void*)filename, "NULL pointer filename on game_save\n");
|
||||||
checkPointer((void*)g);
|
checkPointer((void*)g, "NULL pointer game g on game_save\n");
|
||||||
// try create or open file named filename
|
// try create or open file named filename
|
||||||
FILE* f = fopen(filename, "w");
|
FILE* f = fopen(filename, "w");
|
||||||
checkPointer((void*)f);
|
checkPointer((void*)f, "Couldn't open file in game_save\n");
|
||||||
// try to print the first line (game_nb_rows(g) / 10 + 1 = nb char of a number)
|
// try to print the first line (game_nb_rows(g) / 10 + 1 = nb char of a number)
|
||||||
test_output(fprintf(f, "%d %d %d\n", game_nb_rows(g), game_nb_cols(g), game_is_wrapping(g)),
|
test_output(fprintf(f, "%d %d %d\n", game_nb_rows(g), game_nb_cols(g), game_is_wrapping(g)),
|
||||||
NB_CHAR_HEADER_WITHTOUT_DIMENSIONS + game_nb_rows(g) / 10 + 1 + game_nb_cols(g) / 10 + 1, "Couldn't print in file.\n");
|
NB_CHAR_HEADER_WITHTOUT_DIMENSIONS + game_nb_rows(g) / 10 + 1 + game_nb_cols(g) / 10 + 1, "Couldn't print in file.\n");
|
||||||
|
|||||||
Reference in new issue
Block a user