Skip to content

Instantly share code, notes, and snippets.

@EsProgram
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsProgram/c609eb61b2a5e943bd5d to your computer and use it in GitHub Desktop.
Save EsProgram/c609eb61b2a5e943bd5d to your computer and use it in GitHub Desktop.
#include "Input.h"
char* GetStrLineRec(int *size){
static int endFlag = 0;
static int index;
static char* str;
char ret;
if (endFlag == 0){
ret = (char) getchar();
++(*size);
if (ret == '\n'){
ret = '\0';
endFlag = 1;
index = (*size) - 1;
str = (char*) malloc(sizeof(char) *(*size));
if (str == NULL){
printf("GetStrLineRec : Failed to secure memory.");
exit(EXIT_FAILURE);
}
}
if (endFlag == 0) GetStrLineRec(size);
}
if (endFlag == 1){
str[index--] = ret;
if (index < 0){
endFlag = 0;
return str;
}
}
return NULL;
}
char* FGetStrLineRec(int *size, FILE *fp, int *eofFlag){
static int endFlag = 0;
static int index;
static char* str;
char ret;
if (endFlag == 0){
ret = (char) fgetc(fp
++(*size);
if (ret == EOF) {
ret = '\n';
*eofFlag = 1;
}
if (ret == '\n'){
ret = '\0';
endFlag = 1;
index = (*size) - 1;
str = (char*) malloc(sizeof(char) *(*size));
if (str == NULL){
printf("FGetStrLineRec : Failed to secure memory.");
exit(EXIT_FAILURE);
}
}
if (endFlag == 0) FGetStrLineRec(size, fp, eofFlag);
}
if (endFlag == 1){
str[index--] = ret;
if (index < 0){
endFlag = 0;
return str;
}
}
return NULL;
}
char* GetStrLine(){
char *s;
int size = 0;
s = GetStrLineRec(&size);
return s;
}
char* FGetStrLine(FILE *fp){
char *s;
int size = 0;
int eofFlag = 0;
s = FGetStrLineRec(&size, fp, &eofFlag);
if (eofFlag && s[0] == '\0') return NULL;
return s;
}
int GetStrSize(char *str){
int count = 0;
if (str == NULL) return -1;
while (1){
if (str[count] == '\0') break;
count++;
}
return count + 1;
}
int GetStrLen(char *str){
int count = 0;
count = GetStrSize(str) - 1;
return count;
}
#ifndef HighClass_2013_12_22_08_10_
#define HighClass_2013_12_22_08_10_
#include<stdio.h>
#include<stdlib.h>
/*
標準入力から1行読み込んで末尾にNULL文字を付加して返す。改行コードは削除される。戻り値のポインタは開放すること。
*/
char* GetStrLine();
/*
ファイルから1行読み込んで末尾にNULL文字を付加して返す。改行コードは削除される。EOFのみならNULLを返す。戻り値のポインタは開放する。
*/
char* FGetStrLine(FILE *fp);
//末尾がNULL文字の文字列(charポインタ)のサイズを返す(NULL文字を含むサイズ)。末尾のNULL文字を判定に使う。引数がNULLなら-1を返す
int GetStrSize(char* str);
//末尾がNULL文字の文字列(charポインタ)の長さを返す(NULL文字を含まない)。GetStrSize-1の値を返す。引数がNULLなら-1を返す
int GetStrLen(char *str);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment