check_add_file_ext

 

Contents

Description

Add or replace extension name of a file.

Syntax

int check_add_file_ext( LPSTR lpszFilename, LPCSTR lpcszExt, LPSTR lpszExt2 = NULL )

Parameters

lpszFilename
[input] filename with or without path, typically from a buffer with MAXFULLPATH in size
lpcszExt
[input] file extension, without the dot.
lpszExt2
[output] optional buffer, see Remark

Return

It depends on the various combinations of the arguments, enumerated as CHECKADDFILEEXTERROR as below. lpszFilename is modified only when CHECKADDFILEEXT_UPDATE_SUCCESSFULLY returned.

typedef enum
{
        //length of lpszFilename plus extension is longer than MAX_FILENAME.
        CHECKADDFILEEXT_EXCEED_MAXFILENAME      = -100, 
                
        CHECKADDFILEEXT_UPDATE_SUCCESSFULLY     = 0, //lpszFilename updated successfully.
        CHECKADDFILEEXT_UPDATE_NO_NEED,//no need to update lpszFilename
        
        //If lpcszExt is NULL, means testing, then CHECKADDFILEEXT_HAS_EXTENSION 
        //or CHECKADDFILEEXT_HAS_NOT_EXTENSION would be returned.
        CHECKADDFILEEXT_HAS_EXTENSION,
        CHECKADDFILEEXT_HAS_NOT_EXTENSION,
} CHECKADDFILEEXTERROR;

Examples

EX1

void check_add_file_ext_ex1()
{    
    char szTemp[20];
    char strFileName[100];
    string str = GetAppPath(1) + "origin.ini";
    lstrcat(strFileName, str);
    
    int nRet = check_add_file_ext(strFileName,NULL,szTemp);
    out_str(szTemp);
}

Remark

Add extension to file name if not present, or to get file extension string

This function assumes that lpszFilename has extra space to append file extension

if lpcszExt ==NULL, then return CHECKADDFILEEXT_HAS_EXTENSION if has extension and copied to into lpszExt2 if not NULL

if lpcszExt !=NULL, then

if lpszExt2 == NULL, then replace/append ext regardless if existed or not, return CHECKADDFILEEXT_UPDATE_NO_NEED if no change

if lpszExt2 != NULL, then it is assumed to be 2nd ext, replace/append if not either, return CHECKADDFILEEXT_UPDATE_NO_NEED if no change

See Also

add_file_extension

header to Include

origin.h

Reference