Skip to content

Instantly share code, notes, and snippets.

@moomdate
Created July 18, 2018 10:31
Show Gist options
  • Select an option

  • Save moomdate/6704a759654dc7f7254c8d425028cbec to your computer and use it in GitHub Desktop.

Select an option

Save moomdate/6704a759654dc7f7254c8d425028cbec to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#define lineLR 2 // 0xa,0d
#define maxSizeInLine 41
#define sectorSize maxSizeInLine*4
#define maxLine 97
char data[maxLine][maxSizeInLine]; // line * char num
char buffer;
int maxString = 50;
int currentIndex = 0;
int currentLine = 0;
int beginRead = 0;
int command = 0;
bool switch_ = 0;
//------------------prototype------------------
char *queryFromRom(void);
void doCommand(int cc);
int insertString(char *str,int index);
int countStr(char *data);
void printStringInLine(char *str);
int getMaxLine();
void append(char subject[], const char insert[], int pos);
void removeChar(char *str,int idxToDel );
bool switchLang();
//---------------------------------------------
char str__[] = "assssssssssssss";
int main()
{
do{
printf("Enter your command (99 help):");
scanf("%d",&command);
//if(command>=32&&command<=97+32){
doCommand(command);
//}
}while(1);
return 0;
}
void append(char subject[], const char insert[], int pos) {
char buf[100] = {}; // 100 so that it's big enough. fill with zeros
strncpy(buf, subject, pos); // copy at most first pos characters
int len = strlen(buf);
strcpy(buf+len, insert); // copy all of insert[] at the end
len += strlen(insert); // increase the length by length of insert[]
strcpy(buf+len, subject+pos);
strcpy(subject, buf);
}
void doCommand(int cc){
char buffer[42];
int index_ = 0;
switch (cc){
case 0:
printf("strings:%s\r\nLength:%d\r\n",data);
break;
case 1:
if(currentLine>0){
printf("left line at (%d)\r\n",currentLine);
currentLine--;
}else{
printf("left line at 0\r\n");
}
break;
case 2:
currentLine++;
printf("right line(%d)\r\n",currentLine);
break;
case 3:
printf("up\r\n");
break;
case 4:
printf("down\r\n");
break;
case 5:
printf("enter string:");
scanf("%[^\t]",buffer);
printf("insert string at index:");
scanf("%d",&index_);
append(data[currentLine],buffer,index_);
/*if(insertString(buffer,index_)==1){
printf("insert success\r\n");
}*/
break;
case 6:
printf("enter index your need remove:");
scanf("%d",&index_);
removeChar(data[currentLine],index_);
break;
case 7:
printf("Switch lang\r\n");
printf("------%d------\r\n",switchLang());
printf("//////////////\r\n");
break;
case 97:
printf("------------%d----------\r\n",getMaxLine());
break;
case 98:
printf("---view string in current line---\r\n");
printStringInLine(data[currentLine]);
printf("=================================\r\n");
break;
case 99:
printf("=================\r\n");
printf("0:view string\r\n1:left\r\n2:right\r\n3:up\r\n4:down\r\n5:insert string\r\n6 char remove at index\r\n7:switch lang\r\n97:total line\r\n98:view string in current line\r\n");
printf("");
printf("=================\r\n");
break;
}
}
bool switchLang(){
if(switch_)
switch_ = !switch_;
else
switch_ = !switch_;
return switch_;
}
int insertString(char *str,int index){
char buffer___l[maxSizeInLine]; //22
int cc__ = index;
// printf("%d",countStr(data[currentLine]));
if(countStr(data[currentLine])>0){
while(cc__<maxSizeInLine){
buffer___l[cc__-index] = data[currentLine][index];
cc__++;
}
printf("----\r\n buffer is: %s",buffer___l);
} // end store last string
cc__ = index;
while(cc__<index+strlen(str)){
data[currentLine][cc__] = str[cc__-index];
cc__++;
}
printf("string ss is : ");
printStringInLine(data[currentLine]);
return 1;
}
void printStringInLine(char *str){
int aaa = 0;
while(aaa<maxSizeInLine){
printf("%c",str[aaa]);
aaa++;
}
printf("\r\n");
}
int getMaxLine(){
int cc = 0;
int ss = 0;
while(cc<maxLine){
if(strlen(data[cc])){
ss++;
}
cc++;
}
return ss;
}
int countStr(char *data){
int cc__ = 0;
int cc = 0;
if(strlen(data)>0){
while(data[cc__]!='\0' || cc__<42){
cc++;
}
}
return cc;
}
void removeChar(char *str,int idxToDel ){
memmove(&str[idxToDel], &str[idxToDel + 1], strlen(str) - idxToDel);
}
char *queryFromRom(int comm)
{
switch (comm){
case 1: //left
break;
case 2: //right
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment