Skip to content

Instantly share code, notes, and snippets.

@uucky
Created December 8, 2017 20:57
Show Gist options
  • Save uucky/94260afbdd0321ca0486829030245a68 to your computer and use it in GitHub Desktop.
Save uucky/94260afbdd0321ca0486829030245a68 to your computer and use it in GitHub Desktop.
unix a7
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "ensureCap.h"
int size;
char* end;
char* init(int argc, char *argv[]) {
if(argc == 2) {
size = atoi(argv[1]); //atoi: array to int
end = malloc(size + 1);
if(end) { //if malloc succeed
memset(end, '\n', size); //put 'size' numbers of '\n' into end
end[size] = '\0'; //set the last value of end to be \0
return NULL;
} else {
return "Failed to malloc memory.\n";
}
} else {
return "Needs one argument: num\n";
}
}
void process(char **line, ssize_t *num, size_t *len) {
(*line)[(*num) -1] = '\0';
*num = (*num) -1 + size;
ensurecapacity(line, len, (*num) + 1);
strcat(*line, end);
}
void finalize(char **line, ssize_t *num, size_t *len) {
free(end);
end = NULL;
*num = 0;
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "main.h"
#include "ensureCap.h"
char* init(int argc, char *argv[]) {
return NULL;
}
// *num: how many char in **line. not include \0
// *len: the length of **line.
// *len: line里 一共可以用的长度(len的大小),char array 的容量。
// *num: line里内容的长度。
void process(char **line, ssize_t *num, size_t *len) {
char temp[(*num) + 1]; //
memcpy(temp,*line,(*num) + 1); //make copy of the input
size_t newCap = ((*num) * 2);
//printf("newCap = %zu\n",newCap);
ensurecapacity(line, len, newCap);
//update *num
*num = newCap;
int i = 0;//int for counting position
//loop for copy every char twic
for(; temp[i] != '\0'; i++){
(*line)[i*2] = temp[i];
(*line)[i*2 + 1] = temp[i];
//printf("%s\n = %i\n",*line, i);
}
//delete duplicated newline mark
//and add '\0'
(*line)[i*2 - 1] = '\0';
}
void finalize(char **line, ssize_t *num, size_t *len) {
*num = 0;
}
#include <stdlib.h>
#include <string.h>
#include "main.h"
int number;
int counter = 0;
char* init(int argc, char *argv[]) {
if(argc == 2) {
number = atoi(argv[1]);
return NULL;
} else {
return "Needs one argument: num\n";
}
}
void process(char **line, ssize_t *num, size_t *len) {
if(counter < number){
counter++;
return;
}
else{
*num = 0;
}
}
void finalize(char **line, ssize_t *num, size_t *len){
*num = 0;
}
#include <stdlib.h>
#include <string.h>
#include "main.h"
char* init(int argc, char *argv[]) {
return NULL;
}
void process(char **line, ssize_t *num, size_t *len) {
char temp;
for (int i = 0; i < (*num) / 2; i++){
temp = (*line)[i];
(*line)[i] = (*line)[(*num)-i-2];
(*line)[(*num)-i-2] = temp;
}
}
void finalize(char **line, ssize_t *num, size_t *len){
*num = 0;
}
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "ensureCap.h"
typedef struct {
char* line;
size_t size;
} lineSize;
lineSize* start;
lineSize* next;
lineSize* end;
char* init(int argc, char *argv[]) {
if(argc == 2) {
const int num = atoi(argv[1]);
if(num > 0) {
start = calloc(num, sizeof(lineSize));
if(!start) {
return "Error (c)allocating memory.\n";
}
} else {
start = NULL;
}
next = start;
end = start + num;
return NULL;
} else {
return "Needs an argument: num\n";
}
}
void process(char **line, ssize_t *num, size_t *len) {
if(start) {
ensurecapacity(&(next->line), &(next->size), (*num) +1);
memcpy(next->line, *line, (*num) +1);
if(++next == end) {
next = start;
}
}
*num = 0;
}
void finalize(char **line, ssize_t *num, size_t *len) {
*num = 0;
if(!start) { return; }
for(lineSize* tmp = start; tmp < end; tmp++) {
if(tmp->line) {
*num += strlen(tmp->line);
}
}
ensurecapacity(line, len, *num +1);
**line = '\0';
lineSize* last = next;
do {
if(next->line) {
strcat(*line, next->line);
}
if(++next == end) {
next = start;
}
} while(next != last);
free(start);
start = next = end = NULL;
}
#include "main.h"
char victim;
char replacement;
char* init(int argc, char *argv[]) {
if(argc == 3) {
victim = *(argv[1]);
replacement = *(argv[2]);
return NULL;
} else {
return "need two argument: victim replacement\n";
}
}
void process(char **line, ssize_t *num, size_t *len) {
char *pos = *line;
char* end = pos + *num;
for(; pos < end; pos++) {
if (*pos == victim) {
*pos = replacement;
}
}
}
void finalize(char **line, ssize_t *num, size_t *len){
*num = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment