浏览代码

getNextLine

isundil 2 年之前
父节点
当前提交
a2820b97bf

+ 2 - 1
.gitignore

@@ -1,4 +1,5 @@
 
-/printf
+/printf/printf
+/getnextline/getNextLine
 *.o
 

+ 27 - 0
getnextline/Makefile

@@ -0,0 +1,27 @@
+
+SRC=	get_next_line.c		\
+		main.c
+
+OBJ=	$(SRC:.c=.o)
+
+NAME=	getNextLine
+
+CFLAGS=	-W -Wall -Wextra -Werror -g3
+
+LDFLAGS=-g3
+
+$(NAME):	all
+
+all: $(OBJ)
+	$(CC) $(OBJ) $(LDFLAGS) -o $(NAME)
+
+clean:
+	$(RM) $(NAME)
+
+fclean:	clean
+	$(RM) $(OBJ)
+
+re:	fclean all
+
+.PHONY:	all clean fclean re
+

二进制
getnextline/getNextLine


+ 59 - 0
getnextline/get_next_line.c

@@ -0,0 +1,59 @@
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include "get_next_line.h"
+
+#define GETNEXTLINE_BUF_LENGTH 3
+
+char* flushBuffer(char* buffer, size_t* bufferSize, char readBuffer[GETNEXTLINE_BUF_LENGTH], size_t readBufLength)
+{
+    char* result;
+
+    if (*bufferSize == 0 && readBufLength == 0)
+        return NULL;
+    result = (char*) malloc(sizeof(*buffer) * (*bufferSize + readBufLength +1));
+    if (*bufferSize)
+        memcpy(result, buffer, *bufferSize);
+    memcpy(&result[*bufferSize], readBuffer, readBufLength);
+    result[readBufLength + *bufferSize] = '\0';
+    if (result[readBufLength + *bufferSize -1] == '\n')
+        result[readBufLength + *bufferSize -1] = '\0';
+    if (buffer)
+        free(buffer);
+    *bufferSize = *bufferSize + readBufLength;
+    return result;
+}
+
+char* get_next_line(int fd)
+{
+    char* result;
+    size_t resultSize;
+    char buf[GETNEXTLINE_BUF_LENGTH];
+    size_t bufPos;
+    int rdResult;
+
+    result = NULL;
+    resultSize = 0;
+    bufPos = 0;
+    while (1)
+    {
+        rdResult = read(fd, &buf[bufPos], 1);
+        if (rdResult < 0)
+        {
+            if (result != NULL)
+                free(result);
+            return NULL;
+        }
+        bufPos += rdResult;
+        if (rdResult == 0 || buf[bufPos-rdResult] == '\n')
+            break;
+        if (bufPos == GETNEXTLINE_BUF_LENGTH)
+        {
+            result = flushBuffer(result, &resultSize, buf, bufPos);
+            bufPos = 0;
+        }
+    }
+    return flushBuffer(result, &resultSize, buf, bufPos);
+}
+

+ 7 - 0
getnextline/get_next_line.h

@@ -0,0 +1,7 @@
+
+#ifndef GETNEXTLINE_H__
+# define GETNEXTLINE_H__
+
+char* get_next_line(int fd);
+
+#endif

+ 20 - 0
getnextline/main.c

@@ -0,0 +1,20 @@
+
+#include <stdlib.h>
+#include <stdio.h>
+#include "get_next_line.h"
+
+int main(int ac, char** av)
+{
+    (void) ac;
+    (void) av;
+    while (1)
+    {
+        char* tmp = get_next_line(0);
+        if (tmp == NULL)
+            break;
+        printf("%s\n", tmp);
+        free(tmp);
+    }
+    return 0;
+}
+

+ 1 - 0
getnextline/test

@@ -0,0 +1 @@
+coucou

+ 0 - 0
Makefile → printf/Makefile


+ 13 - 0
printf/TODO

@@ -0,0 +1,13 @@
+   Conversion specifiers
+       e, E   The  double argument is rounded and converted in the style [-]d.ddde±dd where there is one digit (which is nonzero if the argument is nonzero) before the decimal-point character and the number of digits after it is equal to the precision; if the pre‐
+              cision is missing, it is taken as 6; if the precision is zero, no decimal-point character appears.  An E conversion uses the letter E (rather than e) to introduce the exponent.  The exponent always contains at least two digits; if the value is  zero,
+              the exponent is 00.
+
+       g, G   The  double  argument  is  converted in style f or e (or F or E for G conversions).  The precision specifies the number of significant digits.  If the precision is missing, 6 digits are given; if the precision is zero, it is treated as 1.  Style e is
+              used if the exponent from its conversion is less than -4 or greater than or equal to the precision.  Trailing zeros are removed from the fractional part of the result; a decimal point appears only if it is followed by at least one digit.
+
+       a, A   (C99; not in SUSv2, but added in SUSv3) For a conversion, the double argument is converted to hexadecimal notation (using the letters abcdef) in the style [-]0xh.hhhhp±d; for A conversion the prefix 0X, the letters ABCDEF, and the exponent  separator
+              P is used.  There is one hexadecimal digit before the decimal point, and the number of digits after it is equal to the precision.  The default precision suffices for an exact representation of the value if an exact representation in base 2 exists and
+              otherwise  is  sufficiently large to distinguish values of type double.  The digit before the decimal point is unspecified for nonnormalized numbers, and nonzero but otherwise unspecified for normalized numbers.  The exponent always contains at least
+              one digit; if the value is zero, the exponent is 0.
+

+ 0 - 0
ft_aprintf.c → printf/ft_aprintf.c


+ 0 - 0
ft_dprintf.c → printf/ft_dprintf.c


+ 0 - 0
ft_printf.c → printf/ft_printf.c


+ 0 - 0
ft_printf.h → printf/ft_printf.h


+ 0 - 0
ft_printf_internal.h → printf/ft_printf_internal.h


+ 0 - 0
ft_sprintf.c → printf/ft_sprintf.c


+ 0 - 0
main.c → printf/main.c


+ 0 - 0
printFdBuffer.c → printf/printFdBuffer.c


+ 0 - 0
printfC.c → printf/printfC.c


+ 0 - 0
printfDecimal.c → printf/printfDecimal.c


+ 0 - 0
printfDi.c → printf/printfDi.c


+ 0 - 0
printfN.c → printf/printfN.c


+ 0 - 0
printfParams.c → printf/printfParams.c


+ 0 - 0
printfPercent.c → printf/printfPercent.c


+ 0 - 0
printfWrite.c → printf/printfWrite.c


+ 0 - 0
printfouxX.c → printf/printfouxX.c