Skip to content

Commit

Permalink
Hide extension refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kisslorand committed Oct 9, 2023
1 parent 89349cc commit 828675d
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 105 deletions.
2 changes: 1 addition & 1 deletion TFT/src/User/API/Printing.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ void initPrintSummary(void)
infoPrintSummary = (PRINT_SUMMARY){.name[0] = '\0', 0, 0, 0, 0, false};

// save print filename (short or long filename)
sprintf(infoPrintSummary.name, "%." STRINGIFY(SUMMARY_NAME_LEN) "s", getPrintFilename());
strscpy(infoPrintSummary.name, getPrintFilename(), SUMMARY_NAME_LEN);
}

void preparePrintSummary(void)
Expand Down
71 changes: 11 additions & 60 deletions TFT/src/User/API/Vfs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,11 @@ bool isRootFolder(void)
}

// check if filename provides a supported filename extension
char * isSupportedFile(const char * filename)
static char * isSupportedFile(const char * filename)
{
char * extPos = strrchr(filename, '.'); // check last "." in the name where extension is supposed to start

if (extPos != NULL && extPos[1] != 'g' && extPos[1] != 'G')
extPos = NULL;

return extPos;
return (extPos != NULL && (extPos[1] == 'g' || extPos[1] == 'G')) ? extPos : NULL;
}

// add a file name or folder name to file list
Expand Down Expand Up @@ -270,7 +267,7 @@ char * getFoldername(uint8_t index)
}

