Hi,
I pass the string to the following ValidExpression function, it substitutes all invalids with an underscore. You may modify it to suit your purposes.
/* code begins */
function validExpression(cInstring)
local nIter,cThisLetter,nPos := 0
local cInvalids := "~!@'#$%^&*()-+\.:;-<>"+space(1)
local nLenString := LEN(cInString)
local cOutValue := ''
for nIter = 1 TO nLenString
cThisLetter := SUBST(cInString,nIter,1)
IF !(cThisLetter $ cInvalids)
cOutValue += cThisLetter
else
cOutValue += "_"
ENDIF
NEXT
if substr(cOutvalue,-1) = "_"
cOutvalue := substr(cOutvalue,1,nLenstring-1)
endif
RETURN cOutValue
/*Code Ends*/
HTH,
Warm regards,
Jayadev
--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups
"Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/f9f087c4-9969-473c-9ac3-ac960e5cab28%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the Google
Groups "Harbour Users" group.
Unsubscribe: harbour-user...@googlegroups.com
Web: http://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbour-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/harbour-users/55fa4070-0e63-4e76-b6f5-d58c9e846a70%40googlegroups.com.
Unsubscribe: harbou...@googlegroups.com
Web: http://groups.google.com/group/harbour-users
---
You received this message because you are subscribed to the Google Groups "Harbour Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to harbou...@googlegroups.com.
#define MAX_FILENAME_LENGTH 257
FUNCTION IsValidFName( /*@*/ xName, cExt, lSpaceScore, lCheckMaxRelative, cTargetPath, lCheckExists )
LOCAL cInvalidChars, aTmp
LOCAL nI, cType
IF Empty( xName )
RETURN .F.
ENDIF
cType := ValType( xName )
// acceptable values can be of string, numeric, date or timestamp type
DO CASE
CASE cType == "N"
xName := hb_ntos( xName )
CASE cType $ "D"
xName := hb_DtoC( xName )
CASE cType $ "T"
xName := hb_TSToStr( xName, .T. )
OTHERWISE
IF ! HB_ISSTRING( xName )
RETURN .F.
ENDIF
ENDCASE
// invalid chars for windows O/S. Linux guys invited to adapt it as needed.
cInvalidChars := '<>:"/\|?*.'
FOR nI := 0 TO 31
cInvalidChars += Chr( nI )
NEXT
// prepare to remove invlaid chars
aTmp := {}
FOR nI := 1 TO Len( cInvalidChars )
AAdd( aTmp, "" )
NEXT
xName := hb_StrReplace( xName, cInvalidChars, aTmp )
// nothing remained?
IF Len( xName ) < 1
RETURN .F.
ENDIF
// truncate to valid max length
xName := Left( xName, MAX_FILENAME_LENGTH )
// transform spaces to underscores, if asked
IF hb_DefaultValue( lSpaceScore, .F. )
xName := StrTran( xName, " ", "_" )
ENDIF
// now adjust length to be valid when conjuncted to target path
hb_Default( @cTargetPath, hb_cwd() )
cTargetPath := hb_DirSepAdd( cTargetPath )
IF hb_DefaultValue( lCheckMaxRelative, .T. )
nI := Len( cTargetPath ) + Len( xName )
? nI
IF nI > MAX_FILENAME_LENGTH
nI := MAX_FILENAME_LENGTH - (nI - MAX_FILENAME_LENGTH)
xName := Left( xName, nI )
ENDIF
ENDIF
// finally add optional extension and check whether the filename
// already exists into target path (default current path)
lCheckExists := ( hb_DefaultValue( lCheckExists, .F. ) .OR. ! Empty( cExt ) )
IF lCheckExists
xName := hb_FNameExtSet( xName, hb_DefaultValue( cExt, "" ) )
IF hb_vfExists( cTargetPath+xName )
RETURN .F.
ENDIF
ENDIF
RETURN .T.