swad-core/swad_string.h

161 lines
6.8 KiB
C
Raw Permalink Normal View History

2014-12-01 23:55:08 +01:00
// swad_string.h: string processing
#ifndef _SWAD_STR
#define _SWAD_STR
/*
SWAD (Shared Workspace At a Distance in Spanish),
is a web platform developed at the University of Granada (Spain),
and used to support university teaching.
This file is part of SWAD core.
Copyright (C) 1999-2024 Antonio Ca<EFBFBD>as Vargas
2014-12-01 23:55:08 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*****************************************************************************/
/********************************* Headers **********************************/
/*****************************************************************************/
2017-01-17 03:10:43 +01:00
#include <limits.h> // For NAME_MAX and PATH_MAX
2015-10-16 02:24:29 +02:00
#include <stdbool.h> // For boolean type
#include <stdio.h> // For FILE
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/***************************** Public constants *****************************/
/*****************************************************************************/
2017-03-08 03:48:23 +01:00
/*
Some special characters, like a chinese character,
are received from a form in a format like this:
%26%2335753%3B --> %26 %23 3 5 7 5 3 %3B --> &#35753;
^ ^ ^
| | |
SpecialChar SpecialChar SpecialChar
Here one chinese character is converted
to 2 special chars + 5 normal chars + 1 special char,
and finally is stored as the following 8 bytes: &#35753;
2014-12-01 23:55:08 +01:00
2017-03-08 03:48:23 +01:00
The maximum UTF-8 code is 1114111 or 0x10FFFF.
So, when read from form, a character may be read temporarily as %26%231114111%3B (16 bytes)
Then it may be transformed to &#1114111; (10 bytes)
So, each char from a form may be transformed finally into a sequence of 1 to 10 bytes,
but temporarily it may need 16 bytes
*/
2017-03-08 14:12:33 +01:00
#define Str_MAX_BYTES_PER_CHAR 16 // Maximum number of bytes of a char.
// Do not change (or change carefully) because it is used to compute size of database fields
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/******************************* Public types *******************************/
/*****************************************************************************/
typedef enum
{
Str_NO_SKIP_HTML_COMMENTS,
Str_SKIP_HTML_COMMENTS,
} Str_SkipHTMLComments_t;
typedef enum
{
Str_FROM_FORM,
Str_FROM_TEXT,
Str_FROM_HTML,
} Str_ChangeFrom_t;
typedef enum
{
Str_DONT_CHANGE,
Str_TO_RIGOROUS_HTML,
Str_TO_HTML,
Str_TO_TEXT,
2015-04-07 21:44:24 +02:00
Str_TO_MARKDOWN,
2014-12-01 23:55:08 +01:00
} Str_ChangeTo_t;
typedef enum
{
Str_DONT_REMOVE_SPACES,
Str_REMOVE_SPACES,
} Str_RemoveSpaces_t;
2014-12-01 23:55:08 +01:00
/*****************************************************************************/
/***************************** Public prototypes ****************************/
/*****************************************************************************/
bool Str_ChIsAlphaNum (char Ch);
unsigned Str_GetNextASCIICharFromStr (const char *Ptr,unsigned char *Ch);
2014-12-01 23:55:08 +01:00
size_t Str_LimitLengthHTMLStr (char *Str,size_t MaxCharsOnScreen);
2021-02-08 19:04:47 +01:00
void Str_AnalyzeTxtAndStoreNotifyEventToMentionedUsrs (long PubCod,const char *Txt);
2014-12-01 23:55:08 +01:00
void Str_ConvertToTitleType (char *Str);
void Str_ConvertToComparable (char *Str);
char *Str_ConvertToUpperText (char *Str);
char *Str_ConvertToLowerText (char *Str);
char Str_ConvertToUpperLetter (char Ch);
char Str_ConvertToLowerLetter (char Ch);
2015-03-11 14:35:33 +01:00
2019-11-11 00:15:44 +01:00
void Str_DoubleNumToStr (char **Str,double Number);
2020-01-11 15:22:02 +01:00
void Str_DoubleNumToStrFewDigits (char **Str,double Number);
2014-12-01 23:55:08 +01:00
void Str_ConvertStrFloatCommaToStrFloatPoint (char *Str);
2020-03-23 17:14:41 +01:00
double Str_GetDoubleFromStr (const char *Str);
2016-06-04 14:21:01 +02:00
void Str_SetDecimalPointToUS (void);
void Str_SetDecimalPointToLocal (void);
2015-03-11 14:35:33 +01:00
2014-12-01 23:55:08 +01:00
void Str_AddStrToQuery (char *Query,const char *Str,size_t SizeOfQuery);
void Str_ChangeFormat (Str_ChangeFrom_t ChangeFrom,Str_ChangeTo_t ChangeTo,
char *Str,size_t MaxLengthStr,
Str_RemoveSpaces_t RemoveLeadingAndTrailingSpaces);
2014-12-01 23:55:08 +01:00
void Str_RemoveLeadingSpacesHTML (char *Str);
void Str_RemoveTrailingSpacesHTML (char *Str);
void Str_RemoveLeadingZeros (char *Str);
void Str_RemoveLeadingArrobas (char *Str);
bool Str_FindStrInFile (FILE *FileSrc,const char *Str,Str_SkipHTMLComments_t SkipHTMLComments);
bool Str_FindStrInFileBack (FILE *FileSrc,const char *Str,Str_SkipHTMLComments_t SkipHTMLComments);
bool Str_WriteUntilStrFoundInFileIncludingStr (FILE *FileTgt,FILE *FileSrc,const char *Str,Str_SkipHTMLComments_t SkipHTMLComments);
char *Str_GetCellFromHTMLTableSkipComments (FILE *FileSrc,char *Str,int MaxLength);
char *Str_GetNextStrFromFileConvertingToLower (FILE *FileSrc,char *Str, int N);
void Str_GetNextStringUntilSpace (const char **StrSrc,char *StrDst,size_t MaxLength);
void Str_GetNextStringUntilSeparator (const char **StrSrc,char *StrDst,size_t MaxLength);
void Str_GetNextStringUntilComma (const char **StrSrc,char *StrDst,size_t MaxLength);
void Str_ReplaceSeveralSpacesForOne (char *Str);
void Str_CopyStrChangingSpaces (const char *StringWithSpaces,char *StringWithoutSpaces,unsigned MaxLength);
2020-02-27 00:19:55 +01:00
2014-12-01 23:55:08 +01:00
long Str_ConvertStrCodToLongCod (const char *Str);
2020-02-27 00:19:55 +01:00
unsigned Str_ConvertStrToUnsigned (const char *UnsignedStr);
2016-03-28 19:30:37 +02:00
size_t Str_GetLengthRootFileName (const char *FileName);
2017-01-17 03:10:43 +01:00
void Str_SplitFullPathIntoPathAndFileName (const char FullPath[PATH_MAX + 1],
char PathWithoutFileName[PATH_MAX + 1],
char FileName[NAME_MAX + 1]);
2014-12-01 23:55:08 +01:00
bool Str_FileIs (const char *FileName,const char *Extension);
bool Str_FileIsHTML (const char *FileName);
bool Str_Path1BeginsByPath2 (const char *Path1,const char *Path2);
void Str_SkipSpacesInFile (FILE *FileSrc);
void Str_FilePrintStrChangingBRToRetAndNBSPToSpace (FILE *FileTgt,const char *Str);
2016-04-01 03:09:45 +02:00
int Str_ReadFileUntilBoundaryStr (FILE *FileSrc,char *StrDst,
const char *BoundaryStr,
unsigned LengthBoundaryStr,
unsigned long long MaxLength);
2014-12-01 23:55:08 +01:00
bool Str_ConvertFilFolLnkNameToValid (char *FileName);
void Str_ConvertToValidFileName (char *Str);
2016-06-30 18:14:09 +02:00
2014-12-01 23:55:08 +01:00
void Str_CreateRandomAlphanumStr (char *Str,size_t Length);
2017-01-17 03:10:43 +01:00
void Str_Copy (char *Dst,const char *Src,size_t DstSize);
void Str_Concat (char *Dst,const char *Src,size_t DstSize);
2017-01-15 02:05:24 +01:00
char *Str_BuildGoToTitle (const char *Where);
void Str_FreeGoToTitle (void);
2019-12-30 14:55:25 +01:00
2014-12-01 23:55:08 +01:00
#endif