Exemplo n.º 1
0
int main(int argc, char *argv[]){
	if (argc < 2){
		printf("No pattern\n");
		return -1;
	}
	struct RL* rl = rl_open(0,MAX_LEN);
	int i;
	char** patterns = malloc((argc-1)*sizeof(char*));
	char** repls = malloc((argc-1)*sizeof(char*));
	int* greeds = malloc((argc-1)*sizeof(int));
	char* str;
	for (i = 1; i < argc; i++){
		//get pattern
		strtok(argv[i], "/"); 
		char* pattern_tmp = strtok(NULL, "/");//skip s/
		patterns[i-1] = malloc(strlen(pattern_tmp)+1);
		strcpy(patterns[i-1], pattern_tmp);
		pattern_tmp = strtok(NULL, "/");
		repls[i-1] = malloc(strlen(pattern_tmp)+1);
		strcpy(repls[i-1], pattern_tmp);
		greeds[i-1] = (strtok(NULL, "/") != NULL);
		//printf("%s\n%s\n%d\n", patterns[i-1], repls[i-1], greeds[i-1]);
	}
	//test
	int res = rl_readline(rl, rl->buf, rl->buf_size);
	while (res!=0){
		if (res > 0){
			//do shit and write
			//printf("%s",rl->buf);
			str = malloc((res+1)*sizeof(char));
			strcpy(str,rl->buf);
			//printf("%s+%s\n", str,repls[i]);
			for (i = 0; i < argc-1; i++){
				//if (parse_pattern(str,patterns[i],repls[i],greeds[i]) == NULL){
				//	printf("NO F**K YOU");
				//}
				str = parse_pattern(str,patterns[i],repls[i],greeds[i]);
			}
			write(1,str,strlen(str));
			write(1,"\n",1);
			free(str);
		} else {
			break;
		}
		res = rl_readline(rl, rl->buf,rl->buf_size);
	} 
	
	for (i = 0; i < argc -1; i++){
		free(patterns[i]);
		free(repls[i]);
	}
	free(patterns);
	free(repls);
	free(greeds);
	return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]) {
    int len = atoi(argv[1]);
    RL *rl = rl_open(0, len);
    char *buf = (char *) malloc(len + 1);
    
    while (1) {
        int rc = rl_readline(rl, buf, len);
        if (rc == 0)
            break;
        else if (rc > 0) {
            write_line(buf, rc);
        }
    }
    free(buf);
    rl_close(rl);
    return 0;
}
Exemplo n.º 3
0
int main(int argc, char** argv) {
    if (argc != 2) {
        printf("Usage: readlines-main <max_size>\n");
        return 0;
    }

    size_t max_size = atoi(argv[1]);

    RL *rl = rl_open(0, max_size);

    char *buf = malloc(max_size + 1);

    ssize_t read = 0;

    while ((read = rl_readline(rl, buf, max_size + 1)) != 0) {
        if (read == -1) {
            fprintf(stderr, "Error!\n");
            break;
        }

        if (read == -2) {
            fprintf(stderr, "The line does not fit in the buffer\n");
            continue;
        }

        if (read == -3) {
            fprintf(stderr, "Line is too long\n");
            continue;
        }

        size_t written = 0;

        while (written < read)
            written += write(1, &buf[written], read - written);
    }

    rl_close(rl);

    return 0;
}
Exemplo n.º 4
0
TEST rl_open_oom()
{
	int j, i, retval;
	rlite *db = NULL;
	for (j = 0; j < 2; j++) {
		for (i = 1; ; i++) {
			test_mode = 1;
			test_mode_counter = i;
			retval = rl_open(j == 0 ? ":memory:" : "rlite-test.rld", &db, RLITE_OPEN_CREATE | RLITE_OPEN_READWRITE);
			if (retval == RL_OK) {
				if (i == 1) {
					fprintf(stderr, "No OOM triggered\n");
					test_mode = 0;
					FAIL();
				}
				rl_close(db);
				break;
			}
			EXPECT_INT(retval, RL_OUT_OF_MEMORY);
		}
	}
	test_mode = 0;
	PASS();
}