This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
575376a
commit bc63644
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <stdbool.h> | ||
|
||
typedef struct | ||
{ | ||
size_t bitCount; | ||
uint8_t *array; | ||
} BitArray; | ||
|
||
// bit count must be divisible by 8 for this to work | ||
BitArray bitArrayCreate(size_t bitCount); | ||
void bitArrayDelete(BitArray *array); | ||
|
||
void bitArraySet(BitArray *array, size_t index); | ||
void bitArrayClear(BitArray *array, size_t index); | ||
bool bitArrayGet(BitArray *array, size_t index); | ||
void bitArrayWrite(BitArray array, const char *file); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "bitarray.h" | ||
#include <nds/system.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
BitArray bitArrayCreate(size_t bitCount) | ||
{ | ||
BitArray array; | ||
array.array = (uint8_t *)calloc(bitCount, 1); | ||
array.bitCount = bitCount; | ||
return array; | ||
} | ||
|
||
void bitArrayDelete(BitArray *array) | ||
{ | ||
free(array->array); | ||
} | ||
|
||
void bitArraySet(BitArray *array, size_t index) | ||
{ | ||
array->array[index / 8] |= (1 << (index % 8)); | ||
} | ||
|
||
void bitArrayClear(BitArray *array, size_t index) | ||
{ | ||
array->array[index / 8] &= ~(1 << (index % 8)); | ||
} | ||
|
||
bool bitArrayGet(BitArray *array, size_t index) | ||
{ | ||
if (array->array[index / 8] & (1 << index % 8)) | ||
return true; | ||
return false; | ||
} | ||
|
||
void bitArrayWrite(BitArray array, const char *file) | ||
{ | ||
FILE *fp = fopen(file, "w"); | ||
if (!fp) | ||
{ | ||
FILE *fpError = fopen("error.log", "w"); | ||
if (fpError) | ||
{ | ||
// write error into an error file | ||
fprintf(fpError, "Failed to open file %s for writing bit array", file); | ||
fclose(fpError); | ||
} | ||
systemShutDown(); | ||
} | ||
|
||
for (size_t i = 0; i < array.bitCount; ++i) | ||
fputc((bitArrayGet(&array, i)) ? '1' : '0', fp); | ||
fclose(fp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters