Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 50 additions & 53 deletions include/string_gf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifndef POKEPLATINUM_STRING_H
#define POKEPLATINUM_STRING_H
#ifndef POKEPLATINUM_STRING_GF_H
#define POKEPLATINUM_STRING_GF_H

#include "constants/heap.h"
#include "generated/string_padding_mode.h"

#include "charcode.h"

Expand All @@ -9,13 +12,10 @@
typedef struct String {
/// Maximum allocated size of the buffer.
u16 maxSize;

/// Size of the buffer, at present; the length of the underlying string.
u16 size;

/// Integrity value specified at allocation time.
u32 integrity;

/// The underlying character buffer.
///
/// UB: This is meant to be a flexible array, but is purposely defined
Expand All @@ -24,15 +24,12 @@ typedef struct String {
charcode_t data[1];
} String;

#include "generated/string_padding_mode.h"

/**
* Control codes for which charset to use when formatting a string.
*/
enum CharsetMode {
/// Use the Japanese character set (i.e., full-width characters).
CHARSET_MODE_JP,

/// Use the English character set (i.e., half-width characters).
CHARSET_MODE_EN,
};
Expand All @@ -41,11 +38,11 @@ enum CharsetMode {
* @brief Init routine. Allocates memory for a new String, initializes it
* with an integrity-check value, and exposes its address.
*
* @param size The size of the new String to allocate from the heap.
* @param maxSize The maximum size of the new String to allocate from the heap.
* @param heapID ID of the heap to own the new String.
* @return Address to the new String.
*/
String *String_Init(u32 size, u32 heapID);
String *String_New(u32 maxSize, enum HeapID heapID);

/**
* @brief Free routine. Destroys an existing String and returns its memory
Expand All @@ -66,13 +63,13 @@ void String_Clear(String *string);

/**
* @brief Copies the contents of the data buffer in `src` to the data buffer
* in `dst`. Fails if the contents of `src` will not fit into `dst`'s
* in `dest`. Fails if the contents of `src` will not fit into `dest`'s
* allocated memory.
*
* @param dst Destination buffer. `dst->size` and `dst->data` will be modified.
* @param dest Destination buffer. `dest->size` and `dest->data` will be modified.
* @param src Source buffer. `src->size` and `src->data` will be accessed.
*/
void String_Copy(String *dst, const String *src);
void String_Copy(String *dest, String *src);

/**
* @brief Clones the contents of a given String into a new String and returns
Expand All @@ -81,144 +78,144 @@ void String_Copy(String *dst, const String *src);
* This is effectively a nice wrapper around the following code:
*
* ```c
* String *dst = String_Init(src->size + 1, heapID);
* String_Copy(dst, src);
* String *dest = String_New(src->size + 1, heapID);
* String_Copy(dest, src);
* ```
*
* @param src Source buffer to clone.
* @param heapID ID of the heap which will own the new String.
* @return Address to the cloned String.
*/
String *String_Clone(const String *src, u32 heapID);
String *String_Clone(String *src, enum HeapID heapID);

/**
* @brief Format a number into a destination buffer.
*
* The destination buffer must already be initialized.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param num Number to be formatted.
* @param maxDigits Maximum number of digits to format, right-justified.
* @param paddingMode Padding mode to use when formatting the number.
* @param charsetMode Charset mode to use when formatting the number.
*/
void String_FormatInt(String *dst, int num, u32 maxDigits, enum PaddingMode paddingMode, enum CharsetMode charsetMode);
void String_FormatInt(String *dest, int num, u32 maxDigits, enum PaddingMode paddingMode, enum CharsetMode charsetMode);

/**
* @brief Format a number into a destination buffer.
*
* The destination buffer must already be initialized.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param num Number to be formatted.
* @param maxDigits Maximum number of digits to format, right-justified.
* @param paddingMode Padding mode to use when formatting the number.
* @param charsetMode Charset mode to use when formatting the number.
*/
void String_FormatU64(String *dst, u64 num, u32 maxDigits, enum PaddingMode paddingMode, enum CharsetMode charsetMode);
void String_FormatU64(String *dest, u64 num, u32 maxDigits, enum PaddingMode paddingMode, enum CharsetMode charsetMode);

/**
* @brief Parses a numeric string into a number.
*
* @param src Numeric string.
* @param string Numeric string.
* @param[out] success Flag denoting if the result string was fully processed.
* @return Parsed result.
*/
u64 String_AtoI(const String *src, BOOL *success);
u64 String_AtoI(String *string, BOOL *success);

/**
* @brief Compares two strings. Follows the `strcmp` standard.
* @brief Compares two strings. Similar to the `strcmp` standard, but without returning -1. Essentially a BOOL.
*
* @param str1 First string.
* @param str2 Second string.
* @return 0 if the strings match, 1 if they do not.
* @return FALSE if the strings match, TRUE if they do not.
*/
int String_Compare(const String *str1, const String *str2);
BOOL String_Compare(String *str1, String *str2);

/**
* @brief Accessor for the length of a string.
*
* @param string
* @return `string->size`
*/
u32 String_Length(const String *string);
u16 String_GetLength(String *string);

/**
* @brief Counts the number of lines in a string.
*
* @param string
* @return The number of lines in `string`.
*/
u32 String_NumLines(const String *string);
u32 String_CountLines(const String *string);

/**
* @brief Copies a particular line number from `src` into `dst`.
* @brief Copies a particular line number from `src` into `dest`.
*
* Lines are zero-indexed, e.g. `lineNum == 0` will copy the first line,
* `lineNum == 1` will copy the second line, etc.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param src Source buffer.
* @param lineNum Number of the line to copy, zero-indexed.
*/
void String_CopyLineNum(String *dst, const String *src, u32 lineNum);
void String_CopyLine(String *dest, const String *src, u32 lineNum);

/**
* @brief Copies data from a raw character buffer into a managed String.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param src Raw character source buffer.
*/
void String_CopyChars(String *dst, const charcode_t *src);
void String_CopyFromChars(String *dest, const charcode_t *src);

/**
* @brief Copies a specific number of values from a raw character buffer into
* a managed String.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param src Raw character source buffer.
* @param num Number of values to copy.
*/
void String_CopyNumChars(String *dst, const charcode_t *src, u32 num);
void String_CopyNumChars(String *dest, const charcode_t *src, u32 num);

/**
* @brief Dumps the contents of a String into a raw character buffer.
*
* Fails if `src->size + 1 > dstSize`.
* Fails if `src->size + 1 > destSize`.
*
* @param src Source buffer.
* @param[out] dst Destination buffer.
* @param dstSize Size of `dst`.
* @param[out] dest Destination buffer.
* @param destSize Size of `dest`.
*/
void String_ToChars(const String *src, charcode_t *dst, u32 dstSize);
void String_CopyToChars(String *src, charcode_t *dest, u32 destSize);

/**
* @brief Accessor for the underlying data buffer of a managed string.
*
* @param string
* @return Underlying data buffer for `string`.
*/
const charcode_t *String_GetData(const String *string);
charcode_t *String_GetChars(String *string);

/**
* @brief Concatenates `src` onto the end of `dst`, if allocation permits.
* @brief Concatenates `src` onto the end of `dest`, if allocation permits.
*
* Fails if `dst->maxSize < dst->size + src->size + 1`.
* Fails if `dest->maxSize < dest->size + src->size + 1`.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param src Source buffer.
*/
void String_Concat(String *dst, const String *src);
void String_Concat(String *dest, String *src);

/**
* @brief Appends a single character onto `dst`, if allocation permits.
* @brief Appends a single character onto `string`, if allocation permits.
*
* Fails if `dst->maxSize >= dst->size + 1`.
* Fails if `string->maxSize >= string->size + 1`.
*
* @param[out] dst Destination buffer.
* @param[out] string Destination buffer.
* @param c Character to append.
*/
void String_AppendChar(String *dst, charcode_t c);
void String_AppendChar(String *string, charcode_t c);

/**
* @brief Checks if a given string is a trainer name.
Expand All @@ -232,22 +229,22 @@ void String_AppendChar(String *dst, charcode_t c);
BOOL String_IsTrainerName(String *string);

/**
* @brief Concatenates `src` onto the end of `dst`, accounting for trainer
* @brief Concatenates `src` onto the end of `dest`, accounting for trainer
* name compression.
*
* If `src` is not a trainer name, then this falls back to `String_Concat`.
*
* @param[out] dst Destination buffer.
* @param[out] dest Destination buffer.
* @param src Source buffer.
*/
void String_ConcatTrainerName(String *dst, String *src);
void String_ConcatTrainerName(String *dest, String *src);

/**
* @brief Converts a particular character to uppercase.
*
* @param string
* @param i Index of the character to capitalize, zero-indexed.
* @param index Index of the character to capitalize, zero-indexed.
*/
void String_UpperChar(String *string, int i);
void String_UpperChar(String *string, int index);

#endif // POKEPLATINUM_STRING_H
#endif // POKEPLATINUM_STRING_GF_H
4 changes: 2 additions & 2 deletions src/applications/bag/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ static void SetupTextLoaders(BagController *controller)
controller->strTemplate = StringTemplate_Default(HEAP_ID_BAG);
controller->itemNamesLoader = MessageLoader_Init(MSG_LOADER_PRELOAD_ENTIRE_BANK, NARC_INDEX_MSGDATA__PL_MSG, TEXT_BANK_ITEM_NAMES, HEAP_ID_BAG);
controller->moveNamesLoader = MessageLoader_Init(MSG_LOADER_PRELOAD_ENTIRE_BANK, NARC_INDEX_MSGDATA__PL_MSG, TEXT_BANK_MOVE_NAMES, HEAP_ID_BAG);
controller->stringBuffer = String_Init(256, HEAP_ID_BAG);
controller->stringBuffer = String_New(256, HEAP_ID_BAG);
}

static void CountAccessiblePockets(BagController *controller)
Expand Down Expand Up @@ -1007,7 +1007,7 @@ static void LoadCurrentPocketItemNames(BagController *controller)
static void InitItemNameBuffers(BagController *controller)
{
for (u32 i = 0; i < LARGEST_POCKET_SIZE; i++) {
controller->itemNames[i] = String_Init(18, HEAP_ID_BAG);
controller->itemNames[i] = String_New(18, HEAP_ID_BAG);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/applications/bag/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ void BagUI_PrintItemDescription(BagController *controller, u16 item)
String *string;

if (item != 0xffff) {
string = String_Init(130, HEAP_ID_BAG);
string = String_New(130, HEAP_ID_BAG);
Item_LoadDescription(string, item, HEAP_ID_BAG);
} else {
string = MessageLoader_GetNewString(controller->bagStringsLoader, Bag_Text_CloseBagDescription);
Expand Down Expand Up @@ -311,7 +311,7 @@ void BagUI_PrintItemCount(BagController *controller, u16 count, u16 yOffset, u32
Text_AddPrinterWithParamsAndColor(&controller->windows[BAG_UI_WINDOW_ITEM_LIST], FONT_SYSTEM, controller->itemCountX, ITEM_COUNT_START_POS, yOffset, TEXT_SPEED_NO_TRANSFER, color, NULL);
}

String *string = String_Init(10, HEAP_ID_BAG);
String *string = String_New(10, HEAP_ID_BAG);

StringTemplate_SetNumber(controller->strTemplate, 0, count, 3, PADDING_MODE_NONE, CHARSET_MODE_EN);
StringTemplate_Format(controller->strTemplate, string, controller->itemCountNumberFmt);
Expand Down Expand Up @@ -444,7 +444,7 @@ void BagUI_ShowItemActionsMenu(BagController *controller, u8 *actions, u8 numAct
Window_FillTilemap(&controller->windows[msgBoxWindowIdx], 15);

String *template = MessageLoader_GetNewString(controller->bagStringsLoader, Bag_Text_ItemIsSelected);
String *formatted = String_Init(28 * 2, HEAP_ID_BAG);
String *formatted = String_New(28 * 2, HEAP_ID_BAG);
BagApplicationPocket *pocket = &controller->bagCtx->accessiblePockets[controller->bagCtx->currPocketIdx];

BufferPocketSlotItemName(controller, pocket->cursorScroll + pocket->cursorPos - 1, 0);
Expand Down Expand Up @@ -489,7 +489,7 @@ void BagUI_PrintMovingItemMsg(BagController *controller)
Window_FillTilemap(&controller->windows[BAG_UI_WINDOW_ITEM_DESCRIPTION], 0);

String *template = MessageLoader_GetNewString(controller->bagStringsLoader, Bag_Text_MoveItemWhere);
String *formatted = String_Init(130, HEAP_ID_BAG);
String *formatted = String_New(130, HEAP_ID_BAG);

BufferPocketSlotItemName(controller, controller->movedItemPos - 1, 0);

Expand All @@ -508,7 +508,7 @@ void BagUI_ShowItemTrashWindows(BagController *controller)
Window_FillTilemap(&controller->windows[BAG_UI_WINDOW_MSG_BOX], 15);

String *template = MessageLoader_GetNewString(controller->bagStringsLoader, Bag_Text_ThrowAwayHowMany);
String *formatted = String_Init(14 * 2 * 2, HEAP_ID_BAG);
String *formatted = String_New(14 * 2 * 2, HEAP_ID_BAG);
BagApplicationPocket *pocket = &controller->bagCtx->accessiblePockets[controller->bagCtx->currPocketIdx];

BufferPocketSlotItemName(controller, pocket->cursorScroll + pocket->cursorPos - 1, 0);
Expand Down
4 changes: 2 additions & 2 deletions src/applications/diploma.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,8 @@ static void Diploma_LoadGraphics(Diploma *data)

static void Diploma_LoadText(Diploma *data)
{
String *string = String_Init(512, data->heapID);
String *formatString = String_Init(512, data->heapID);
String *string = String_New(512, data->heapID);
String *formatString = String_New(512, data->heapID);

MessageLoader_GetString(data->messageLoader, Diploma_Text_Player, formatString);
StringTemplate_SetPlayerName(data->stringTemplate, 0, data->trainerInfo);
Expand Down
2 changes: 1 addition & 1 deletion src/applications/journal_display/journal_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ static void JournalController_InitStringUtil(JournalManager *journalManager)
{
journalManager->loader = MessageLoader_Init(MSG_LOADER_PRELOAD_ENTIRE_BANK, NARC_INDEX_MSGDATA__PL_MSG, TEXT_BANK_JOURNAL_ENTRIES, HEAP_ID_JOURNAL);
journalManager->template = StringTemplate_Default(HEAP_ID_JOURNAL);
journalManager->string = String_Init(128, HEAP_ID_JOURNAL);
journalManager->string = String_New(128, HEAP_ID_JOURNAL);
}

static void JournalController_FreeStringUtil(JournalManager *journalManager)
Expand Down
10 changes: 5 additions & 5 deletions src/applications/journal_display/journal_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,18 @@ static void JournalPrinter_PrintTrainerEvent(JournalManager *journalManager, Win
}

String *name = MessageBank_GetNewStringFromNARC(NARC_INDEX_MSGDATA__PL_MSG, TEXT_BANK_LOCATION_NAMES, MapHeader_GetMapLabelTextID(journalEntryTrainer.mapID), HEAP_ID_JOURNAL);
strLength = String_Length(name);
strLength = String_GetLength(name);
String_Free(name);

if (Trainer_LoadParam(journalEntryTrainer.trainerID, TRDATA_CLASS) == TRAINER_CLASS_RIVAL) {
name = MessageLoader_GetNewString(journalManager->loader, JournalEntries_Text_RivalName);
StringTemplate_SetRivalName(journalManager->template, 1, journalManager->saveData);
StringTemplate_Format(journalManager->template, journalManager->string, name);
strLength += String_Length(journalManager->string);
strLength += String_GetLength(journalManager->string);
String_Free(name);
} else {
name = MessageBank_GetNewStringFromNARC(NARC_INDEX_MSGDATA__PL_MSG, TEXT_BANK_NPC_TRAINER_NAMES, journalEntryTrainer.trainerID, HEAP_ID_JOURNAL);
strLength += String_Length(name);
strLength += String_GetLength(name);
String_Free(name);
StringTemplate_SetTrainerName(journalManager->template, 1, journalEntryTrainer.trainerID);
}
Expand Down Expand Up @@ -886,9 +886,9 @@ static void JournalPrinter_PrintPokemonDefeated(JournalManager *journalManager,

static void JournalPrinter_SetPlayerOrPokemonName(JournalManager *journalManager, u16 *name, u8 unused, u8 idx)
{
String *string = String_Init(32, HEAP_ID_JOURNAL);
String *string = String_New(32, HEAP_ID_JOURNAL);

String_CopyChars(string, name);
String_CopyFromChars(string, name);
StringTemplate_SetString(journalManager->template, idx, string, unused, TRUE, GAME_LANGUAGE);
String_Free(string);
}
Expand Down
Loading