Browse Source

Add syntax highlighting

merge-requests/365/head
Jørgen Lien Sellæg 11 years ago
parent
commit
b2f2ccb388
  1. 1
      .gitignore
  2. 38
      juci/CMakeLists.txt
  3. 373
      juci/config.json
  4. 2
      juci/notebook.cc
  5. 64
      juci/source.cc
  6. 16
      juci/source.h
  7. 92
      juci/sourcefile.cc
  8. 23
      juci/sourcefile.h

1
.gitignore vendored

@ -8,3 +8,4 @@
!*.cc
!*.h
!CMakeLists.txt
!config.json

38
juci/CMakeLists.txt

@ -5,9 +5,19 @@ set(module juci_to_python_api)
project (${project_name})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/")
INCLUDE(FindPkgConfig)
message("Searcing for libclang")
#LIBCLANG_FOUND System has libclang.
#LIBCLANG_INCLUDE_DIRS The libclang include directories.
#LIBCLANG_LIBRARIES The libraries needed to use libclang.
#LIBCLANG_LIBRARY_DIR The path to the directory containing libclang.
#LIBCLANG_KNOWN_LLVM_VERSIONS Known LLVM release numbers.
find_package(Testlcl)
#### Finding boost, the variables below is set #####
#PYTHONLIBS_FOUND - True if headers and requested libraries were found
#PYTHON_INCLUDE_DIRS - Boost include directories
@ -59,7 +69,6 @@ else()
message("Gtkmm link dirs ${GTKMM_LIBRARIES}")
message(FATAL_ERROR "The gtkmm libraries are required. Quitting.")
endif()
# name of the executable on Windows will be example.exe
add_executable(${project_name}
#list of every needed file to create the executable
@ -67,6 +76,8 @@ add_executable(${project_name}
keybindings
menu
source
sourcefile.h
sourcefile.cc
window
api
notebook
@ -81,11 +92,30 @@ add_library(${module} SHARED
)
# dependencies
include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS} "/home/zalox/bachelor/libclang++/")
link_directories(${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS} ${PYTHON_INCLUDE_DIRS} "/home/zalox/bachelor/libclang++/")
if(${lcl_NOTFOUND})
message("FATAL_ERROR ohh lord!")
else()
message("Found libs!")
endif()
include_directories(
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
${GTKMM_INCLUDE_DIRS}
"/home/zalox/bachelor/libclang++/headers/"
)
link_directories(
${GTKMM_LIBRARY_DIRS}
${Boost_LIBRARY_DIRS}
${PYTHON_INCLUDE_DIRS}
${LCL_LIBRARY_DIRS}
)
#module:
set_target_properties(${module} PROPERTIES PREFIX "")
target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
#executable:
target_link_libraries(${project_name} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} "/home/zalox/bachelor/libclang++/")
target_link_libraries(${project_name} ${LCL_LIBRARIES} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

373
juci/config.json

@ -1,192 +1,193 @@
{
"colors": {
"text_color": ["black", "standard text color"],
"string": ["green", "string text color"]
"text_color": "black",
"string" : "green"
},
"syntax": {
"1": ["text_color","CXCursor_UnexposedDecl"],
"2": ["text_color","CXCursor_StructDecl"],
"3": ["text_color","CXCursor_UnionDecl"],
"4": ["text_color","CXCursor_ClassDecl"],
"5": ["text_color","CXCursor_EnumDecl"],
"6": ["text_color","CXCursor_FieldDecl"],
"7": ["text_color","CXCursor_EnumConstantDecl"],
"8": ["text_color","CXCursor_FunctionDecl"],
"9": ["text_color","CXCursor_VarDecl"],
"10": ["text_color","CXCursor_ParmDecl"],
"11": ["text_color","CXCursor_ObjCInterfaceDecl"],
"12": ["text_color","CXCursor_ObjCCategoryDecl"],
"13": ["text_color","CXCursor_ObjCProtocolDecl"],
"14": ["text_color","CXCursor_ObjCPropertyDecl"],
"15": ["text_color","CXCursor_ObjCIvarDecl"],
"16": ["text_color","CXCursor_ObjCInstanceMethodDecl"],
"17": ["text_color","CXCursor_ObjCClassMethodDecl"],
"18": ["text_color","CXCursor_ObjCImplementationDecl"],
"19": ["text_color","CXCursor_ObjCCategoryImplDecl"],
"20": ["text_color","CXCursor_TypedefDecl"],
"21": ["text_color","CXCursor_CXXMethod"],
"22": ["text_color","CXCursor_Namespace"],
"23": ["text_color","CXCursor_LinkageSpec"],
"24": ["text_color","CXCursor_Constructor"],
"25": ["text_color","CXCursor_Destructor"],
"26": ["text_color","CXCursor_ConversionFunction"],
"27": ["text_color","CXCursor_TemplateTypeParameter"],
"28": ["text_color","CXCursor_NonTypeTemplateParameter"],
"29": ["text_color","CXCursor_TemplateTemplateParameter"],
"30": ["text_color","CXCursor_FunctionTemplate"],
"31": ["text_color","CXCursor_ClassTemplate"],
"32": ["text_color","CXCursor_ClassTemplatePartialSpecialization"],
"33": ["text_color","CXCursor_NamespaceAlias"],
"34": ["text_color","CXCursor_UsingDirective"],
"35": ["text_color","CXCursor_UsingDeclaration"],
"36": ["text_color","CXCursor_TypeAliasDecl"],
"37": ["text_color","CXCursor_ObjCSynthesizeDecl"],
"38": ["text_color","CXCursor_ObjCDynamicDecl"],
"39": ["text_color","CXCursor_CXXAccessSpecifier"],
"40": ["text_color","CXCursor_FirstRef"],
"41": ["text_color","CXCursor_ObjCSuperClassRef"],
"42": ["text_color","CXCursor_ObjCProtocolRef"],
"43": ["text_color","CXCursor_ObjCClassRef"],
"44": ["text_color","CXCursor_TypeRef"],
"45": ["text_color","CXCursor_CXXBaseSpecifier"],
"46": ["text_color","CXCursor_TemplateRef"],
"47": ["text_color","CXCursor_NamespaceRef"],
"48": ["text_color","CXCursor_MemberRef"],
"49": ["text_color","CXCursor_LabelRef"],
"50": ["text_color","CXCursor_OverloadedDeclRef"],
"51": ["text_color","CXCursor_VariableRef"],
"70": ["text_color","CXCursor_FirstInvalid"],
"70": ["text_color","CXCursor_InvalidFile"],
"71": ["text_color","CXCursor_NoDeclFound"],
"72": ["text_color","CXCursor_NotImplemented"],
"73": ["text_color","CXCursor_InvalidCode"],
"100": ["text_color","CXCursor_FirstExpr"],
"100": ["text_color","CXCursor_UnexposedExpr"],
"101": ["text_color","CXCursor_DeclRefExpr"],
"102": ["text_color","CXCursor_MemberRefExpr"],
"103": ["text_color","CXCursor_CallExpr"],
"104": ["text_color","CXCursor_ObjCMessageExpr"],
"105": ["text_color","CXCursor_BlockExpr"],
"106": ["text_color","CXCursor_IntegerLiteral"],
"107": ["text_color","CXCursor_FloatingLiteral"],
"108": ["text_color","CXCursor_ImaginaryLiteral"],
"109": ["string","CXCursor_StringLiteral"],
"110": ["text_color","CXCursor_CharacterLiteral"],
"111": ["text_color","CXCursor_ParenExpr"],
"112": ["text_color","CXCursor_UnaryOperator"],
"113": ["text_color","CXCursor_ArraySubscriptExpr"],
"114": ["text_color","CXCursor_BinaryOperator"],
"115": ["text_color","CXCursor_CompoundAssignOperator"],
"116": ["text_color","CXCursor_ConditionalOperator"],
"117": ["text_color","CXCursor_CStyleCastExpr"],
"118": ["text_color","CXCursor_CompoundLiteralExpr"],
"119": ["text_color","CXCursor_InitListExpr"],
"120": ["text_color","CXCursor_AddrLabelExpr"],
"121": ["text_color","CXCursor_StmtExpr"],
"122": ["text_color","CXCursor_GenericSelectionExpr"],
"123": ["text_color","CXCursor_GNUNullExpr"],
"124": ["text_color","CXCursor_CXXStaticCastExpr"],
"125": ["text_color","CXCursor_CXXDynamicCastExpr"],
"126": ["text_color","CXCursor_CXXReinterpretCastExpr"],
"127": ["text_color","CXCursor_CXXConstCastExpr"],
"128": ["text_color","CXCursor_CXXFunctionalCastExpr"],
"129": ["text_color","CXCursor_CXXTypeidExpr"],
"130": ["text_color","CXCursor_CXXBoolLiteralExpr"],
"131": ["text_color","CXCursor_CXXNullPtrLiteralExpr"],
"132": ["text_color","CXCursor_CXXThisExpr"],
"133": ["text_color","CXCursor_CXXThrowExpr"],
"134": ["text_color","CXCursor_CXXNewExpr"],
"135": ["text_color","CXCursor_CXXDeleteExpr"],
"136": ["text_color","CXCursor_UnaryExpr"],
"137": ["text_color","CXCursor_ObjCStringLiteral"],
"138": ["text_color","CXCursor_ObjCEncodeExpr"],
"139": ["text_color","CXCursor_ObjCSelectorExpr"],
"140": ["text_color","CXCursor_ObjCProtocolExpr"],
"141": ["text_color","CXCursor_ObjCBridgedCastExpr"],
"142": ["text_color","CXCursor_PackExpansionExpr"],
"143": ["text_color","CXCursor_SizeOfPackExpr"],
"144": ["text_color","CXCursor_LambdaExpr"],
"145": ["text_color","CXCursor_ObjCBoolLiteralExpr"],
"146": ["text_color","CXCursor_ObjCSelfExpr"],
"200": ["text_color","CXCursor_FirstStmt"],
"200": ["text_color","CXCursor_UnexposedStmt"],
"201": ["text_color","CXCursor_LabelStmt"],
"202": ["text_color","CXCursor_CompoundStmt"],
"203": ["text_color","CXCursor_CaseStmt"],
"204": ["text_color","CXCursor_DefaultStmt"],
"205": ["text_color","CXCursor_IfStmt"],
"206": ["text_color","CXCursor_SwitchStmt"],
"207": ["text_color","CXCursor_WhileStmt"],
"208": ["text_color","CXCursor_DoStmt"],
"209": ["text_color","CXCursor_ForStmt"],
"210": ["text_color","CXCursor_GotoStmt"],
"211": ["text_color","CXCursor_IndirectGotoStmt"],
"212": ["text_color","CXCursor_ContinueStmt"],
"213": ["text_color","CXCursor_BreakStmt"],
"214": ["text_color","CXCursor_ReturnStmt"],
"215": ["text_color","CXCursor_GCCAsmStmt"],
"216": ["text_color","CXCursor_ObjCAtTryStmt"],
"217": ["text_color","CXCursor_ObjCAtCatchStmt"],
"218": ["text_color","CXCursor_ObjCAtFinallyStmt"],
"219": ["text_color","CXCursor_ObjCAtThrowStmt"],
"220": ["text_color","CXCursor_ObjCAtSynchronizedStmt"],
"221": ["text_color","CXCursor_ObjCAutoreleasePoolStmt"],
"222": ["text_color","CXCursor_ObjCForCollectionStmt"],
"223": ["text_color","CXCursor_CXXCatchStmt"],
"224": ["text_color","CXCursor_CXXTryStmt"],
"225": ["text_color","CXCursor_CXXForRangeStmt"],
"226": ["text_color","CXCursor_SEHTryStmt"],
"227": ["text_color","CXCursor_SEHExceptStmt"],
"228": ["text_color","CXCursor_SEHFinallyStmt"],
"229": ["text_color","CXCursor_MSAsmStmt"],
"230": ["text_color","CXCursor_NullStmt"],
"231": ["text_color","CXCursor_DeclStmt"],
"232": ["text_color","CXCursor_OMPParallelDirective"],
"233": ["text_color","CXCursor_OMPSimdDirective"],
"234": ["text_color","CXCursor_OMPForDirective"],
"235": ["text_color","CXCursor_OMPSectionsDirective"],
"236": ["text_color","CXCursor_OMPSectionDirective"],
"237": ["text_color","CXCursor_OMPSingleDirective"],
"238": ["text_color","CXCursor_OMPParallelForDirective"],
"239": ["text_color","CXCursor_OMPParallelSectionsDirective"],
"240": ["text_color","CXCursor_OMPTaskDirective"],
"241": ["text_color","CXCursor_OMPMasterDirective"],
"242": ["text_color","CXCursor_OMPCriticalDirective"],
"243": ["text_color","CXCursor_OMPTaskyieldDirective"],
"244": ["text_color","CXCursor_OMPBarrierDirective"],
"245": ["text_color","CXCursor_OMPTaskwaitDirective"],
"246": ["text_color","CXCursor_OMPFlushDirective"],
"247": ["text_color","CXCursor_SEHLeaveStmt"],
"248": ["text_color","CXCursor_OMPOrderedDirective"],
"249": ["text_color","CXCursor_OMPAtomicDirective"],
"250": ["text_color","CXCursor_OMPForSimdDirective"],
"251": ["text_color","CXCursor_OMPParallelForSimdDirective"],
"252": ["text_color","CXCursor_OMPTargetDirective"],
"253": ["text_color","CXCursor_OMPTeamsDirective"],
"300": ["text_color","CXCursor_TranslationUnit"],
"400": ["text_color","CXCursor_FirstAttr"],
"400": ["text_color","CXCursor_UnexposedAttr"],
"401": ["text_color","CXCursor_IBActionAttr"],
"402": ["text_color","CXCursor_IBOutletAttr"],
"403": ["text_color","CXCursor_IBOutletCollectionAttr"],
"404": ["text_color","CXCursor_CXXFinalAttr"],
"405": ["text_color","CXCursor_CXXOverrideAttr"],
"406": ["text_color","CXCursor_AnnotateAttr"],
"407": ["text_color","CXCursor_AsmLabelAttr"],
"408": ["text_color","CXCursor_PackedAttr"],
"409": ["text_color","CXCursor_PureAttr"],
"410": ["text_color","CXCursor_ConstAttr"],
"411": ["text_color","CXCursor_NoDuplicateAttr"],
"412": ["text_color","CXCursor_CUDAConstantAttr"],
"413": ["text_color","CXCursor_CUDADeviceAttr"],
"414": ["text_color","CXCursor_CUDAGlobalAttr"],
"415": ["text_color","CXCursor_CUDAHostAttr"],
"416": ["text_color","CXCursor_CUDASharedAttr"],
"500": ["text_color","CXCursor_PreprocessingDirective"],
"501": ["text_color","CXCursor_MacroDefinition"],
"502": ["text_color","CXCursor_MacroExpansion"],
"503": ["text_color","CXCursor_InclusionDirective"],
"600": ["text_color","CXCursor_ModuleImportDecl"],
"700": ["text_color","CXCursor_OverloadCandidate"]
"1": "text_color",
"2": "text_color",
"2": "text_color",
"3": "text_color",
"4": "text_color",
"5": "text_color",
"6": "text_color",
"7": "text_color",
"8": "text_color",
"9": "text_color",
"10": "text_color",
"11": "text_color",
"12": "text_color",
"13": "text_color",
"14": "text_color",
"15": "text_color",
"16": "text_color",
"17": "text_color",
"18": "text_color",
"19": "text_color",
"20": "text_color",
"21": "text_color",
"22": "text_color",
"23": "text_color",
"24": "text_color",
"25": "text_color",
"26": "text_color",
"27": "text_color",
"28": "text_color",
"29": "text_color",
"30": "text_color",
"31": "text_color",
"32": "text_color",
"33": "text_color",
"34": "text_color",
"35": "text_color",
"36": "text_color",
"37": "text_color",
"38": "text_color",
"39": "text_color",
"40": "text_color",
"41": "text_color",
"42": "text_color",
"43": "text_color",
"44": "text_color",
"45": "text_color",
"46": "text_color",
"47": "text_color",
"48": "text_color",
"49": "text_color",
"50": "text_color",
"51": "text_color",
"70": "text_color",
"70": "text_color",
"71": "text_color",
"72": "text_color",
"73": "text_color",
"100": "text_color",
"100": "text_color",
"101": "text_color",
"102": "text_color",
"103": "text_color",
"104": "text_color",
"105": "text_color",
"106": "text_color",
"107": "text_color",
"108": "text_color",
"109": "string",
"110": "text_color",
"111": "text_color",
"112": "text_color",
"113": "text_color",
"114": "text_color",
"115": "text_color",
"116": "text_color",
"117": "text_color",
"118": "text_color",
"119": "text_color",
"120": "text_color",
"121": "text_color",
"122": "text_color",
"123": "text_color",
"124": "text_color",
"125": "text_color",
"126": "text_color",
"127": "text_color",
"128": "text_color",
"129": "text_color",
"130": "text_color",
"131": "text_color",
"132": "text_color",
"133": "text_color",
"134": "text_color",
"135": "text_color",
"136": "text_color",
"137": "text_color",
"138": "text_color",
"139": "text_color",
"140": "text_color",
"141": "text_color",
"142": "text_color",
"143": "text_color",
"144": "text_color",
"145": "text_color",
"146": "text_color",
"200": "text_color",
"200": "text_color",
"201": "text_color",
"202": "text_color",
"203": "text_color",
"204": "text_color",
"205": "text_color",
"206": "text_color",
"207": "text_color",
"208": "text_color",
"209": "text_color",
"210": "text_color",
"211": "text_color",
"212": "text_color",
"213": "text_color",
"214": "text_color",
"215": "text_color",
"216": "text_color",
"217": "text_color",
"218": "text_color",
"219": "text_color",
"220": "text_color",
"221": "text_color",
"222": "text_color",
"223": "text_color",
"224": "text_color",
"225": "text_color",
"226": "text_color",
"227": "text_color",
"228": "text_color",
"229": "text_color",
"230": "text_color",
"231": "text_color",
"232": "text_color",
"233": "text_color",
"234": "text_color",
"235": "text_color",
"236": "text_color",
"237": "text_color",
"238": "text_color",
"239": "text_color",
"240": "text_color",
"241": "text_color",
"242": "text_color",
"243": "text_color",
"244": "text_color",
"245": "text_color",
"246": "text_color",
"247": "text_color",
"248": "text_color",
"249": "text_color",
"250": "text_color",
"251": "text_color",
"252": "text_color",
"253": "text_color",
"300": "text_color",
"400": "text_color",
"400": "text_color",
"401": "text_color",
"402": "text_color",
"403": "text_color",
"404": "text_color",
"405": "text_color",
"406": "text_color",
"407": "text_color",
"408": "text_color",
"409": "text_color",
"410": "text_color",
"411": "text_color",
"412": "text_color",
"413": "text_color",
"414": "text_color",
"415": "text_color",
"416": "text_color",
"500": "text_color",
"501": "text_color",
"502": "text_color",
"503": "text_color",
"600": "text_color",
"700": "text_color"
}
}

2
juci/notebook.cc

@ -14,6 +14,7 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){
scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow());
source_vec_.push_back(new Source::Controller);
scrolledwindow_vec_.back()->add(source_vec_.back()->view());
source_vec_.back()->OnNewEmptyFile();
view_.notebook().append_page(*scrolledwindow_vec_.back(), "juCi++");
@ -71,6 +72,7 @@ void Notebook::Controller::OnNewPage(std::string name) {
scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow());
source_vec_.push_back(new Source::Controller);
scrolledwindow_vec_.back()->add(source_vec_.back()->view());
source_vec_.back()->OnNewEmptyFile();
view_.notebook().append_page(*scrolledwindow_vec_.back(), name);
view_.notebook().show_all_children();
view_.notebook().set_focus_child(*scrolledwindow_vec_.back());

