WARNING: BREAKING CHANGE
Added a bunch of useful text management functions. Consequently, some already available functions like `FormatText()` and `SubText()` has been renamed for consistency. Created temporal fallbacks for old names. raylib version bumped to 2.3.
This commit is contained in:
parent
9a8320c52b
commit
01338b0a14
9 changed files with 288 additions and 61 deletions
|
@ -2,7 +2,7 @@
|
||||||
*
|
*
|
||||||
* raylib [shapes] example - raylib logo animation
|
* raylib [shapes] example - raylib logo animation
|
||||||
*
|
*
|
||||||
* This example has been created using raylib 1.4 (www.raylib.com)
|
* This example has been created using raylib 2.3 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
* Copyright (c) 2014 Ramon Santamaria (@raysan5)
|
||||||
|
@ -140,7 +140,7 @@ int main()
|
||||||
|
|
||||||
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
||||||
|
|
||||||
DrawText(SubText("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
|
DrawText(TextSubtext("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
|
||||||
}
|
}
|
||||||
else if (state == 4)
|
else if (state == 4)
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
*
|
*
|
||||||
* raylib [text] example - Text Writing Animation
|
* raylib [text] example - Text Writing Animation
|
||||||
*
|
*
|
||||||
* This example has been created using raylib 1.4 (www.raylib.com)
|
* This example has been created using raylib 2.3 (www.raylib.com)
|
||||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||||
*
|
*
|
||||||
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
|
* Copyright (c) 2016 Ramon Santamaria (@raysan5)
|
||||||
|
@ -44,7 +44,7 @@ int main()
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
DrawText(SubText(message, 0, framesCounter/10), 210, 160, 20, MAROON);
|
DrawText(TextSubtext(message, 0, framesCounter/10), 210, 160, 20, MAROON);
|
||||||
|
|
||||||
DrawText("PRESS [ENTER] to RESTART!", 240, 260, 20, LIGHTGRAY);
|
DrawText("PRESS [ENTER] to RESTART!", 240, 260, 20, LIGHTGRAY);
|
||||||
DrawText("PRESS [SPACE] to SPEED UP!", 239, 300, 20, LIGHTGRAY);
|
DrawText("PRESS [SPACE] to SPEED UP!", 239, 300, 20, LIGHTGRAY);
|
||||||
|
|
|
@ -219,10 +219,10 @@ void InitGameplayScreen(void)
|
||||||
{
|
{
|
||||||
foundWord = false;
|
foundWord = false;
|
||||||
|
|
||||||
messageWords[currentWord - 1].rec.width = (int)MeasureTextEx(fontMessage, SubText(missions[currentMission].msg, wordInitPosX, (i - wordInitPosX)), 30, 0).x;
|
messageWords[currentWord - 1].rec.width = (int)MeasureTextEx(fontMessage, TextSubtext(missions[currentMission].msg, wordInitPosX, (i - wordInitPosX)), 30, 0).x;
|
||||||
messageWords[currentWord - 1].rec.height = fontMessage.baseSize;
|
messageWords[currentWord - 1].rec.height = fontMessage.baseSize;
|
||||||
|
|
||||||
strncpy(messageWords[currentWord - 1].text, SubText(missions[currentMission].msg, wordInitPosX, (i - wordInitPosX)), i - wordInitPosX);
|
strncpy(messageWords[currentWord - 1].text, TextSubtext(missions[currentMission].msg, wordInitPosX, (i - wordInitPosX)), i - wordInitPosX);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c == '@') // One word to change
|
if (c == '@') // One word to change
|
||||||
|
@ -230,7 +230,7 @@ void InitGameplayScreen(void)
|
||||||
foundWord = true;
|
foundWord = true;
|
||||||
missions[currentMission].msg[i] = ' ';
|
missions[currentMission].msg[i] = ' ';
|
||||||
|
|
||||||
offsetX = (int)MeasureTextEx(fontMessage, SubText(missions[currentMission].msg, wordInitPosY, (i + 1) - wordInitPosY), 30, 0).x;
|
offsetX = (int)MeasureTextEx(fontMessage, TextSubtext(missions[currentMission].msg, wordInitPosY, (i + 1) - wordInitPosY), 30, 0).x;
|
||||||
|
|
||||||
messageWords[currentWord].rec.x = offsetX;
|
messageWords[currentWord].rec.x = offsetX;
|
||||||
messageWords[currentWord].rec.y = offsetY;
|
messageWords[currentWord].rec.y = offsetY;
|
||||||
|
|
|
@ -205,7 +205,7 @@ void DrawMissionScreen(void)
|
||||||
DrawTexturePro(texBackline, sourceRecBackLine, destRecBackLine, (Vector2){0,0},0, Fade(WHITE, fadeBackLine));
|
DrawTexturePro(texBackline, sourceRecBackLine, destRecBackLine, (Vector2){0,0},0, Fade(WHITE, fadeBackLine));
|
||||||
|
|
||||||
if (writeNumber) DrawTextEx(fontMission, FormatText("Filtración #%02i ", currentMission + 1), numberPosition, missionSize + 10, 0, numberColor);
|
if (writeNumber) DrawTextEx(fontMission, FormatText("Filtración #%02i ", currentMission + 1), numberPosition, missionSize + 10, 0, numberColor);
|
||||||
DrawTextEx(fontMission, SubText(missions[currentMission].brief, 0, missionLenght), missionPosition, missionSize, 0, missionColor);
|
DrawTextEx(fontMission, TextSubtext(missions[currentMission].brief, 0, missionLenght), missionPosition, missionSize, 0, missionColor);
|
||||||
if (writeKeyword && blinkKeyWord) DrawTextEx(fontMission, FormatText("Keyword: %s", missions[currentMission].key), keywordPosition, missionSize + 10, 0, keywordColor);
|
if (writeKeyword && blinkKeyWord) DrawTextEx(fontMission, FormatText("Keyword: %s", missions[currentMission].key), keywordPosition, missionSize + 10, 0, keywordColor);
|
||||||
|
|
||||||
if (showButton)
|
if (showButton)
|
||||||
|
|
|
@ -137,8 +137,8 @@ void UpdateTitleScreen(void)
|
||||||
void DrawTitleScreen(void)
|
void DrawTitleScreen(void)
|
||||||
{
|
{
|
||||||
DrawTexture(texBackground, 0,0, WHITE);
|
DrawTexture(texBackground, 0,0, WHITE);
|
||||||
DrawTextEx(fontTitle, SubText(textTitle, 0, transmissionLenght), transmissionPosition, titleSize, 0, titleColor);
|
DrawTextEx(fontTitle, TextSubtext(textTitle, 0, transmissionLenght), transmissionPosition, titleSize, 0, titleColor);
|
||||||
DrawTextEx(fontTitle, SubText(textTitle, 12, missionLenght), missionPositon, titleSize, 0, titleColor);
|
DrawTextEx(fontTitle, TextSubtext(textTitle, 12, missionLenght), missionPositon, titleSize, 0, titleColor);
|
||||||
|
|
||||||
DrawButton("start");
|
DrawButton("start");
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
*
|
*
|
||||||
**********************************************************************************************/
|
**********************************************************************************************/
|
||||||
|
|
||||||
#define RAYLIB_VERSION "2.2-dev"
|
#define RAYLIB_VERSION "2.3-dev"
|
||||||
|
|
||||||
// Edit to control what features Makefile'd raylib is compiled with
|
// Edit to control what features Makefile'd raylib is compiled with
|
||||||
#if defined(RAYLIB_CMAKE)
|
#if defined(RAYLIB_CMAKE)
|
||||||
|
|
|
@ -3176,17 +3176,17 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i
|
||||||
|
|
||||||
// NOTE: delay represents the time between frames in the gif, if we capture a gif frame every
|
// NOTE: delay represents the time between frames in the gif, if we capture a gif frame every
|
||||||
// 10 game frames and each frame trakes 16.6ms (60fps), delay between gif frames should be ~16.6*10.
|
// 10 game frames and each frame trakes 16.6ms (60fps), delay between gif frames should be ~16.6*10.
|
||||||
GifBegin(FormatText("screenrec%03i.gif", screenshotCounter), screenWidth, screenHeight, (int)(GetFrameTime()*10.0f), 8, false);
|
GifBegin(TextFormat("screenrec%03i.gif", screenshotCounter), screenWidth, screenHeight, (int)(GetFrameTime()*10.0f), 8, false);
|
||||||
screenshotCounter++;
|
screenshotCounter++;
|
||||||
|
|
||||||
TraceLog(LOG_INFO, "Begin animated GIF recording: %s", FormatText("screenrec%03i.gif", screenshotCounter));
|
TraceLog(LOG_INFO, "Begin animated GIF recording: %s", TextFormat("screenrec%03i.gif", screenshotCounter));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
#endif // SUPPORT_GIF_RECORDING
|
#endif // SUPPORT_GIF_RECORDING
|
||||||
#if defined(SUPPORT_SCREEN_CAPTURE)
|
#if defined(SUPPORT_SCREEN_CAPTURE)
|
||||||
{
|
{
|
||||||
TakeScreenshot(FormatText("screenshot%03i.png", screenshotCounter));
|
TakeScreenshot(TextFormat("screenshot%03i.png", screenshotCounter));
|
||||||
screenshotCounter++;
|
screenshotCounter++;
|
||||||
}
|
}
|
||||||
#endif // SUPPORT_SCREEN_CAPTURE
|
#endif // SUPPORT_SCREEN_CAPTURE
|
||||||
|
@ -4454,7 +4454,7 @@ static void LogoAnimation(void)
|
||||||
|
|
||||||
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
DrawRectangle(screenWidth/2 - 112, screenHeight/2 - 112, 224, 224, Fade(RAYWHITE, alpha));
|
||||||
|
|
||||||
DrawText(SubText("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
|
DrawText(TextSubtext("raylib", 0, lettersCount), screenWidth/2 - 44, screenHeight/2 + 48, 50, Fade(BLACK, alpha));
|
||||||
}
|
}
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
|
26
src/raylib.h
26
src/raylib.h
|
@ -135,6 +135,11 @@
|
||||||
#define MAGENTA CLITERAL{ 255, 0, 255, 255 } // Magenta
|
#define MAGENTA CLITERAL{ 255, 0, 255, 255 } // Magenta
|
||||||
#define RAYWHITE CLITERAL{ 245, 245, 245, 255 } // My own White (raylib logo)
|
#define RAYWHITE CLITERAL{ 245, 245, 245, 255 } // My own White (raylib logo)
|
||||||
|
|
||||||
|
// Temporal hack to avoid breaking old codebases using
|
||||||
|
// deprecated raylib implementation of these functions
|
||||||
|
#define FormatText TextFormat
|
||||||
|
#define SubText TextSubText
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Structures Definition
|
// Structures Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
@ -1111,11 +1116,22 @@ RLAPI int MeasureText(const char *text, int fontSize);
|
||||||
RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font
|
RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font
|
||||||
RLAPI int GetGlyphIndex(Font font, int character); // Get index position for a unicode character on font
|
RLAPI int GetGlyphIndex(Font font, int character); // Get index position for a unicode character on font
|
||||||
|
|
||||||
// Text string edition functions
|
// Text strings management functions
|
||||||
RLAPI const char *FormatText(const char *text, ...); // Formatting of text with variables to 'embed'
|
// NOTE: Some strings allocate memory internally for returned strings, just be careful!
|
||||||
RLAPI const char *SubText(const char *text, int position, int length); // Get a piece of a text string
|
RLAPI bool TextIsEqual(const char *text1, const char *text2); // Check if two text string are equal
|
||||||
RLAPI char **SplitText(char *text, char delimiter, int *strCount); // Split text string into multiple strings (memory should be freed manually!)
|
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending
|
||||||
RLAPI bool IsEqualText(const char *text1, const char *text2); // Check if two text string are equal
|
RLAPI const char *TextFormat(const char *text, ...); // Text formatting with variables (sprintf)
|
||||||
|
RLAPI const char *TextSubtext(const char *text, int position, int length); // Get a piece of a text string
|
||||||
|
RLAPI const char *TextReplace(char *text, const char *replace, const char *by); // Replace text string (memory should be freed!)
|
||||||
|
RLAPI const char *TextInsert(const char *text, const char *insert, int position); // Insert text in a position (memory should be freed!)
|
||||||
|
RLAPI const char *TextJoin(const char **textList, int count, const char *delimiter); // Join text strings with delimiter
|
||||||
|
RLAPI char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings (memory should be freed!)
|
||||||
|
RLAPI void TextSplitEx(const char *text, char delimiter, int *count, const char **ptrs, int *lengths); // Get pointers to substrings separated by delimiter
|
||||||
|
RLAPI void TextAppend(char *text, const char *append, int *position); // Append text at specific position and move cursor!
|
||||||
|
RLAPI int TextFindIndex(const char *text, const char *find); // Find first text occurrence within a string
|
||||||
|
RLAPI const char *TextToUpper(const char *text); // Get upper case version of provided string
|
||||||
|
RLAPI const char *TextToLower(const char *text); // Get lower case version of provided string
|
||||||
|
RLAPI const char *TextToPascal(const char *text); // Get Pascal case notation version of provided string
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Basic 3d Shapes Drawing Functions (Module: models)
|
// Basic 3d Shapes Drawing Functions (Module: models)
|
||||||
|
|
293
src/text.c
293
src/text.c
|
@ -47,6 +47,7 @@
|
||||||
#include <string.h> // Required for: strlen()
|
#include <string.h> // Required for: strlen()
|
||||||
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
|
#include <stdarg.h> // Required for: va_list, va_start(), vfprintf(), va_end()
|
||||||
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets()
|
#include <stdio.h> // Required for: FILE, fopen(), fclose(), fscanf(), feof(), rewind(), fgets()
|
||||||
|
#include <ctype.h> // Required for: toupper(), tolower()
|
||||||
|
|
||||||
#include "utils.h" // Required for: fopen() Android mapping
|
#include "utils.h" // Required for: fopen() Android mapping
|
||||||
|
|
||||||
|
@ -62,8 +63,7 @@
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Defines and Macros
|
// Defines and Macros
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
#define MAX_FORMATTEXT_LENGTH 512
|
#define MAX_TEXT_BUFFER_LENGTH 1024 // Size of internal static buffers of some Text*() functions
|
||||||
#define MAX_SUBTEXT_LENGTH 512
|
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Types and Structures Definition
|
// Types and Structures Definition
|
||||||
|
@ -700,7 +700,7 @@ void DrawFPS(int posX, int posY)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
// NOTE: We have rounding errors every frame, so it oscillates a lot
|
||||||
DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME);
|
DrawText(TextFormat("%2i FPS", fps), posX, posY, 20, LIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw text (using default font)
|
// Draw text (using default font)
|
||||||
|
@ -863,10 +863,33 @@ int GetGlyphIndex(Font font, int character)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formatting of text with variables to 'embed'
|
// Text strings management functions
|
||||||
const char *FormatText(const char *text, ...)
|
//----------------------------------------------------------------------------------
|
||||||
|
// Check if two text string are equal
|
||||||
|
// REQUIRES: strcmp()
|
||||||
|
bool TextIsEqual(const char *text1, const char *text2)
|
||||||
{
|
{
|
||||||
static char buffer[MAX_FORMATTEXT_LENGTH];
|
bool result = false;
|
||||||
|
|
||||||
|
if (strcmp(text1, text2) == 0) result = true;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get text length in bytes, check for \0 character
|
||||||
|
unsigned int TextLength(const char *text)
|
||||||
|
{
|
||||||
|
unsigned int length = 0;
|
||||||
|
|
||||||
|
while (*text++) length++;
|
||||||
|
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Formatting of text with variables to 'embed'
|
||||||
|
const char *TextFormat(const char *text, ...)
|
||||||
|
{
|
||||||
|
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, text);
|
va_start(args, text);
|
||||||
|
@ -877,9 +900,11 @@ const char *FormatText(const char *text, ...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a piece of a text string
|
// Get a piece of a text string
|
||||||
const char *SubText(const char *text, int position, int length)
|
// REQUIRES: strlen()
|
||||||
|
const char *TextSubtext(const char *text, int position, int length)
|
||||||
{
|
{
|
||||||
static char buffer[MAX_SUBTEXT_LENGTH] = { 0 };
|
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
||||||
|
|
||||||
int textLength = strlen(text);
|
int textLength = strlen(text);
|
||||||
|
|
||||||
if (position >= textLength)
|
if (position >= textLength)
|
||||||
|
@ -901,52 +926,238 @@ const char *SubText(const char *text, int position, int length)
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split string into multiple strings
|
// Replace text string
|
||||||
// NOTE: Files count is returned by parameters pointer
|
// REQUIRES: strlen(), strstr(), strncpy(), strcpy()
|
||||||
// NOTE: Allocated memory should be manually freed
|
// WARNING: Internally allocated memory must be freed by the user (if return != NULL)
|
||||||
char **SplitText(char *text, char delimiter, int *strCount)
|
const char *TextReplace(char *text, const char *replace, const char *by)
|
||||||
{
|
{
|
||||||
#define MAX_SUBSTRING_LENGTH 128
|
char *result;
|
||||||
|
|
||||||
char **strings = NULL;
|
char *insertPoint; // Next insert point
|
||||||
int len = strlen(text);
|
char *temp; // Temp pointer
|
||||||
char *strDup = (char *)malloc(len + 1);
|
int replaceLen; // Replace string length of (the string to remove)
|
||||||
strcpy(strDup, text);
|
int byLen; // Replacement length (the string to replace replace by)
|
||||||
int counter = 1;
|
int lastReplacePos; // Distance between replace and end of last replace
|
||||||
|
int count; // Number of replacements
|
||||||
|
|
||||||
// Count how many substrings we have on string
|
// Sanity checks and initialization
|
||||||
for (int i = 0; i < len; i++) if (text[i] == delimiter) counter++;
|
if (!text || !replace) return NULL;
|
||||||
|
|
||||||
// Memory allocation for substrings
|
replaceLen = strlen(replace);
|
||||||
strings = (char **)malloc(sizeof(char *)*counter);
|
if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
|
||||||
for (int i = 0; i < counter; i++) strings[i] = (char *)malloc(sizeof(char)*MAX_SUBSTRING_LENGTH);
|
|
||||||
|
|
||||||
char *substrPtr = NULL;
|
if (!by) by = ""; // Replace by nothing if not provided
|
||||||
char delimiters[1] = { delimiter }; // Only caring for one delimiter
|
byLen = strlen(by);
|
||||||
substrPtr = strtok(strDup, delimiters);
|
|
||||||
|
|
||||||
for (int i = 0; (i < counter) && (substrPtr != NULL); i++)
|
// Count the number of replacements needed
|
||||||
|
insertPoint = text;
|
||||||
|
for (count = 0; (temp = strstr(insertPoint, replace)); count++) insertPoint = temp + replaceLen;
|
||||||
|
|
||||||
|
// Allocate returning string and point temp to it
|
||||||
|
temp = result = malloc(strlen(text) + (byLen - replaceLen)*count + 1);
|
||||||
|
|
||||||
|
if (!result) return NULL; // Memory could not be allocated
|
||||||
|
|
||||||
|
// First time through the loop, all the variable are set correctly from here on,
|
||||||
|
// temp points to the end of the result string
|
||||||
|
// insertPoint points to the next occurrence of replace in text
|
||||||
|
// text points to the remainder of text after "end of replace"
|
||||||
|
while (count--)
|
||||||
{
|
{
|
||||||
strcpy(strings[i], substrPtr);
|
insertPoint = strstr(text, replace);
|
||||||
substrPtr = strtok(NULL, delimiters);
|
lastReplacePos = insertPoint - text;
|
||||||
|
temp = strncpy(temp, text, lastReplacePos) + lastReplacePos;
|
||||||
|
temp = strcpy(temp, by) + byLen;
|
||||||
|
text += lastReplacePos + replaceLen; // Move to next "end of replace"
|
||||||
}
|
}
|
||||||
|
|
||||||
*strCount = counter;
|
// Copy remaind text part after replacement to result (pointed by moving temp)
|
||||||
free(strDup);
|
strcpy(temp, text);
|
||||||
|
|
||||||
return strings;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if two text string are equal
|
|
||||||
bool IsEqualText(const char *text1, const char *text2)
|
|
||||||
{
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
if (strcmp(text1, text2) == 0) result = true;
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Insert text in a specific position, moves all text forward
|
||||||
|
// REQUIRES: strlen(), strcpy(), strtok()
|
||||||
|
// WARNING: Allocated memory should be manually freed
|
||||||
|
const char *TextInsert(const char *text, const char *insert, int position)
|
||||||
|
{
|
||||||
|
int textLen = strlen(text);
|
||||||
|
int insertLen = strlen(insert);
|
||||||
|
|
||||||
|
char *result = (char *)malloc(textLen + insertLen + 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < position; i++) result[i] = text[i];
|
||||||
|
for (int i = position; i < insertLen + position; i++) result[i] = insert[i];
|
||||||
|
for (int i = (insertLen + position); i < (textLen + insertLen); i++) result[i] = text[i];
|
||||||
|
|
||||||
|
result[textLen + insertLen] = '\0'; // Make sure text string is valid!
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Join text strings with delimiter
|
||||||
|
// REQUIRES: strcat()
|
||||||
|
const char *TextJoin(const char **textList, int count, const char *delimiter)
|
||||||
|
{
|
||||||
|
static char text[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
||||||
|
memset(text, 0, MAX_TEXT_BUFFER_LENGTH);
|
||||||
|
|
||||||
|
int delimiterLen = strlen(delimiter);
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
strcat(text, textList[i]);
|
||||||
|
if ((delimiterLen > 0) && (i < (count - 1))) strcat(text, delimiter);
|
||||||
|
}
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split string into multiple strings
|
||||||
|
// REQUIRES: strlen(), strcpy(), strtok()
|
||||||
|
// WARNING: Allocated memory should be manually freed
|
||||||
|
char **TextSplit(const char *text, char delimiter, int *count)
|
||||||
|
{
|
||||||
|
#define MAX_SUBSTRING_LENGTH 128
|
||||||
|
|
||||||
|
// TODO: Allocate memory properly for every substring size
|
||||||
|
|
||||||
|
char **result = NULL;
|
||||||
|
|
||||||
|
int len = strlen(text);
|
||||||
|
char *textcopy = (char *)malloc(len + 1);
|
||||||
|
strcpy(textcopy, text);
|
||||||
|
int counter = 1;
|
||||||
|
|
||||||
|
// Count how many substrings we have on text and init memory for each of them
|
||||||
|
for (int i = 0; i < len; i++) if (text[i] == delimiter) counter++;
|
||||||
|
|
||||||
|
// Memory allocation for substrings
|
||||||
|
result = (char **)malloc(sizeof(char *)*counter);
|
||||||
|
for (int i = 0; i < counter; i++) result[i] = (char *)malloc(sizeof(char)*MAX_SUBSTRING_LENGTH);
|
||||||
|
|
||||||
|
char *substrPtr = NULL;
|
||||||
|
char delimiters[1] = { delimiter }; // Only caring for one delimiter
|
||||||
|
substrPtr = strtok(textcopy, delimiters);
|
||||||
|
|
||||||
|
for (int i = 0; (i < counter) && (substrPtr != NULL); i++)
|
||||||
|
{
|
||||||
|
strcpy(result[i], substrPtr);
|
||||||
|
substrPtr = strtok(NULL, delimiters);
|
||||||
|
}
|
||||||
|
|
||||||
|
*count = counter;
|
||||||
|
free(textcopy);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get pointers to substrings separated by delimiter
|
||||||
|
void TextSplitEx(const char *text, char delimiter, int *count, const char **ptrs, int *lengths)
|
||||||
|
{
|
||||||
|
int elementsCount = 0;
|
||||||
|
int charsCount = 0;
|
||||||
|
|
||||||
|
ptrs[0] = text;
|
||||||
|
|
||||||
|
for (int i = 0; text[i] != '\0'; i++)
|
||||||
|
{
|
||||||
|
charsCount++;
|
||||||
|
|
||||||
|
if (text[i] == delimiter)
|
||||||
|
{
|
||||||
|
lengths[elementsCount] = charsCount - 1;
|
||||||
|
charsCount = 0;
|
||||||
|
elementsCount++;
|
||||||
|
|
||||||
|
ptrs[elementsCount] = &text[i + 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lengths[elementsCount] = charsCount;
|
||||||
|
elementsCount++;
|
||||||
|
|
||||||
|
*count = elementsCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append text at specific position and move cursor!
|
||||||
|
// REQUIRES: strcpy()
|
||||||
|
void TextAppend(char *text, const char *append, int *position)
|
||||||
|
{
|
||||||
|
strcpy(text + *position, append);
|
||||||
|
*position += strlen(append);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find first text occurrence within a string
|
||||||
|
// REQUIRES: strstr()
|
||||||
|
int TextFindIndex(const char *text, const char *find)
|
||||||
|
{
|
||||||
|
int position = -1;
|
||||||
|
|
||||||
|
char *ptr = strstr(text, find);
|
||||||
|
|
||||||
|
if (ptr != NULL) position = ptr - text;
|
||||||
|
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get upper case version of provided string
|
||||||
|
// REQUIRES: toupper()
|
||||||
|
const char *TextToUpper(const char *text)
|
||||||
|
{
|
||||||
|
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++)
|
||||||
|
{
|
||||||
|
if (text[i] != '\0') buffer[i] = (char)toupper(text[i]);
|
||||||
|
else { buffer[i] = '\0'; break; }
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get lower case version of provided string
|
||||||
|
// REQUIRES: tolower()
|
||||||
|
const char *TextToLower(const char *text)
|
||||||
|
{
|
||||||
|
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_TEXT_BUFFER_LENGTH; i++)
|
||||||
|
{
|
||||||
|
if (text[i] != '\0') buffer[i] = (char)tolower(text[i]);
|
||||||
|
else { buffer[i] = '\0'; break; }
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Pascal case notation version of provided string
|
||||||
|
// REQUIRES: toupper()
|
||||||
|
const char *TextToPascal(const char *text)
|
||||||
|
{
|
||||||
|
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
||||||
|
|
||||||
|
buffer[0] = (char)toupper(text[0]);
|
||||||
|
|
||||||
|
for (int i = 1, j = 1; i < MAX_TEXT_BUFFER_LENGTH; i++, j++)
|
||||||
|
{
|
||||||
|
if (text[j] != '\0')
|
||||||
|
{
|
||||||
|
if (text[j] != '_') buffer[i] = text[j];
|
||||||
|
else
|
||||||
|
{
|
||||||
|
j++;
|
||||||
|
buffer[i] = (char)toupper(text[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else { buffer[i] = '\0'; break; }
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
// Module specific Functions Definition
|
// Module specific Functions Definition
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue