CROSS=
CROSSCFLAGS=
PLATFORM=
ifdef CROSS
endif
CC=$(CROSS)gcc
CXX=$(CROSS)g++
AR=$(CROSS)ar
STRIP = $(CROSS)strip -R .comment
DYLIB = so
MAKE = make
CFLAGS = -Wall $(CROSSCFLAGS) -fsigned-char
CXXFLAGS = -Wall $(CROSSCFLAGS) -fsigned-char
COMMONFLAGS = -Wall $(CROSSCFLAGS) -fsigned-char
LINKFLAGS=
DYLINKFLAGS = -Wl,--retain-symbols-file=ExportedFunctions -fPIC --share
DEBUGFLAGS = $(COMMONFLAGS) -g -D_DEBUG -Werror
RELEASEFLAGS = $(COMMONFLAGS) -O2 -fno-strict-aliasing
UTFLAGS = -Dprivate=public -Dprotected=public
COVERAGE = -fprofile-arcs -ftest-coverage
.PHONY: all
all : release
TARGET=main
FLAGS=
INCS = \
LIBS = \
OBJS= \
main.o
%.o : %.c
$(CC) -c $(FLAGS) $(INCS) $< -o $@
%.o : %.cpp
$(CXX) -c $(FLAGS) $(INCS) $< -o $@
$(TARGET): $(OBJS)
$(CXX) $(FLAGS) -o $@ $(OBJS) $(LIBS)
.PHONY: debug
debug: FLAGS=$(DEBUGFLAGS)
debug: clean $(TARGET)
.PHONY: release
release: FLAGS=$(RELEASEFLAGS)
release: clean $(TARGET)
.PHONY: clean
clean:
rm -rf *.o $(TARGET)
#
# -Wall: open some very useful warning options.
# -Werror: treat warning as error.
#
# -fsigned-char: define 'char' as signed char.
# -funsigned-char: define 'char' as unsigned char.
#
# -g: generate debug information.
#
# -Dname: define a macro "name".
# -D_DEBUG: define a macro "_DEBUG".
# -Dprivate=public: define a macro "private", value is "public".
# -Dprotected=public: define a macro "protected", value is "public".
#
# -O0: no optimization.
# -O (-O1): try to optimize.
# -O2: more optimization, but no "trade space for time".
# -O3: more optimization than -O2
# -fstrict-aliasing:
# -fno-strict-aliasing:
#
CROSS=
CROSSCFLAGS=
PLATFORM=
ifdef CROSS
endif
CC=$(CROSS)gcc
CXX=$(CROSS)g++
AR=$(CROSS)ar
STRIP = $(CROSS)strip -R .comment
DYLIB = so
MAKE = make
CFLAGS = -Wall $(CROSSCFLAGS) -fsigned-char
CXXFLAGS = -Wall $(CROSSCFLAGS) -fsigned-char
COMMONFLAGS = -Wall $(CROSSCFLAGS) -fsigned-char
ARFLAGS = rs
LINKFLAGS=
DYLINKFLAGS = -Wl,--retain-symbols-file=ExportedFunctions -fPIC --share
DEBUGFLAGS = $(COMMONFLAGS) -g -D_DEBUG -Werror
RELEASEFLAGS = $(COMMONFLAGS) -O2 -fno-strict-aliasing
UTFLAGS = -Dprivate=public -Dprotected=public
COVERAGE = -fprofile-arcs -ftest-coverage
.PHONY: all
all : release
TARGET=main
FLAGS=
ROOTDIR = ../../../libs
APR_LIB = -L$(ROOTDIR)/apr/apr-1.5.2/ubuntu/x86/lib -lapr-1
APR_INC = -I$(ROOTDIR)/apr/apr-1.5.2/ubuntu/x86/include/apr-1
CRYPTO_LIB = -L$(ROOTDIR)/cryptoPlusPlus/cryptoPlusPlus_5.6.3/ubuntu/x86/lib -lcryptopp
CRYPTO_INC = -I$(ROOTDIR)/cryptoPlusPlus/cryptoPlusPlus_5.6.3/ubuntu/x86/include
CURRENT_PATH = .
PUBLIC_PATH = ../../public
CFILES = $(wildcard *.c) $(wildcard $(PUBLIC_PATH)/*.c)
CXXFILES = $(wildcard *.cpp) $(wildcard $(PUBLIC_PATH)/*.cpp)
CSOURCES = $(notdir $(CFILES))
CXXSOURCES = $(notdir $(CXXFILES))
SOURCES = $(CSOURCES) $(CXXSOURCES)
OBJS = $(CSOURCES:.c=.o) $(CXXSOURCES:.cpp=.o)
VPATH = ../../public
INCS = \
-I$(PUBLIC_PATH) \
$(APR_INC) \
$(CRYPTO_INC)
LIBS = \
$(APR_LIB) \
$(CRYPTO_LIB)
%.o : %.c
$(CC) -c $(FLAGS) $(INCS) $< -o $@
%.o : %.cpp
$(CXX) -c $(FLAGS) $(INCS) $< -o $@
$(TARGET): $(OBJS)
$(CXX) $(FLAGS) -o $@ $(OBJS) $(LIBS)
.PHONY: debug
debug: FLAGS=$(DEBUGFLAGS)
debug: clean $(TARGET)
.PHONY: release
release: FLAGS=$(RELEASEFLAGS)
release: clean $(TARGET)
.PHONY: clean
clean:
rm -rf *.o $(TARGET)
.PHONY: output
output:
echo $(SOURCES)
echo $(OBJS)
``` #ifndef CSINGLE_CORE_H #define CSINGLE_CORE_H
enum CTYPE { COREA, COREB };
class CSingleCore { public: CSingleCore(); virtual ~CSingleCore(); virtual void Show() = 0; };
class CSingleCoreA : public CSingleCore { public: CSingleCoreA(); virtual ~CSingleCoreA(); void Show(); };
class CSingleCoreB : public CSingleCore { public: CSingleCoreB(); virtual ~CSingleCoreB(); void Show(); };
#ifndef CSINGLE_CORE_H
#define CSINGLE_CORE_H
enum CTYPE
{
COREA,
COREB
};
class CSingleCore
{
public:
CSingleCore();
virtual ~CSingleCore();
virtual void Show() = 0;
};
class CSingleCoreA : public CSingleCore
{
public:
CSingleCoreA();
virtual ~CSingleCoreA();
void Show();
};
class CSingleCoreB : public CSingleCore
{
public:
CSingleCoreB();
virtual ~CSingleCoreB();
void Show();
};
class CFactory
{
public:
CSingleCore* CreateSingleCore(enum CTYPE type);
};
#endif
#include "CSingleCore.h"
#include <stdio.h>
CSingleCore::CSingleCore()
{
printf("CSingleCore::CSingleCore()\n");
}
CSingleCore::~CSingleCore()
{
printf("CSingleCore::~CSingleCore()\n");
}
CSingleCoreA::CSingleCoreA()
{
printf("CSingleCoreA::CSingleCoreA()\n");
}
CSingleCoreA::~CSingleCoreA()
{
printf("CSingleCoreA::~CSingleCoreA()\n");
}
void CSingleCoreA::Show()
{
printf("CSingleCoreA\n");
}
CSingleCoreB::CSingleCoreB()
{
printf("CSingleCoreB::CSingleCoreB()\n");
}
CSingleCoreB::~CSingleCoreB()
{
printf("CSingleCoreB::~CSingleCoreB()\n");
}
void CSingleCoreB::Show()
{
printf("CSingleCoreB\n");
}
CSingleCore*
CFactory::CreateSingleCore(enum CTYPE type)
{
if(type == COREA)
return new CSingleCoreA();
else if(type == COREB)
return new CSingleCoreB();
else
return NULL;
}
#include <stdio.h>
#include "CSingleCore.h"
int main()
{
CFactory oFactory;
CSingleCore * pSingleCore = oFactory.CreateSingleCore(COREA);
pSingleCore->Show();
delete pSingleCore;
pSingleCore = oFactory.CreateSingleCore(COREB);
pSingleCore->Show();
delete pSingleCore;
return 0;
}
CXX=g++
CFLAGS=
TARGET=main
OBJS= \
main.o \
CSingleCore.o
%.o : %.cpp
$(CXX) -c $(CFLAGS) -fno-strict-aliasing $< -o $@
$(TARGET): $(OBJS)
$(CXX) $(CFLAGS) -o $@ $(OBJS)
clean:
rm -rf *.o $(TARGET)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#define JBUF_SIZE 4096
#define BBUF_SIZE 2048
#define MBUF_SIZE 1024
#define SBUF_SIZE 512
#define BUF_SIZE 256
#define LBUF_SIZE 128
#define UBUF_SIZE 64
#define NBUF_SIZE 32
int
iGetFileSize(FILE * fp)
{
fseek(fp, 0, SEEK_END);
int iFileSize = ftell(fp);
rewind(fp);
return iFileSize;
}
int
iGetFileData(const char * pcFile, char ** ppcContent, int * piContentSize)
{
if(NULL == pcFile) {
return -1;
}
FILE * fp = fopen(pcFile, "rb");
if(NULL == fp) {
printf("fopen %s failed\n", pcFile);
return -1;
}
int iFileSize = iGetFileSize(fp);
printf("iFileSize = %d\n", iFileSize);
if(iFileSize <= 0) {
printf("%s file size = %d\n", pcFile, iFileSize);
return -1;
}
char * pcContent = (char*)malloc(iFileSize + 1);
if(NULL == pcContent) {
printf("malloc failed\n");
return -1;
}
memset(pcContent, 0, iFileSize + 1);
int iReadBytes = fread(pcContent, 1, iFileSize, fp);
if(iReadBytes != iFileSize) {
printf("fread %s failed\n", pcFile);
return -1;
}
pcContent[iFileSize] = '\0';
if(fclose(fp) != 0) {
printf("fclose %s failed\n", pcFile);
return -1;
}
*ppcContent = pcContent;
*piContentSize = iFileSize;
return 0;
}
int
iWriteToFile(const char * pcFileName, const char * pcContent, int iContentSize)
{
if(NULL == pcFileName || NULL == pcContent) {
return -1;
}
FILE * fp = fopen(pcFileName, "wb+");
if(NULL == fp) {
printf("fopen %s failed\n", pcFileName);
return -1;
}
int iWriteBytes = fwrite(pcContent, iContentSize, 1, fp);
if(iWriteBytes <= 0) {
printf("fwrite %s failed\n", pcFileName);
return -1;
}
if(fclose(fp) != 0) {
printf("fclose %s failed\n", pcFileName);
return -1;
}
return 0;
}
int
iReplaceTab(const char * pcStr, int iSize, char ** ppcNewContent, int * piNewSize)
{
if(NULL == pcStr || iSize <= 0) {
return -1;
}
int i = 0;
int iTabNum = 0;
for (i = 0; i < iSize; i++)
{
if(pcStr[i] == '\t')
{
++iTabNum;
}
}
int iNewSize = iSize + iTabNum*3 + 1;
if(iTabNum == 0)
{
return 1;
}
char * pcNewStr = (char*)malloc(iNewSize);
if(NULL == pcNewStr) {
printf("malloc failed\n");
return -1;
}
memset(pcNewStr, 0, iNewSize);
int iIndex = 0;
for (i = 0; i < iSize; i++)
{
if(pcStr[i] == '\t')
{
pcNewStr[iIndex++] = ' ';
pcNewStr[iIndex++] = ' ';
pcNewStr[iIndex++] = ' ';
pcNewStr[iIndex++] = ' ';
}
else
{
pcNewStr[iIndex++] = pcStr[i];
}
}
pcNewStr[iIndex] = '\0';
*ppcNewContent = pcNewStr;
*piNewSize = iIndex;
return 0;
}
void vReplaceFileTab(const char * pcFileName)
{
if(NULL == pcFileName) {
printf("NULL == pcFileName\n");
return ;
}
char * pcContent = NULL;
int iContentSize = 0;
int iRet = iGetFileData(pcFileName, &pcContent, &iContentSize);
if(iRet != 0) {
printf("iGetFileData failed\n");
return ;
}
char * pcNewContent = NULL;
int iNewSize = 0;
iRet = iReplaceTab(pcContent, iContentSize, &pcNewContent, &iNewSize);
if(iRet == 1)
{
//printf("There is no tab in %s\n", pcFileName);
free(pcContent);
return ;
}
else if(iRet != 0)
{
printf("NULL == pcNewContent\n");
return ;
}
//printf("%s\n", pcContent);
//printf("%s\n", pcNewContent);
iRet = iWriteToFile(pcFileName, pcNewContent, iNewSize);
if(iRet != 0) {
printf("iWriteToFile failed\n");
}
if(pcContent != NULL) {
free(pcContent);
}
if(pcNewContent != NULL) {
free(pcNewContent);
}
}
bool bIsMarkDownFile(const char * pcFileName)
{
if(NULL == pcFileName)
{
return false;
}
int iLen = strlen(pcFileName);
if(iLen <= 3)
{
return false;
}
if(pcFileName[iLen-1] == 'd' &&
pcFileName[iLen-2] == 'm' &&
pcFileName[iLen-3] == '.')
{
return true;
}
else
{
return false;
}
}
void vReplaceTabInDirectory(const char * pcDirectory)
{
if(NULL == pcDirectory) {
return ;
}
DIR * dir = opendir(pcDirectory);
if(NULL == dir) {
printf("opendir:(%s) failed, errno=(%d,%s)\n", pcDirectory, errno, strerror(errno));
return ;
}
struct dirent * ptr;
while ( (ptr = readdir(dir)) != NULL )
{
if(DT_UNKNOWN == ptr->d_type)
{
continue;
}
else if(DT_DIR == ptr->d_type)
{
if(strcmp(".", ptr->d_name) == 0 ||
strcmp("..", ptr->d_name) == 0)
{
continue;
}
char acNewDir[SBUF_SIZE] = {0};
sprintf(acNewDir, "%s/%s", pcDirectory, ptr->d_name);
vReplaceTabInDirectory(acNewDir);
}
else
{
if(bIsMarkDownFile(ptr->d_name))
{
char acFilePath[MBUF_SIZE] = {0};
sprintf(acFilePath, "%s/%s", pcDirectory, ptr->d_name);
vReplaceFileTab(acFilePath);
}
//printf("%d:%s/%s\n", ptr->d_type, pcDirectory, ptr->d_name);
}
}
}
int main()
{
vReplaceTabInDirectory(".");
return 0;
}
红黑树的5个性质:
1.每个节点要么是红色,要么是黑色。
2.根节点是黑色。
3.每个叶子节点,即空节点(NIL)是黑色的。
4.如果一个节点是红色的,那么它的两个儿子都是黑色的。
(从根节点到每个叶子节点的路径上,不能有两个连续的红色节点。)
5.对每个节点,从该节点到叶子节点的所有路径上包含相同数目的黑色节点。
这些约束强制了红黑树的关键性质: 从根到叶子的最长的可能路径不多于最短的可能路径的两倍长。
要知道为什么这些特性确保了这个结果,注意到性质4导致了路径不能有两个毗连的红色节点就足够了。 最短的可能路径都是黑色节点,最长的可能路径有交替的红色和黑色节点。 因为根据性质5所有最长的路径都有相同数目的黑色节点,这就表明了没有路径能多于任何其他路径的两倍长。