64
juci/source.cc

@ -1,5 +1,8 @@
#include "source.h"
#include <iostream>
#include "sourcefile.h"
#include <boost/property_tree/json_parser.hpp>
#include <fstream>
//////////////
//// View ////
@ -12,11 +15,10 @@ Source::View::View() {
string Source::View::UpdateLine() {
Gtk::TextIter line(get_buffer()->get_insert()->get_iter());
// for each word --> check what it is --> apply appropriate tag
// retUrn "";
return "";
}
String Source::View::Getline(const Gtk::TextIter &begin) {
string Source::View::GetLine(const Gtk::TextIter &begin) {
Gtk::TextIter end(begin);
while (!end.ends_line())
end++;
@ -31,12 +33,28 @@ void Source::View::ApplyTheme(const Source::Theme &theme) {
}
}
void Source::View::OnOpenFile(std::vector<Clang::SourceLocation> &locations,
const Source::Theme &theme) {
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer();
for (auto &loc : locations) {
string type = std::to_string(loc.kind());
int linum = loc.line_number();
int begin = loc.begin();
int end = loc.end();
buffer->apply_tag_by_name(theme.tagtable().at(type),
buffer->get_iter_at_line_offset(linum, begin),
buffer->get_iter_at_line_offset(linum, end));
}
}
// Source::View::Theme::tagtable()
// returns a const refrence to the tagtable
const std::unordered_map<string, string>& Source::Theme::tagtable() const {
return tagtable_;
}
void Source::Theme::InsertTag(const string &key, const string &value) {
tagtable_[key] = value;
}
// Source::View::Theme::SetTagTable()
// sets the tagtable for the view
void Source::Theme::SetTagTable(
@ -47,14 +65,37 @@ void Source::Theme::SetTagTable(
///////////////
//// Model ////
///////////////
Source::Model::Model() {
Source::Model::Model() :
theme_() {
std::cout << "Model constructor run" << std::endl;
boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json("config.json", pt);
for ( auto &i : pt ) {
boost::property_tree::ptree props = pt.get_child(i.first);
for (auto &pi : props) {
if (i.first.compare("syntax")) { // checks the config-file
theme_.InsertTag(pi.first, pi.second.get_value<std::string>());
}
}
}
}
Source::Theme& Source::Model::theme() {
return theme_;
}
const string Source::Model::filepath() {
return filepath_;
}
void Source::Model::SetFilePath(const string &filepath) {
filepath_ = filepath;
}
void Source::Model::
SetSourceLocations(const std::vector<Clang::SourceLocation> &locations) {
locations_ = locations;
}
////////////////////
//// Controller ////
////////////////////
@ -82,3 +123,18 @@ Source::Model& Source::Controller::model() {
void Source::Controller::OnLineEdit() {
view().UpdateLine();
}
void Source::Controller::OnNewEmptyFile() {
string filename("/tmp/juci_t");
sourcefile s(filename);
model().SetFilePath(filename);
s.save("");
}
void Source::Controller::OnOpenFile(const string &filename) {
sourcefile s(filename);
view().get_buffer()->set_text(s.get_content());
Clang::TranslationUnit tu(filename.c_str());
model().SetSourceLocations(tu.getSourceLocations());
view().OnOpenFile(model().getSourceLocations(), model().theme());
}

16
juci/source.h

@ -5,6 +5,7 @@
#include <vector>
#include <string>
#include "gtkmm.h"
#include <TranslationUnit.h>
using std::string;
@ -13,6 +14,7 @@ namespace Source {
public:
const std::unordered_map<string, string>& tagtable() const;
void SetTagTable(const std::unordered_map<string, string> &tagtable);
void InsertTag(const string &key, const string &value);
private:
std::unordered_map<string, string> tagtable_;
string background_;
@ -23,6 +25,8 @@ namespace Source {
View();
string UpdateLine();
void ApplyTheme(const Theme &theme);
void OnOpenFile(std::vector<Clang::SourceLocation> &locations,
const Theme &theme);
private:
string GetLine(const Gtk::TextIter &begin);
}; // class View
@ -30,11 +34,18 @@ namespace Source {
class Model{
public:
Model();
Theme theme();
Theme& theme();
const string filepath();
void SetFilePath(const string &filepath);
void SetSourceLocations(const std::vector<Clang::SourceLocation> &locations);
std::vector<Clang::SourceLocation>& getSourceLocations() {
return locations_;
}
private:
Theme theme_;
string filepath_;
std::vector<Clang::SourceLocation> locations_;
};
class Controller {
@ -42,10 +53,11 @@ namespace Source {
Controller();
View& view();
Model& model();
void OnNewEmptyFile();
void OnOpenFile(const string &filename);
private:
void OnLineEdit();
void OnOpenFile();
void OnSaveFile();
protected:

92
juci/sourcefile.cc

@ -0,0 +1,92 @@
#include "sourcefile.h"
#include <giomm.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
sourcefile::sourcefile(const string &input_filename)
: lines(), filename(input_filename) {
open(input_filename);
}
/**
*
*/
void sourcefile::open(const string &filename) {
Gio::init();
// Creates/Opens a file specified by the input string.
Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filename);
if (!file) // Gio::File has overloaded operator
cerr << "Was not able to open file: " << filename << endl;
// Creates pointer for filestream
if (!file->query_exists()) {
file->create_file()->close();
}
Glib::RefPtr<Gio::FileInputStream> stream = file->read();
if (!stream) // error message on stream failure
cerr << filename << " returned an empty stream" << endl;
Glib::RefPtr<Gio::DataInputStream>
datainput = Gio::DataInputStream::create(stream);
string line;
while (datainput->read_line(line)) {
lines.push_back(line);
}
datainput->close();
stream->close();
}
vector<string> sourcefile::get_lines() {
return lines;
}
string sourcefile::get_line(int line_number) {
return lines[line_number];
}
int sourcefile::save(const string &text) {
Gio::init();
// Creates/Opens a file specified by the input string.
Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filename);
if (!file) // Gio::File has overloaded operator
cerr << "Was not able to open file: " << filename << endl;
// Creates
Glib::RefPtr<Gio::FileOutputStream> stream =
file->query_exists() ? file->replace() : file->create_file();
if (!stream) // error message on stream failure
cerr << filename << " returned an empty stream" << endl;
Glib::RefPtr<Gio::DataOutputStream>
output = Gio::DataOutputStream::create(stream);
output->put_string(text);
output->close();
stream->close();
return 0;
}
string sourcefile::get_content() {
string res;
for (auto line : lines) {
res.append(line).append("\n");
}
return res;
}

23
juci/sourcefile.h

@ -0,0 +1,23 @@
#ifndef JUCI_SOURCEFILE_H_
#define JUCI_SOURCEFILE_H_
#include <string>
#include <vector>
using namespace std;
class sourcefile {
public:
explicit sourcefile(const string &filename);
vector<string> get_lines();
string get_content();
string get_line(int line_number);
int save(const string &text);
private:
void open(const string &filename);
vector<string> lines;
string filename;
};
#endif // JUCI_SOURCEFILE_H_
Loading…
Cancel
Save