Browse Source

Make .c files really just DROP-IN; add a few new FS functions

master
parent
commit
7d7b644860
  1. 4
      src/bits/bits.c
  2. 1
      src/crypt/xorcipher.c
  3. 10
      src/datastruct/cvec.c
  4. 4
      src/endian/endian.c
  5. 55
      src/fs/fs.c
  6. 17
      src/fs/fs.h
  7. 35
      src/img/ppm.c
  8. 6
      src/img/ppm.h
  9. 8
      src/math/misc.c
  10. 153
      src/math/vector.c
  11. 4
      src/math/vector.h
  12. 2
      src/rng/rng.c
  13. 14
      src/strings/levenshtein.c
  14. 12
      src/strings/levenshtein.h
  15. 15
      testing/test.c

4
src/bits/bits.c

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,7 +10,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "bits.h"
#include <stdint.h>
// Returns 1 if position bit in byte is 1, 0 if 0; -1 is returned in case position is not in range [1..8].
// Position is counted from the least significant bit, ie: in 10010010 position 1 is "0", but position 8 is "1"

1
src/crypt/xorcipher.c

@ -10,7 +10,6 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "xorcipher.h"
#include <stdio.h>
// XOR character with a key

10
src/datastruct/cvec.c

@ -10,8 +10,16 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "cvec.h"
#include <string.h>
#include <stdlib.h>
// Basic vector structure
typedef struct cvec {
size_t cap;
size_t size;
size_t data_size;
char* contents;
} cvec;
// Create new vector with the maximum data elements of cap where data is data_size bytes long
cvec cvec_new(size_t data_size, size_t cap) {

4
src/endian/endian.c

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,7 +10,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "endian.h"
#include <stdint.h>
// Determines this machine's endianness. Returns 0 in case it is of Big-Endian type, 1 - Little-Endian
int endianness() {

55
src/fs/fs.c

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,7 +10,19 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "fs.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#if __WIN32__
#include<sys/types.h>
#include<sys/stat.h>
#define stat _stat
#else
#include <sys/stat.h>
#endif
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path) {
@ -23,7 +35,27 @@ uint64_t file_size(char* path) {
return (uint64_t) file_stats.st_size;
}
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns -1 in case of an error and 0 if the operation was sucessfull
// Get file size by actually reading the whole file byte-by-byte until it hits EOF. Returns UINT64_MAX on open error
uint64_t file_size_brute(char* path) {
uint64_t size = 0;
int c = 0;
FILE* file = NULL;
file = fopen(path, "rb");
if (!file) {
return UINT64_MAX;
}
while((c = getc(file)) != EOF) {
size++;
}
fclose(file);
return size;
}
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns 0 in case of an error and 1 if the operation was sucessfull
int copy_file(char* path_src, char* path_dst) {
FILE* src_file;
FILE* dst_file;
@ -33,12 +65,12 @@ int copy_file(char* path_src, char* path_dst) {
src_file = fopen(path_src, "rb");
if (!src_file) {
return -1;
return 0;
}
dst_file = fopen(path_dst, "wb");
if (!dst_file) {
return -1;
return 0;
}
while (!feof(src_file)) {
@ -49,5 +81,18 @@ int copy_file(char* path_src, char* path_dst) {
fclose(src_file);
fclose(dst_file);
return 1;
}
// Check whether a file can be opened in read mode. Returns 0 in case of an error, 1 otherwise (successfully opened file)
int can_open_file(char* path) {
FILE* file = NULL;
file = fopen(path, "r");
if (!file) {
return 0;
}
fclose(file);
return 1;
}

17
src/fs/fs.h

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,17 +10,16 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/stat.h>
#if __WIN32__
#define stat _stat64
#endif
// Get file size. returns 18446744073709551615 (uint64_t maximum value) if something went wrong
uint64_t file_size(char* path);
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns -1 in case of an error and 0 if the operation was sucessfull
// Get file size by actually reading the whole file byte-by-byte until it hits EOF. Returns UINT64_MAX on open error
uint64_t file_size_brute(char* path);
// Copy file at path_src to path_dst. Does not create nonexistent directories. Returns 0 in case of an error and 1 if the operation was sucessfull
int copy_file(char* path_src, char* path_dst);
// Check whether a file can be opened in read mode. Returns 0 in case of an error, 1 otherwise (successfully opened file)
int can_open_file(char* path);

35
src/img/ppm.c

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,7 +10,26 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ppm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
// 8bit Red-Green-Blue color representation
typedef struct rgb8 {
uint8_t r;
uint8_t g;
uint8_t b;
} rgb8;
// Minimal ppm image structure
typedef struct ppm_image {
unsigned int width;
unsigned int height;
rgb8* pixels;
} ppm_image;
// Read ppm image from file on the disk. Returns NULL if something went wrong
ppm_image* read_ppm(const char* path) {
@ -83,13 +102,13 @@ ppm_image* read_ppm(const char* path) {
return ppm;
}
// Write ppm image to the disk. Returns 0 if everything is alright, 1 otherwise
// Write ppm image to the disk. Returns 1 if everything is alright, 0 otherwise
int write_ppm(const ppm_image* ppm, const char* path) {
FILE* file;
file = fopen(path, "w");
if (!file) {
return 1;
return 0;
}
fprintf(file, "P6\n");
@ -98,21 +117,21 @@ int write_ppm(const ppm_image* ppm, const char* path) {
fclose(file);
return 0;
return 1;
}
// Put pixel with specified rgb8 color at x,y coordinates. Returns 0 if pixel has been replaced, 1 if
// Put pixel with specified rgb8 color at x,y coordinates. Returns 1 if pixel has been replaced, 0 if
// coordinates are out of bounds
int put_pixel_at(const unsigned int x, const unsigned int y, const rgb8 color, ppm_image* ppm) {
unsigned int index = ppm->width * y + x;
if (index >= ppm->width*ppm->height) {
// out of bounds !
return 1;
return 0;
}
ppm->pixels[index] = color;
return 0;
return 1;
}
// Get pixel color at specified coordinates. Returns a const pointer to that pixel color if

6
src/img/ppm.h

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -34,10 +34,10 @@ typedef struct ppm_image {
// Read ppm image from file on the disk. Returns NULL if something went wrong
ppm_image* read_ppm(const char* path);
// Write ppm image to the disk. Returns 0 if everything is alright, 1 otherwise
// Write ppm image to the disk. Returns 1 if everything is alright, 0 otherwise
int write_ppm(const ppm_image* ppm, const char* path);
// Put pixel with specified rgb8 color at x,y coordinates. Returns 0 if pixel has been replaced, 1 if
// Put pixel with specified rgb8 color at x,y coordinates. Returns 1 if pixel has been replaced, 0 if
// coordinates are out of bounds
int put_pixel_at(const unsigned int x, const unsigned int y, const rgb8 color, ppm_image* ppm);

8
src/math/misc.c

@ -11,8 +11,6 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
*/
// Returns an absolute value of n
#include "misc.h"
char absch(char n) {
if (n >= 0) {
return n;
@ -84,6 +82,12 @@ long double absld(long double n) {
}
}
// Returns the approximate result of base^exp
float powerf(float base, float exp);
// Returns the approximate result of base^exp
long double powerl(long double base, long double exp);
long double n_root(long double a, unsigned int n, unsigned int max_iter, long double epsilon) {
if (n == 1 || a == 0) {
return a;

153
src/math/vector.c

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,7 +10,156 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "vector.h"
/* Static math definitions */
// Returns an absolute value of n
static long double absld(long double n) {
if (n >= 0.0f) {
return n;
} else {
return -n;
}
}
// Returns the approximate result of base^exp
static long double powerl(long double base, long double exp);
static long double n_root(long double a, unsigned int n, unsigned int max_iter, long double epsilon) {
if (n == 1 || a == 0) {
return a;
}
float prev_val = 1;
float val = 0;
for (unsigned int i = 0; i < max_iter; i++) {
val = (1.0 / n) * ((n - 1) * prev_val + a / powerl(prev_val, n-1));
if (absld(prev_val - val) < epsilon) {
return val;
}
prev_val = val;
}
return val;
}
// Returns the approximate result of base^exp
static long double powerl(long double base, long double exp) {
// all zeroes|base is zero, but exp is not
if ((base == 0.0f && exp == 0.0f) || (base == 0.0f && exp != 0.0f)) {
return 0.0f;
}
// exp is 0
if (exp == 0.0f) {
return 1.0f;
}
// exp is negative
if (exp < 0.0f) {
return 1.0 / powerl(base, -exp);
}
// exp is in (0.0; 1.0)
if (exp > 0.0f && exp < 1.0f) {
return n_root(base, 1.0 / exp, 500, 0.0000000001f);
}
// exp is even
if ((int) exp % 2 == 0) {
float hpow = powerl(base, exp / 2.0f);
return hpow * hpow;
}
// exp is integer
return base * powerl(base, exp-1.0);
}
// Returns the approximate result of square root of n
static float sqrootl(long double n) {
return powerl(n, 0.5);
}
/* End of math definitions */
typedef struct vec2 {
long long int x;
long long int y;
} vec2;
typedef struct vec2f {
long double x;
long double y;
} vec2f;
// Calculate vector's length
long double vec2_len(vec2 vec);
// Calculate vector's length
long double vec2f_len(vec2f vec);
// Multiply 2 vectors the scalar way
long long int vec2_multiply_scalar(vec2 a, vec2 b);
// Multiply 2 vectors the scalar way
long double vec2f_multiply_scalar(vec2f a, vec2f b);
// Add 2 vectors together
vec2 vec2_add(vec2 a, vec2 b);
// Add 2 vectors together
vec2f vec2f_add(vec2f a, vec2f b);
// Get cosinus of angle between 2 given vectors
long double vec2_angle(vec2 a, vec2 b);
// Get cosinus of angle between 2 given vectors
long double vec2f_angle(vec2f a, vec2f b);
typedef struct vec3 {
long long int x;
long long int y;
long long int z;
} vec3;
typedef struct vec3f {
long double x;
long double y;
long double z;
} vec3f;
// Calculate vector's length
long double vec3_len(vec3 vec);
// Calculate vector's length
long double vec3f_len(vec3f vec);
// Add 2 vectors together
vec3 vec3_add(vec3 a, vec3 b);
// Add 2 vectors together
vec3f vec3f_add(vec3f a, vec3f b);
// Multiply 2 vectors the scalar way
long long int vec3_multiply_scalar(vec3 a, vec3 b);
// Multiply 2 vectors the scalar way
long double vec3f_multiply_scalar(vec3f a, vec3f b);
// Get cosinus of angle between 2 given vectors
long double vec3_angle(vec3 a, vec3 b);
// Get cosinus of angle between 2 given vectors
long double vec3f_angle(vec3f a, vec3f b);
// Get resulting vector of vector multiplication of 2 given vectors
vec3 vec3_multiply_vector(vec3 a, vec3 b);
// Get resulting vector of vector multiplication of 2 given vectors
vec3f vec3f_multiply_vector(vec3f a, vec3f b);
// Calculate vector's length
long double vec2_len(vec2 vec) {

4
src/math/vector.h

@ -1,7 +1,7 @@
/*
The MIT License (MIT)
Copyright © 2022 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Copyright © 2022,2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
@ -10,8 +10,6 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "misc.h"
typedef struct vec2 {
long long int x;
long long int y;

2
src/rng/rng.c

@ -10,7 +10,7 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "rng.h"
#include <stdint.h>
// Implementation of BBS RNG with pre-defined variables
int64_t bbs(int64_t seed) {

14
src/strings/levenshtein.c

@ -1,4 +1,16 @@
#include "levenshtein.h"
/*
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
int _min(int a, int b) {
if (a < b) {

12
src/strings/levenshtein.h

@ -1,4 +1,14 @@
#include <string.h>
/*
The MIT License (MIT)
Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Calculates Levenshtein distance for str1 and str2
int levenshtein_distance(const char* str1, const char* str2);

15
testing/test.c

@ -44,7 +44,7 @@ int test_ppm() {
return EXIT_FAILURE;
}
if (write_ppm(ppm, "result_image.ppm") != 0) {
if (!write_ppm(ppm, "result_image.ppm")) {
printf("[ERROR] failed to write new ppm image\n");
return EXIT_FAILURE;
}
@ -59,7 +59,7 @@ int test_ppm() {
}
}
if (write_ppm(new_ppm, "result_image.ppm") != 0) {
if (!write_ppm(new_ppm, "result_image.ppm")) {
printf("[ERROR] failed to write new changed ppm image\n");
return EXIT_FAILURE;
}
@ -130,12 +130,23 @@ int test_fs() {
printf("[ERROR] Failed to determine file size of test ppm image\n");
return EXIT_FAILURE;
}
uint64_t fsize_stat = file_size("test_img512x512.ppm");
uint64_t fsize_brute = file_size_brute("test_img512x512.ppm");
if (fsize_brute != fsize_stat) {
printf("[ERROR] File size functions returned different sizes on the same file: (brute - stat) (%ld - %ld)\n", fsize_brute, fsize_stat);
return EXIT_FAILURE;
}
if (copy_file("test_img512x512.ppm", "copied_ppm_file.ppm") == -1) {
printf("[ERROR] Failed to copy test ppm image\n");
return EXIT_FAILURE;
}
if (!can_open_file("test_img512x512.ppm")) {
printf("[ERROR] Failed to open test_img512x512.ppm, which must exist\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Loading…
Cancel
Save