# 구현 | Libft

## 새롭게 정의한 함수들

libft에서 따로 설명이 필요한, 새로 정의한 함수들은 다음과 같습니다.

```cpp
int free(void *ptr);
int freeStr(char **str);
int freeDoublestr(char ***doublestr_addr);
```

댕글링 포인터를 만들지 않는 safety free를 위해 free 함수들을 만들어서 사용합니다.

```cpp
char *strsjoin(std::string s1 = "", std::string s2 = "", std::string s3 = "", std::string s4 = "", std::string s5 = "");
```

string 인자들을 합쳐 char pointer로 바꾸는 작업은 cgi 환경변수 세팅을 위해 디자인했습니다.

```cpp
std::set<std::string> stringVectorToSet(std::vector<std::string> stringVector);
std::map<std::string, std::string> stringVectorToMap(std::vector<std::string> stringVector, char sep = ':');
std::string containerToString(std::vector<unsigned char> container, std::string sep = "");
```

스플릿한 벡터를 셋이나 맵으로 변환해야 할 때가 많고, 반대로 vector, set, map을 하나의 문자열로 병합해서 사용해야 할 일들도 있습니다.

```cpp
int getline(int fd, char *line, int max_buffer_size);
int getline(std::string& data, std::string& line, size_t max_buffer_size);
int getline(std::string& data, std::string& line);
int getline(std::string& data, int& readed_size, std::string& line);
```

getline 같은 경우 시행착오를 겪는 과정에서 여러 개를 만들어서 오버로딩을 했지만, 서브젝트 요건에 맞춰 코드를 다듬다보니 1\~2개 밖에 쓰지 않았습니다.

```cpp
template <typename T, typename V>
bool hasKey(T container, V value);
```

코드를 짧게 줄이고 가독적으로 사용하기 위해 key의 존재만 확인하는 템플릿 함수를 정의했습니다.

## Libft.hpp

저희가 Mandatory part에서 사용한 libft의 헤더 파일은 다음과 같습니다.

```cpp
/* C LIBFT */
void bzero(void *data, size_t len);
void *memcpy(void *dest, const void *src, size_t len);
void *calloc(size_t size, size_t count);
char *strdup(const char *s);
int free(void *ptr);
int freeStr(char **str);
int freeDoublestr(char ***doublestr_addr);
size_t strlen(const char *s);
char *strchr(const char *s, int c);
char *strsub(char const *s, unsigned int start, size_t len);
char *strsjoin(std::string s1 = "", std::string s2 = "", std::string s3 = "", std::string s4 = "", std::string s5 = "");
int startswith(const char *str, const char *sub);
int lenDoubleStr(char **str);
size_t pow(size_t root, size_t square);
long long int abs(long long int num);
std::string to_string(long long int n);
int stoi(std::string str, size_t base = 10);
std::string itos(std::string number, size_t from, size_t to);

/* C++ LIBFT */
std::string ltrim(std::string s, std::string seps = " ");
std::string rtrim(std::string s, std::string seps = " ");
std::string trim(std::string s, std::string seps = " ");
std::string getStringFromFile(std::string file_path, int max_size = -1);
std::string getStringFromFd(int fd, int max_size = -1);
std::vector<std::string> split(std::string s, char c = '\n');
std::set<std::string> stringVectorToSet(std::vector<std::string> stringVector);
std::map<std::string, std::string> stringVectorToMap(std::vector<std::string> stringVector, char sep = ':');
std::string containerToString(std::vector<unsigned char> container, std::string sep = "");
int getline(int fd, char *line, int max_buffer_size);
int getline(std::string& data, std::string& line, size_t max_buffer_size);
int getline(std::string& data, std::string& line);
int getline(std::string& data, int& readed_size, std::string& line);
bool isFile(std::string path);
bool isDirectory(std::string path);

/* TCP function */
void convertTimespecToTm(time_t s, struct tm* t);
unsigned long ws_htonl(unsigned long x);
unsigned short ws_htons(unsigned short x);
std::string inet_ntoa(unsigned int address);

/* FD SET operator */
void fdZero(fd_set *x);
void fdSet(int fd, fd_set *x);
int fdIsset(int fd, fd_set *x);
void fdClr(int fd, fd_set *x);
std::string getSetFdString(int max_fd, fd_set* fset);

/* Log util function */
bool isRightTime(int second);
void log(int log_fd, std::string text);
std::string getTimestamp(void);
std::string getSpeed(timeval from);

/* Template function */
template <typename T, typename V>
bool hasKey(T container, V value);
template <typename T>
std::string containerToString(T container, std::string sep = " ");
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42seoul.gitbook.io/webserv/or-libft.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
