Survival Royale 1.0.0
A very simple yet funny card game.
Loading...
Searching...
No Matches
utility.c
Go to the documentation of this file.
1// Copyright (C) 2025 Giulio Salvi, Jacopo Paradisi
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
20
21#include "utility.h"
22
23char* substring(const char* string, int startPosition, int length) {
24 if (startPosition < 0 || length < 0 || startPosition + length > strlen(string)) {
25 return NULL;
26 }
27
28 char* sub = (char*)malloc(length + 1);
29 if(sub == NULL)
31
32 strncpy(sub, string + startPosition, length);
33 sub[length] = '\0';
34
35 return sub;
36}
37
38int offsetFromNext(const char* string, char character, int startPosition) {
39 int strLen = strlen(string);
40 if(startPosition < 0 || startPosition >= strLen)
41 return -1;
42
43 for(int i = startPosition; i < strLen; i++)
44 if(string[i] == character)
45 return i - startPosition;
46
47 return -1;
48}
49
50int count(const char* string, char character, int startPosition) {
51 int strLen = strlen(string);
52 if(startPosition < 0 || startPosition >= strLen)
53 return 0;
54
55 int counter = 0;
56 for(int i = startPosition; i < strLen; i++)
57 if(string[i] == character)
58 counter++;
59
60 return counter;
61}
62
63bool containsFrom(const char* string, char character, int startPosition) {
64 int strLen = strlen(string);
65 if(startPosition < 0 || startPosition >= strLen)
66 return false;
67
68 for(int i = startPosition; i < strLen; i++)
69 if(string[i] == character)
70 return true;
71
72 return false;
73}
74
75bool containsSubstringFrom(const char* string, const char* subString, int startPosition) {
76 if(strcmp(string, subString) == 0)
77 return true;
78
79 int strLen = strlen(string);
80 int subStrLen = strlen(subString);
81
82 if(startPosition < 0 || startPosition >= strLen || subStrLen == 0 || subStrLen > strLen - startPosition)
83 return false;
84
85 for(int i = startPosition; i < strLen; i++)
86 if(string[i] == subString[0] && i + subStrLen < strLen)
87 for(int j = 0; j < subStrLen; j++) {
88 if(string[i + j] != subString[j])
89 break;
90 else if(j == subStrLen - 1)
91 return true;
92 }
93
94 return false;
95}
96
97bool isDecimalDigit(char character) {
98 return character >= '0' && character <= '9';
99}
100
101bool isHexDigit(char character) {
102 return isDecimalDigit(character) || (character >= 'a' && character <= 'f') || (character >= 'A' && character <= 'F');
103}
104
105int decimalDigitToInt(char digit) {
106 return (int)(isDecimalDigit(digit) ? digit - '0' : digit);
107}
108
109int hexDigitToInt(char digit) {
110 return isDecimalDigit(digit) ?
111 decimalDigitToInt(digit) :
112 (
113 isHexDigit(digit) ?
114 (
115 digit >= 'a' ?
116 (int)(digit - 'a') + 10 :
117 (int)(digit - 'A') + 10
118 ) :
119 (int)digit
120 );
121}
122
123int evalutateBase(char* number) {
124 char max = -1;
125
126 for(int i = 0; i < strlen(number); i++) {
127 if(isHexDigit(number[i])) {
128 if(number[i] > max)
129 max = number[i];
130 } else
131 return -1;
132 }
133
134 return hexDigitToInt(max) + 1;
135
136}
137
138int nDigits(int number) {
139 return number == 0 ? 1 : (int)log10(abs(number)) + 1;
140}
#define EXIT_ALLOC_FAILURE
Exit code when dynamic memory allocation fails.
Definition consts.h:22
bool isHexDigit(char character)
Checks if a char is a hexadecimal digit (not case sensitive).
Definition utility.c:101
bool containsFrom(const char *string, char character, int startPosition)
Checks if there is an occoruncy of the passed char from the passed position in the passed string.
Definition utility.c:63
int decimalDigitToInt(char digit)
If is a decimal digit, converts it to int; Otherwise returns ASCII integer representation of the pass...
Definition utility.c:105
int count(const char *string, char character, int startPosition)
Counts the number of occoruncies in the string, from a passed position, of the passed char.
Definition utility.c:50
int evalutateBase(char *number)
Evalutates the base of the number (by finding the biggest digit up to hexadecimal digit F) and return...
Definition utility.c:123
bool isDecimalDigit(char character)
Checks if a char is a decimal digit.
Definition utility.c:97
int offsetFromNext(const char *string, char character, int startPosition)
Counts the offset in the string between the position and the next occoruncy of the passed char.
Definition utility.c:38
int nDigits(int number)
Evalutates the number of digits of a number. The time complexity of this function is constant (O(1)).
Definition utility.c:138
char * substring(const char *string, int startPosition, int length)
Copies a substring (heap-allocated) from the passed string. If fails when allocating memory for the s...
Definition utility.c:23
int hexDigitToInt(char digit)
If is a hexadecimal digit (not case sensitive), converts it to int; Otherwise returns ASCII integer r...
Definition utility.c:109
bool containsSubstringFrom(const char *string, const char *subString, int startPosition)
Checks if there is an occoruncy of the passed substring from the passed position in the passed string...
Definition utility.c:75
Definition of utility functions.