提示コードですが改行関数と文字と文字の間に文字を挿入する関数を作成したのですがこれだと下記の問題点のよな問題が発生するのですがどういったやり方をするのが定番なのでしょうか?
問題点
このような文字列の操作をしているとカッコの自動挿入や補填やシンタックスハイライトなど実装がむずかしくなる
知りたいこと
テキストエディタの作成でどう文字列をいじればいいか知りたい
cpp
1#include "MainScreen.hpp" 2#include <Magick++.h> 3#include "memory.h" 4#include "../../LibConsole/lib_src/Console.hpp" 5#include <ncurses.h> 6#include <iostream> 7#include <filesystem> 8#define CTRL(x) ((x) & 0x1f) 9#define LINE_BUFFER ((int)1000) 10#define byte char 11 12 13MainScreen::MainScreen() 14{ 15 setlocale(LC_ALL, ""); //日本語利用 16 17 //cbreak(); 18 //nocbreak(); 19 //noecho(); 20// echo(); 21 GetFileData(); 22 23cursorPosition = glm::ivec2(0,0); 24 25logo = std::string(R"( 26 ______ __ 27/_ __/____ __/ /_ 28 / / / -_) \ / __/ 29/_/ \__/_\_\\__/ ___ __ 30 / __/__/ (_) /____ ____ 31 / _// _ / / __/ _ \/ __/ 32 /___/\_,_/_/\__/\___/_/ 33 34)"); 35 36} 37 38std::string MainScreen::DragAndDrop() 39{ 40 std::string p = Console::InputString(); 41 42 if(p.length() > 3) 43 { 44 p.erase(0,1); 45 p.pop_back(); 46 p.pop_back(); 47 48 return p; 49 } 50 else 51 { 52 return std::string(""); 53 } 54 55 56 return std::string(""); 57} 58 59 60bool MainScreen::getExit() 61{ 62 return isExit; 63} 64 65void MainScreen::GetFileData() 66{ 67 std::string fileName = "Main.cpp"; 68 FILE *fp = fopen(fileName.c_str(),"a+"); 69 70 71 if(fp != NULL) 72 { 73 char str[LINE_BUFFER]; 74 75 while(fgets(str,LINE_BUFFER,fp) != NULL) 76 { 77 file.push_back(new char[LINE_BUFFER]); 78 //file.push_back(str); 79 80 memcpy(file.back(),str,LINE_BUFFER); 81 } 82 83 } 84 else 85 { 86 87 } 88 89 for(int i = 0; i < file.size(); i++) 90 { 91 Console::Draw(Console::ColorCode::White,Console::ColorCode::Black,nullptr,"%s",file.at(i)); 92 } 93} 94 95void MainScreen::LineSpacing(int key) 96{ 97 char str[LINE_BUFFER] = {'\0'}; 98 int t = strlen(file.at(cursorPosition.y)); 99 int len = t - cursorPosition.x; //文字列の長さ 100 101 for(int i = 0; i < len; i++) 102 { 103 str[i] = file.at(cursorPosition.y)[cursorPosition.x + i]; 104 } 105 106 file.at(cursorPosition.y)[strlen(file.at(cursorPosition.y)) + 1] = '\0'; 107 file.at(cursorPosition.y)[strlen(file.at(cursorPosition.y)) + 2] = '\n'; 108 file.at(cursorPosition.y)[cursorPosition.x] = (char)key; 109 cursorPosition.x += 1; 110 111 Console::Debug::WriteLog("aaa %s\n",str); 112 113 for(int i = 0; i < strlen(str); i++) 114 { 115 file.at(cursorPosition.y)[cursorPosition.x + i] = str[i]; 116 } 117 118} 119 120void MainScreen::NewLine() 121{ 122 char str[LINE_BUFFER] = {'\0'}; 123 int t = strlen(file.at(cursorPosition.y)); 124 int len = t - cursorPosition.x; //文字列の長さ 125 126 for(int i = 0; i < len; i++) 127 { 128 str[i] = file.at(cursorPosition.y)[cursorPosition.x + i]; 129 } 130 str[len] = '\0'; 131 132 for(int i = 0; i < t; i++) 133 { 134 file.at(cursorPosition.y)[cursorPosition.x + i] = '\0'; 135 } 136 137 file.at(cursorPosition.y)[strlen(file.at(cursorPosition.y))] = '\n'; //改行挿入 138 139 std::vector<char*> line; 140 for(int i = 0; i < (file.size() - cursorPosition.y); i++) 141 { 142 line.push_back(new char[LINE_BUFFER]); 143 strcpy(line.back(),file.at(cursorPosition.y + i)); 144 } 145 146 int total = (file.size() - cursorPosition.y); 147 file.push_back(new char[LINE_BUFFER]); 148 for(int i = 0; i < line.size(); i++) 149 { 150 strcpy(file.at(cursorPosition.y + i + 1),line.at(i)); 151 } 152 153 for(int i = 0; i < strlen(str); i++) 154 { 155 file.at(cursorPosition.y + 1)[i] = str[i]; 156 } 157 158 //file.at(cursorPosition.y) = "\0"; 159 cursorPosition.x = 0; 160 cursorPosition.y += 1; 161} 162 163 164void MainScreen::Loop() 165{ 166 Console::ClearBuffer(NULL); 167 168 for(int i = 0; i < file.size(); i++) 169 { 170 Console::Draw(Console::ColorCode::White,Console::ColorCode::Black,nullptr,"%s",file.at(i)); 171 } 172 173 174 move(cursorPosition.y,cursorPosition.x); 175 int key = getch(); 176 if(key == KEY_ESC) 177 { 178 isExit = true; 179 } 180 else if( key == KEY_DOWN) 181 { 182 cursorPosition.y += 1; 183 } 184 else if( key == KEY_UP) 185 { 186 cursorPosition.y += -1; 187 } 188 else if( key == KEY_RIGHT) 189 { 190 cursorPosition.x += 1; 191 } 192 else if( key == KEY_LEFT) 193 { 194 cursorPosition.x += -1; 195 } 196 else if( key == KEY_RETURN) 197 { 198 //Console::Debug::WriteLog("qqqq \n"); 199 200 NewLine(); 201 } 202 else if( key == CTRL('x') ) 203 { 204 Console::Debug::WriteLog("Save\n"); 205 } 206 else if( (byte)(key << 4) & (byte)KEY_BACKSPACE ) 207 { 208 Console::Debug::WriteLog("KEY_BACKSPACE \n"); 209 210 } 211 else 212 { 213 //Console::Debug::WriteLog("ああああああ \n"); 214 215 LineSpacing(key); 216 } 217 218 219 //Console::Debug::WriteLog("KEY_BACKSPACE \n"); 220 221} 222 223void MainScreen::SetFilePath(std::filesystem::path p) 224{ 225 for (const std::filesystem::directory_entry& x : std::filesystem::recursive_directory_iterator(p.native())) 226 { 227 if(std::filesystem::is_directory(x.path()) == false) 228 { 229 Console::Draw(Console::ColorCode::White,Console::ColorCode::Black,nullptr,"path %s\n",x.path().c_str()); 230 filePath.push_back(std::string(x.path().c_str())); 231 } 232 } 233 234// Console::Draw(Console::ColorCode::White,Console::ColorCode::Black,nullptr,"path \\n"); 235 236} 237 238 239 240MainScreen::~MainScreen() 241{ 242 for(int i = 0; i< file.size(); i++) 243 { 244 free(file.at(i)); 245 } 246} 247
あなたの回答
tips
プレビュー