// return the long file name if exists, otherwise the short one
char * getFilename(uint8_t index)
char * getFilename(uint16_t index)
{
if (infoFile.longFile[index] != NULL)
return infoFile.longFile[index];
Expand All @@ -280,56 +277,32 @@ char * getFilename(uint8_t index)

char * hideExtension(char * filename)
{
if (infoSettings.filename_extension == 0) // if filename extension is disabled
if (infoSettings.filename_extension == 0 && strchr(filename, '\0')[1] == '\0') // if showing the filename extension is disabled and extension not already hidden
{
char * extPos = isSupportedFile(filename);

// if filename provides a supported filename extension then
// check extra byte for filename extension check. If 0, no filename extension was previously hidden
if (extPos != NULL && strchr(filename, '\0')[1] == '\0')
*extPos = 0; // temporary hide filename extension
if (extPos != NULL)
*extPos = '\0'; // temporary hide filename extension
}

return filename;
}

char * restoreExtension(char * filename)
{
if (infoSettings.filename_extension == 0) // if filename extension is disabled
if (infoSettings.filename_extension == 0) // if showing the filename extension is disabled
{
char * extPos = strchr(filename, '\0');

// check extra byte for filename extension check. If 0, no filename extension was previously hidden
if (filename[strlen(filename) + 1] != '\0')
filename[strlen(filename)] = '.'; // restore filename extension
if (extPos[1] != '\0')
*extPos = '.'; // restore filename extension
}

return filename;
}

// hide the extension of the file name and return a pointer to that file name
// (the long one if exists, otherwise the short one).
// The hide of the extension is not temporary so do not forget to restore it afterwards!
char * hideFilenameExtension(uint8_t index)
{
char * filename = hideExtension(infoFile.file[index]);

if (infoFile.longFile[index] != NULL)
filename = hideExtension(infoFile.longFile[index]);

return filename;
}

// restore the extension of the file name and return a pointer to that file name
// (the long one if exists, otherwise the short one)
char * restoreFilenameExtension(uint8_t index)
{
char * filename = restoreExtension(infoFile.file[index]);

if (infoFile.longFile[index] != NULL)
filename = restoreExtension(infoFile.longFile[index]);

return filename;
}

// get print filename according to print originator (remote or local to TFT)
char * getPrintFilename(void)
{
Expand All @@ -342,28 +315,6 @@ char * getPrintFilename(void)
return getFilename(infoFile.fileIndex);
}

// get print title according to print originator (remote or local to TFT)
bool getPrintTitle(char * buf, uint8_t len)
{
// example: "SD:/test/cap2.gcode" -> "SD:cap2.gcode"

char * strPtr = getPrintFilename();

// "+ 2": space for terminating null character and the flag for filename extension check
if (strlen(getFS()) + strlen(strPtr) + 2 > len)
{
*buf = '\0'; // set buffer to empty string

return false;
}

strxcpy(buf, getFS(), len); // set source and set the flag for filename extension check
strcat(buf, strPtr); // append filename
hideExtension(buf); // hide filename extension if filename extension feature is disabled

return true;
}

// volume exist detect
static bool volumeSrcStatus[FF_VOLUMES] = {false, false};

Expand Down
7 changes: 3 additions & 4 deletions TFT/src/User/API/Vfs/vfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ bool addFile(bool isFile, const char * shortName, const char * longName); // ad

// called in Print.c
char * getFoldername(uint8_t index); // return the long folder name if exists, otherwise the short one
char * getFilename(uint8_t index); // return the long file name if exists, otherwise the short one
char * hideFilenameExtension(uint8_t index); // hide the extension of the file name and return a pointer to that file name
char * restoreFilenameExtension(uint8_t index); // restore the extension of the file name and return a pointer to that file name
char * getFilename(uint16_t index); // return the long file name if exists, otherwise the short one
char * hideExtension(char * filename); // hide the extension of the file name and return a pointer to that file name
char * restoreExtension(char * filename); // restore the extension of the file name and return a pointer to that file name

// called in PrintingMenu.c
char * getPrintFilename(void); // get print filename according to print originator (remote or local to TFT)
bool getPrintTitle(char * buf, uint8_t len); // get print title according to print originator (remote or local to TFT)

// called in menu.c
bool volumeExists(uint8_t src);
Expand Down
11 changes: 4 additions & 7 deletions TFT/src/User/Menu/Print.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ void gocdeIconDraw(void)
// draw gcode files
for (; (baseIndex + i < infoFile.folderCount + infoFile.fileCount) && (i < NUM_PER_PAGE); i++)
{
restoreFilenameExtension(baseIndex + i - infoFile.folderCount); // restore filename extension if filename extension feature is disabled

if (enterFolder(infoFile.file[baseIndex + i - infoFile.folderCount]) == false) // always use short filename for file path
if (enterFolder(restoreExtension(infoFile.file[baseIndex + i - infoFile.folderCount])) == false) // always use short filename for file path
break;

// if model preview bmp exists, display bmp directly without writing to flash
Expand All @@ -99,8 +97,7 @@ void gocdeIconDraw(void)

exitFolder();

hideFilenameExtension(baseIndex + i - infoFile.folderCount); // hide filename extension if filename extension feature is disabled
normalNameDisp(&gcodeRect[i], (uint8_t*)infoFile.file[baseIndex + i - infoFile.folderCount]); // always use short filename
normalNameDisp(&gcodeRect[i], (uint8_t *)hideExtension(infoFile.file[baseIndex + i - infoFile.folderCount])); // always use short filename
}

// clear blank icons
Expand All @@ -126,7 +123,7 @@ void gocdeListDraw(LISTITEM * item, uint16_t index, uint8_t itemPos)
item->icon = CHARICON_FILE;
item->itemType = LIST_LABEL;
item->titlelabel.index = LABEL_DYNAMIC;
setDynamicLabel(itemPos, hideFilenameExtension(index - infoFile.folderCount)); // hide filename extension if filename extension feature is disabled
setDynamicLabel(itemPos, hideExtension(getFilename(index - infoFile.folderCount))); // hide filename extension if filename extension feature is disabled
}
}

Expand All @@ -150,7 +147,7 @@ bool printPageItemSelected(uint16_t index)
else if (index < infoFile.folderCount + infoFile.fileCount) // gcode file
{
infoFile.fileIndex = index - infoFile.folderCount;
char * filename = restoreFilenameExtension(infoFile.fileIndex); // restore filename extension if filename extension feature is disabled
char * filename = restoreExtension(getFilename(infoFile.fileIndex)); // restore filename extension if filename extension feature is disabled

if (infoHost.connected != true || enterFolder(infoFile.file[infoFile.fileIndex]) == false) // always use short filename for file path
{
Expand Down
12 changes: 10 additions & 2 deletions TFT/src/User/Menu/PrintingMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,19 @@ static void setLayerNumberTxt(char * layer_number_txt)
}
}

// set the print title according to the print originator (remote or local to TFT)
static void setPrintTitle(void)
{
snprintf(title, MAX_TITLE_LEN, "%s%s%c", getFS(), getPrintFilename(), '\0');

hideExtension(title);
}

// initialize printing info before opening Printing menu
static void initMenuPrinting(void)
{
getPrintTitle(title, MAX_TITLE_LEN); // get print title according to print originator (remote or local to TFT)
clearInfoFile(); // as last, clear and free memory for file list
setPrintTitle(); // set the print title according to the print originator (remote or local to TFT)
clearInfoFile(); // as last, clear and free memory for file list

progDisplayType = infoSettings.prog_disp_type;

Expand Down
5 changes: 0 additions & 5 deletions TFT/src/User/Menu/StatusScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ extern "C" {
extern const GUI_RECT msgRect;

void menuStatus(void);
void drawTemperature(void);
void storegantry(int n, float val);
void statusScreen_setMsg(const uint8_t *title,const uint8_t *msg);
void statusScreen_setReady(void);
void drawStatusScreenMsg(void);
float getAxisLocation(uint8_t n);
void gantry_dec(int n, float val);
void gantry_inc(int n, float val);

#ifdef __cplusplus
}
Expand Down
13 changes: 6 additions & 7 deletions TFT/src/User/Menu/Terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ typedef struct
uint8_t lastSrc;
} TERMINAL_DATA;

typedef enum
{
KEYBOARD_VIEW = 0,
TERMINAL_VIEW
} TERMINAL_WINDOW;

typedef enum
{
GKEY_PREV = 0,
Expand Down Expand Up @@ -316,7 +310,12 @@ const uint16_t fontSrcColor[3][3] = {
KEYBOARD_DATA * keyboardData;
TERMINAL_DATA * terminalData;
char * terminalBuf;
TERMINAL_WINDOW curView = KEYBOARD_VIEW;

enum
{
KEYBOARD_VIEW = 0,
TERMINAL_VIEW
} curView = KEYBOARD_VIEW;

bool numpad =
#if defined(KB_TYPE_QWERTY)
Expand Down
18 changes: 13 additions & 5 deletions TFT/src/User/my_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ uint32_t calculateCRC16(const uint8_t *data, uint32_t length)
/*
* - always copy num-1 characters from source to destination
* regardless of null terminating character found in source
* - destination always ends with '\0'
*
* Note:
* - to avoid overflows, the size of "destination" should be at least "num" bytes
* and the size of "source" should be at least "num-1" bytes, in both cases
* including the '\0' terminal character
* - destination always ends with '\0'
*/
void strxcpy(char * destination, const char * source, size_t num)
{
Expand All @@ -62,6 +64,9 @@ void strxcpy(char * destination, const char * source, size_t num)
* - copy source to destination but no more than width-1 characters
* - if null terminating character found in source the rest is padded with 0
* - destination always ends with '\0'
* Note:
* - to avoid overflows, the size of "destination" should be at least
* "width" bytes, including the '\0' terminal character
*/
void strwcpy(char * destination, const char * source, size_t width)
{
Expand All @@ -79,6 +84,10 @@ void strwcpy(char * destination, const char * source, size_t width)
* - copy source to destination but no more than size-1 characters
* - if null terminating character found in source the copy stops there
* - destination always ends with '\0'
* Note:
* - to avoid overflows, the dimension of "destination" should be
* at least the "size" bytes or the size of "source", which one
* is smaller (including the '\0' terminal character)
*/
void strscpy(char * destination, const char * source, size_t size)
{
Expand Down Expand Up @@ -245,10 +254,8 @@ void stripChecksum(char *str)
// "/test/cap2.gcode *36\n\0" -> "/test/cap2.gcode"
// "/test/cap2.gcode \n\0" -> "/test/cap2.gcode"

char *strPtr = strrchr(str, '*'); // e.g. "/test/cap2.gcode *36\n\0" -> "*36\n\0"

if (strPtr == NULL)
strPtr = str + strlen(str); // e.g. "/test/cap2.gcode \n\0" -> "\0"
char *strPtr = strrchr(str, '*');
if (strPtr == NULL) strPtr = strchr(str, '\0');

while (strPtr != str)
{
Expand All @@ -264,6 +271,7 @@ void stripChecksum(char *str)
}
}

// Add the null terminator to remove any trailing unwanted characters
// e.g. " *36\n\0" -> "\0 *36\n\0"
// e.g. " \n\0" -> "\0 \n\0"
//
Expand Down
28 changes: 14 additions & 14 deletions TFT/src/User/my_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extern "C" {
#define MINUTES(t) (t % (60 * 60) / 60) // minutes remaining to next hour
#define SECONDS(t) (t % 60) // seconds remaining to next minute

#define strtod stringToDouble // light weight strtod() function without exponential support
#define strtod stringToDouble // lightweight strtod() function without exponential support

#define strncpy(...) \
do { \
Expand All @@ -79,21 +79,21 @@ extern "C" {
uint8_t inRange(int cur, int tag , int range);
long map(long x, long in_min, long in_max, long out_min, long out_max);

uint32_t calculateCRC16(const uint8_t *data, uint32_t length); // calculate CRC16 checksum
uint32_t calculateCRC16(const uint8_t *data, uint32_t length);

void strxcpy(char * destination, const char * source, size_t num);
void strwcpy(char * destination, const char * source, size_t num); // lightweight and safe strncpy() function with padding
void strscpy(char * destination, const char * source, size_t num); // lightweight and safe strncpy() function without padding

uint8_t string_2_uint8(const uint8_t *string); // string convert to uint8, MSB ("2C" to 0x2C)
uint8_t *uint8_2_string(uint8_t num, uint8_t *string); // uint8 convert to string, MSB (0x2C to "2C")
uint32_t string_2_uint32(const uint8_t *string, const uint8_t bytes_num); // string convert to uint32, MSB
uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *string); // uint32 convert to string, MSB
void timeToString(char *buf, char *strFormat, uint32_t time); // convert time to string with given formatting
double stringToDouble(char *str, char **endptr); // light weight strtod() function without exponential support

const char *stripHead(const char *str); // strip out any leading " ", "/" or ":" character that might be in the string
void stripChecksum(char *str); // strip out any trailing checksum that might be in the string
void strwcpy(char * destination, const char * source, size_t num);
void strscpy(char * destination, const char * source, size_t num);

uint8_t string_2_uint8(const uint8_t *string);
uint8_t *uint8_2_string(uint8_t num, uint8_t *string);
uint32_t string_2_uint32(const uint8_t *string, const uint8_t bytes_num);
uint8_t *uint32_2_string(uint32_t num, uint8_t bytes_num, uint8_t *string);
void timeToString(char *buf, char *strFormat, uint32_t time);
double stringToDouble(char *str, char **endptr);

const char *stripHead(const char *str);
void stripChecksum(char *str);
uint8_t getChecksum(char *str);
bool validateChecksum(char *str);

Expand Down

0 comments on commit 828675d

Please sign in to comment.