Formatted source files

This commit is contained in:
edo9300 2019-02-13 21:02:02 +01:00
parent b5f65106ca
commit 82eda30cfd
9 changed files with 440 additions and 494 deletions

View file

@ -10,7 +10,7 @@ class CRC16 {
std::vector<uint16_t>crc16_tab;
uint16_t crc16_constant = 0xA001;
bool mdflag;
public:
public:
CRC16(bool modbus_flag = false) {
// initialize the precalculated tables
if(crc16_tab.empty()) {
@ -47,20 +47,23 @@ class CRC16 {
int32_t GetBanerSize(uint16_t banner_type) {
switch(banner_type) {
case NDS_BANNER_VER_ZH: {
return NDS_BANNER_SIZE_ZH;
break;
}
case NDS_BANNER_VER_ZH_KO: {
return NDS_BANNER_SIZE_ZH_KO;
break;
}
case NDS_BANNER_VER_DSi: {
return NDS_BANNER_SIZE_DSi;
break;
}
default:
return NDS_BANNER_SIZE_ORIGINAL;
case NDS_BANNER_VER_ZH:
{
return NDS_BANNER_SIZE_ZH;
break;
}
case NDS_BANNER_VER_ZH_KO:
{
return NDS_BANNER_SIZE_ZH_KO;
break;
}
case NDS_BANNER_VER_DSi:
{
return NDS_BANNER_SIZE_DSi;
break;
}
default:
return NDS_BANNER_SIZE_ORIGINAL;
}
}
void ReplaceBanner(const std::string& target, const std::string& input, const std::string& output) {
@ -230,7 +233,7 @@ int PathStringReplace(std::string path) {
if(!target.is_open())
return 255;
std::string str((std::istreambuf_iterator<char>(target)),
std::istreambuf_iterator<char>());
std::istreambuf_iterator<char>());
std::size_t found = str.find("sd:/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
if(found == std::string::npos)
return 5;
@ -238,4 +241,4 @@ int PathStringReplace(std::string path) {
target.write(&path[0], path.size());
target.put('\0');
return 0;
}
}

View file

@ -41,41 +41,41 @@ using namespace std;
struct DirEntry {
string name;
bool isDirectory;
} ;
};
bool nameEndsWith (const string& name, const vector<string> extensionList) {
bool nameEndsWith(const string& name, const vector<string> extensionList) {
if (name.size() == 0) return false;
if(name.size() == 0) return false;
if (extensionList.size() == 0) return true;
if(extensionList.size() == 0) return true;
for (int i = 0; i < (int)extensionList.size(); i++) {
for(int i = 0; i < (int)extensionList.size(); i++) {
const string ext = extensionList.at(i);
if ( strcasecmp (name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true;
if(strcasecmp(name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true;
}
return false;
}
bool dirEntryPredicate (const DirEntry& lhs, const DirEntry& rhs) {
bool dirEntryPredicate(const DirEntry& lhs, const DirEntry& rhs) {
if (!lhs.isDirectory && rhs.isDirectory) {
if(!lhs.isDirectory && rhs.isDirectory) {
return false;
}
if (lhs.isDirectory && !rhs.isDirectory) {
if(lhs.isDirectory && !rhs.isDirectory) {
return true;
}
return strcasecmp(lhs.name.c_str(), rhs.name.c_str()) < 0;
}
void getDirectoryContents (vector<DirEntry>& dirContents, const vector<string> extensionList) {
void getDirectoryContents(vector<DirEntry>& dirContents, const vector<string> extensionList) {
struct stat st;
dirContents.clear();
DIR *pdir = opendir (".");
DIR *pdir = opendir(".");
if (pdir == NULL) {
iprintf ("Unable to open the directory.\n");
if(pdir == NULL) {
iprintf("Unable to open the directory.\n");
} else {
while(true) {
@ -88,8 +88,8 @@ void getDirectoryContents (vector<DirEntry>& dirContents, const vector<string> e
dirEntry.name = pent->d_name;
dirEntry.isDirectory = (st.st_mode & S_IFDIR) ? true : false;
if (dirEntry.name.compare(".") != 0 && (dirEntry.isDirectory || nameEndsWith(dirEntry.name, extensionList))) {
dirContents.push_back (dirEntry);
if(dirEntry.name.compare(".") != 0 && (dirEntry.isDirectory || nameEndsWith(dirEntry.name, extensionList))) {
dirContents.push_back(dirEntry);
}
}
@ -100,124 +100,124 @@ void getDirectoryContents (vector<DirEntry>& dirContents, const vector<string> e
sort(dirContents.begin(), dirContents.end(), dirEntryPredicate);
}
void getDirectoryContents (vector<DirEntry>& dirContents) {
void getDirectoryContents(vector<DirEntry>& dirContents) {
vector<string> extensionList;
getDirectoryContents (dirContents, extensionList);
getDirectoryContents(dirContents, extensionList);
}
void showDirectoryContents (const vector<DirEntry>& dirContents, int startRow) {
void showDirectoryContents(const vector<DirEntry>& dirContents, int startRow) {
char path[PATH_MAX];
getcwd(path, PATH_MAX);
// Clear the screen
iprintf ("\x1b[2J");
iprintf("\x1b[2J");
// Print the path
if (strlen(path) < SCREEN_COLS) {
iprintf ("%s", path);
if(strlen(path) < SCREEN_COLS) {
iprintf("%s", path);
} else {
iprintf ("%s", path + strlen(path) - SCREEN_COLS);
iprintf("%s", path + strlen(path) - SCREEN_COLS);
}
// Move to 2nd row
iprintf ("\x1b[1;0H");
iprintf("\x1b[1;0H");
// Print line of dashes
iprintf ("--------------------------------");
iprintf("--------------------------------");
// Print directory listing
for (int i = 0; i < ((int)dirContents.size() - startRow) && i < ENTRIES_PER_SCREEN; i++) {
for(int i = 0; i < ((int)dirContents.size() - startRow) && i < ENTRIES_PER_SCREEN; i++) {
const DirEntry* entry = &dirContents.at(i + startRow);
char entryName[SCREEN_COLS + 1];
// Set row
iprintf ("\x1b[%d;0H", i + ENTRIES_START_ROW);
iprintf("\x1b[%d;0H", i + ENTRIES_START_ROW);
if (entry->isDirectory) {
strncpy (entryName, entry->name.c_str(), SCREEN_COLS);
if(entry->isDirectory) {
strncpy(entryName, entry->name.c_str(), SCREEN_COLS);
entryName[SCREEN_COLS - 3] = '\0';
iprintf (" [%s]", entryName);
iprintf(" [%s]", entryName);
} else {
strncpy (entryName, entry->name.c_str(), SCREEN_COLS);
strncpy(entryName, entry->name.c_str(), SCREEN_COLS);
entryName[SCREEN_COLS - 1] = '\0';
iprintf (" %s", entryName);
iprintf(" %s", entryName);
}
}
}
string browseForFile (const vector<string>& extensionList) {
string browseForFile(const vector<string>& extensionList) {
int pressed = 0;
int screenOffset = 0;
int fileOffset = 0;
vector<DirEntry> dirContents;
getDirectoryContents (dirContents, extensionList);
showDirectoryContents (dirContents, screenOffset);
getDirectoryContents(dirContents, extensionList);
showDirectoryContents(dirContents, screenOffset);
while (true) {
while(true) {
// Clear old cursors
for (int i = ENTRIES_START_ROW; i < ENTRIES_PER_SCREEN + ENTRIES_START_ROW; i++) {
iprintf ("\x1b[%d;0H ", i);
for(int i = ENTRIES_START_ROW; i < ENTRIES_PER_SCREEN + ENTRIES_START_ROW; i++) {
iprintf("\x1b[%d;0H ", i);
}
// Show cursor
iprintf ("\x1b[%d;0H*", fileOffset - screenOffset + ENTRIES_START_ROW);
iprintf("\x1b[%d;0H*", fileOffset - screenOffset + ENTRIES_START_ROW);
// Power saving loop. Only poll the keys once per frame and sleep the CPU if there is nothing else to do
do {
scanKeys();
pressed = keysDownRepeat();
swiWaitForVBlank();
} while (!pressed);
} while(!pressed);
if (pressed & KEY_UP) fileOffset -= 1;
if (pressed & KEY_DOWN) fileOffset += 1;
if (pressed & KEY_LEFT) fileOffset -= ENTRY_PAGE_LENGTH;
if (pressed & KEY_RIGHT) fileOffset += ENTRY_PAGE_LENGTH;
if(pressed & KEY_UP) fileOffset -= 1;
if(pressed & KEY_DOWN) fileOffset += 1;
if(pressed & KEY_LEFT) fileOffset -= ENTRY_PAGE_LENGTH;
if(pressed & KEY_RIGHT) fileOffset += ENTRY_PAGE_LENGTH;
if (fileOffset < 0) fileOffset = dirContents.size() - 1; // Wrap around to bottom of list
if (fileOffset > ((int)dirContents.size() - 1)) fileOffset = 0; // Wrap around to top of list
if(fileOffset < 0) fileOffset = dirContents.size() - 1; // Wrap around to bottom of list
if(fileOffset > ((int)dirContents.size() - 1)) fileOffset = 0; // Wrap around to top of list
// Scroll screen if needed
if (fileOffset < screenOffset) {
if(fileOffset < screenOffset) {
screenOffset = fileOffset;
showDirectoryContents (dirContents, screenOffset);
showDirectoryContents(dirContents, screenOffset);
}
if (fileOffset > screenOffset + ENTRIES_PER_SCREEN - 1) {
if(fileOffset > screenOffset + ENTRIES_PER_SCREEN - 1) {
screenOffset = fileOffset - ENTRIES_PER_SCREEN + 1;
showDirectoryContents (dirContents, screenOffset);
showDirectoryContents(dirContents, screenOffset);
}
if (pressed & KEY_A) {
if(pressed & KEY_A) {
DirEntry* entry = &dirContents.at(fileOffset);
if (entry->isDirectory) {
if(entry->isDirectory) {
iprintf("Entering directory\n");
// Enter selected directory
chdir (entry->name.c_str());
getDirectoryContents (dirContents, extensionList);
chdir(entry->name.c_str());
getDirectoryContents(dirContents, extensionList);
screenOffset = 0;
fileOffset = 0;
showDirectoryContents (dirContents, screenOffset);
showDirectoryContents(dirContents, screenOffset);
} else {
// Clear the screen
iprintf ("\x1b[2J");
iprintf("\x1b[2J");
// Return the chosen file
char cwd[PATH_MAX];
getcwd(cwd, PATH_MAX);
std::string full_path;
full_path.resize(sizeof(cwd)+entry->name.size()+10);
full_path.resize(sizeof(cwd) + entry->name.size() + 10);
sprintf(&full_path[0], "%s%s", cwd, entry->name.c_str());
return full_path;
}
}
if (pressed & KEY_B) {
if(pressed & KEY_B) {
// Go up a directory
chdir ("..");
getDirectoryContents (dirContents, extensionList);
chdir("..");
getDirectoryContents(dirContents, extensionList);
screenOffset = 0;
fileOffset = 0;
showDirectoryContents (dirContents, screenOffset);
showDirectoryContents(dirContents, screenOffset);
}
}
}

View file

@ -25,7 +25,7 @@
#include <string>
#include <vector>
std::string browseForFile (const std::vector<std::string>& extensionList);
std::string browseForFile(const std::vector<std::string>& extensionList);

View file

@ -1,21 +1,21 @@
/*
inifile.cpp
Copyright (C) 2007 Acekard, www.acekard.com
Copyright (C) 2007-2009 somebody
Copyright (C) 2009 yellow wood goblin
inifile.cpp
Copyright (C) 2007 Acekard, www.acekard.com
Copyright (C) 2007-2009 somebody
Copyright (C) 2009 yellow wood goblin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
@ -23,84 +23,69 @@
#include "inifile.h"
//#include "stringtool.h"
static bool freadLine(FILE* f,std::string& str)
{
str.clear();
static bool freadLine(FILE* f, std::string& str) {
str.clear();
__read:
char p=0;
char p = 0;
size_t readed=fread(&p,1,1,f);
if(0==readed)
{
str="";
return false;
}
if('\n'==p||'\r'==p)
{
str="";
return true;
}
size_t readed = fread(&p, 1, 1, f);
if(0 == readed) {
str = "";
return false;
}
if('\n' == p || '\r' == p) {
str = "";
return true;
}
while(p!='\n'&&p!='\r'&&readed)
{
str+=p;
readed=fread(&p,1,1,f);
}
while(p != '\n'&&p != '\r'&&readed) {
str += p;
readed = fread(&p, 1, 1, f);
}
if(str.empty()||""==str)
{
goto __read;
}
if(str.empty() || "" == str) {
goto __read;
}
return true;
return true;
}
static void trimString(std::string& str)
{
size_t first=str.find_first_not_of(" \t"),last;
if(first==str.npos)
{
str="";
}
else
{
last=str.find_last_not_of(" \t");
if(first>0||(last+1)<str.length()) str=str.substr(first,last-first+1);
}
static void trimString(std::string& str) {
size_t first = str.find_first_not_of(" \t"), last;
if(first == str.npos) {
str = "";
} else {
last = str.find_last_not_of(" \t");
if(first > 0 || (last + 1) < str.length()) str = str.substr(first, last - first + 1);
}
}
CIniFile::CIniFile()
{
m_bLastResult=false;
m_bModified=false;
m_bReadOnly=false;
CIniFile::CIniFile() {
m_bLastResult = false;
m_bModified = false;
m_bReadOnly = false;
}
CIniFile::CIniFile(const std::string& filename)
{
m_sFileName=filename;
m_bLastResult=false;
m_bModified=false;
m_bReadOnly=false;
m_bHasHandle=false;
LoadIniFile(m_sFileName);
CIniFile::CIniFile(const std::string& filename) {
m_sFileName = filename;
m_bLastResult = false;
m_bModified = false;
m_bReadOnly = false;
m_bHasHandle = false;
LoadIniFile(m_sFileName);
}
CIniFile::~CIniFile()
{
if(m_FileContainer.size()>0)
{
m_FileContainer.clear();
}
CIniFile::~CIniFile() {
if(m_FileContainer.size() > 0) {
m_FileContainer.clear();
}
}
void CIniFile::SetString(const std::string& Section,const std::string& Item,const std::string& Value)
{
if(GetFileString(Section,Item)!=Value)
{
SetFileString(Section,Item,Value);
m_bModified=true;
}
void CIniFile::SetString(const std::string& Section, const std::string& Item, const std::string& Value) {
if(GetFileString(Section, Item) != Value) {
SetFileString(Section, Item, Value);
m_bModified = true;
}
}
/*void CIniFile::SetInt(const std::string& Section,const std::string& Item,int Value)
@ -109,65 +94,56 @@ void CIniFile::SetString(const std::string& Section,const std::string& Item,cons
if(GetFileString(Section,Item)!=strtemp)
{
SetFileString(Section,Item,strtemp);
m_bModified=true;
SetFileString(Section,Item,strtemp);
m_bModified=true;
}
}*/
std::string CIniFile::GetString(const std::string& Section,const std::string& Item)
{
return GetFileString(Section,Item);
std::string CIniFile::GetString(const std::string& Section, const std::string& Item) {
return GetFileString(Section, Item);
}
std::string CIniFile::GetString(const std::string& Section,const std::string& Item,const std::string& DefaultValue)
{
std::string temp=GetString(Section,Item);
if(!m_bLastResult)
{
SetString(Section,Item,DefaultValue);
temp=DefaultValue;
}
return temp;
std::string CIniFile::GetString(const std::string& Section, const std::string& Item, const std::string& DefaultValue) {
std::string temp = GetString(Section, Item);
if(!m_bLastResult) {
SetString(Section, Item, DefaultValue);
temp = DefaultValue;
}
return temp;
}
void CIniFile::GetStringVector(const std::string& Section,const std::string& Item,std::vector< std::string >& strings,char delimiter)
{
std::string strValue=GetFileString(Section,Item);
strings.clear();
size_t pos;
while((pos=strValue.find(delimiter),strValue.npos!=pos))
{
const std::string string=strValue.substr(0,pos);
if(string.length())
{
strings.push_back(string);
}
strValue=strValue.substr(pos+1,strValue.npos);
}
if(strValue.length())
{
strings.push_back(strValue);
}
void CIniFile::GetStringVector(const std::string& Section, const std::string& Item, std::vector< std::string >& strings, char delimiter) {
std::string strValue = GetFileString(Section, Item);
strings.clear();
size_t pos;
while((pos = strValue.find(delimiter), strValue.npos != pos)) {
const std::string string = strValue.substr(0, pos);
if(string.length()) {
strings.push_back(string);
}
strValue = strValue.substr(pos + 1, strValue.npos);
}
if(strValue.length()) {
strings.push_back(strValue);
}
}
void CIniFile::SetStringVector(const std::string& Section,const std::string& Item,std::vector<std::string>& strings,char delimiter)
{
std::string strValue;
for(size_t ii=0;ii<strings.size();++ii)
{
if(ii) strValue+=delimiter;
strValue+=strings[ii];
}
SetString(Section,Item,strValue);
void CIniFile::SetStringVector(const std::string& Section, const std::string& Item, std::vector<std::string>& strings, char delimiter) {
std::string strValue;
for(size_t ii = 0; ii < strings.size(); ++ii) {
if(ii) strValue += delimiter;
strValue += strings[ii];
}
SetString(Section, Item, strValue);
}
/*int CIniFile::GetInt(const std::string& Section,const std::string& Item)
{
std::string value=GetFileString(Section,Item);
if(value.size()>2&&'0'==value[0]&&('x'==value[1]||'X'==value[1]))
return strtol(value.c_str(),NULL,16);
return strtol(value.c_str(),NULL,16);
else
return strtol(value.c_str(),NULL,10);
return strtol(value.c_str(),NULL,10);
}
int CIniFile::GetInt(const std::string& Section,const std::string& Item,int DefaultValue)
@ -176,224 +152,192 @@ int CIniFile::GetInt(const std::string& Section,const std::string& Item,int Defa
temp=GetInt(Section,Item);
if(!m_bLastResult)
{
SetInt(Section,Item,DefaultValue);
temp=DefaultValue;
SetInt(Section,Item,DefaultValue);
temp=DefaultValue;
}
return temp;
}*/
bool CIniFile::LoadIniFile(const std::string& FileName)
{
//dbg_printf("load %s\n",FileName.c_str());
if(FileName!="") m_sFileName=FileName;
bool CIniFile::LoadIniFile(const std::string& FileName) {
//dbg_printf("load %s\n",FileName.c_str());
if(FileName != "") m_sFileName = FileName;
FILE* f=fopen(FileName.c_str(),"rb");
FILE* f = fopen(FileName.c_str(), "rb");
if(NULL==f) return false;
m_bHasHandle = true;
//check for utf8 bom.
char bom[3];
if(fread(bom,3,1,f)==1&&bom[0]==0xef&&bom[1]==0xbb&&bom[2]==0xbf) ;
else fseek(f,0,SEEK_SET);
if(NULL == f) return false;
std::string strline("");
m_FileContainer.clear();
m_bHasHandle = true;
while(freadLine(f,strline))
{
trimString(strline);
if(strline!=""&&';'!=strline[0]&&'/'!=strline[0]&&'!'!=strline[0]) m_FileContainer.push_back(strline);
}
//check for utf8 bom.
char bom[3];
if(fread(bom, 3, 1, f) == 1 && bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf);
else fseek(f, 0, SEEK_SET);
fclose(f);
std::string strline("");
m_FileContainer.clear();
m_bLastResult=false;
m_bModified=false;
while(freadLine(f, strline)) {
trimString(strline);
if(strline != ""&&';' != strline[0] && '/' != strline[0] && '!' != strline[0]) m_FileContainer.push_back(strline);
}
return true;
fclose(f);
m_bLastResult = false;
m_bModified = false;
return true;
}
bool CIniFile::SaveIniFileModified(const std::string& FileName)
{
if(m_bModified==true)
{
return SaveIniFile(FileName);
}
bool CIniFile::SaveIniFileModified(const std::string& FileName) {
if(m_bModified == true) {
return SaveIniFile(FileName);
}
return true;
return true;
}
bool CIniFile::HasFileHandle()
{
return m_bHasHandle;
bool CIniFile::HasFileHandle() {
return m_bHasHandle;
}
bool CIniFile::SaveIniFile(const std::string& FileName)
{
if(FileName!="")
m_sFileName=FileName;
bool CIniFile::SaveIniFile(const std::string& FileName) {
if(FileName != "")
m_sFileName = FileName;
FILE* f=fopen(m_sFileName.c_str(),"wb");
if(NULL==f)
{
return false;
}
FILE* f = fopen(m_sFileName.c_str(), "wb");
if(NULL == f) {
return false;
}
for(size_t ii=0;ii<m_FileContainer.size();ii++)
{
std::string& strline=m_FileContainer[ii];
size_t notSpace=strline.find_first_not_of(' ');
strline=strline.substr(notSpace);
if(strline.find('[')==0&&ii>0)
{
if(!m_FileContainer[ii-1].empty()&&m_FileContainer[ii-1]!="")
fwrite("\r\n",1,2,f);
}
if(!strline.empty()&&strline!="")
{
fwrite(strline.c_str(),1,strline.length(),f);
fwrite("\r\n",1,2,f);
}
}
for(size_t ii = 0; ii < m_FileContainer.size(); ii++) {
std::string& strline = m_FileContainer[ii];
size_t notSpace = strline.find_first_not_of(' ');
strline = strline.substr(notSpace);
if(strline.find('[') == 0 && ii > 0) {
if(!m_FileContainer[ii - 1].empty() && m_FileContainer[ii - 1] != "")
fwrite("\r\n", 1, 2, f);
}
if(!strline.empty() && strline != "") {
fwrite(strline.c_str(), 1, strline.length(), f);
fwrite("\r\n", 1, 2, f);
}
}
fclose(f);
fclose(f);
m_bModified=false;
m_bModified = false;
return true;
return true;
}
std::string CIniFile::GetFileString(const std::string& Section,const std::string& Item)
{
std::string strline;
std::string strSection;
std::string strItem;
std::string strValue;
std::string CIniFile::GetFileString(const std::string& Section, const std::string& Item) {
std::string strline;
std::string strSection;
std::string strItem;
std::string strValue;
size_t ii=0;
size_t iFileLines=m_FileContainer.size();
size_t ii = 0;
size_t iFileLines = m_FileContainer.size();
if(m_bReadOnly)
{
cSectionCache::iterator it=m_Cache.find(Section);
if((it!=m_Cache.end())) ii=it->second;
}
if(m_bReadOnly) {
cSectionCache::iterator it = m_Cache.find(Section);
if((it != m_Cache.end())) ii = it->second;
}
m_bLastResult=false;
m_bLastResult = false;
if(iFileLines>=0)
{
while(ii<iFileLines)
{
strline=m_FileContainer[ii++];
if(iFileLines >= 0) {
while(ii < iFileLines) {
strline = m_FileContainer[ii++];
size_t rBracketPos=0;
if('['==strline[0]) rBracketPos=strline.find(']');
if(rBracketPos>0&&rBracketPos!=std::string::npos)
{
strSection=strline.substr(1,rBracketPos-1);
if(m_bReadOnly) m_Cache.insert(std::make_pair(strSection,ii-1));
if(strSection==Section)
{
while(ii<iFileLines)
{
strline=m_FileContainer[ii++];
size_t equalsignPos=strline.find('=');
if(equalsignPos!=strline.npos)
{
size_t last=equalsignPos?strline.find_last_not_of(" \t",equalsignPos-1):strline.npos;
if(last==strline.npos) strItem="";
else strItem=strline.substr(0,last+1);
size_t rBracketPos = 0;
if('[' == strline[0]) rBracketPos = strline.find(']');
if(rBracketPos > 0 && rBracketPos != std::string::npos) {
strSection = strline.substr(1, rBracketPos - 1);
if(m_bReadOnly) m_Cache.insert(std::make_pair(strSection, ii - 1));
if(strSection == Section) {
while(ii < iFileLines) {
strline = m_FileContainer[ii++];
size_t equalsignPos = strline.find('=');
if(equalsignPos != strline.npos) {
size_t last = equalsignPos ? strline.find_last_not_of(" \t", equalsignPos - 1) : strline.npos;
if(last == strline.npos) strItem = "";
else strItem = strline.substr(0, last + 1);
if(strItem==Item)
{
size_t first=strline.find_first_not_of(" \t",equalsignPos+1);
if(first==strline.npos) strValue="";
else strValue=strline.substr(first);
m_bLastResult=true;
return strValue;
}
}
else if('['==strline[0])
{
break;
}
}
break;
}
}
}
}
return std::string("");
if(strItem == Item) {
size_t first = strline.find_first_not_of(" \t", equalsignPos + 1);
if(first == strline.npos) strValue = "";
else strValue = strline.substr(first);
m_bLastResult = true;
return strValue;
}
} else if('[' == strline[0]) {
break;
}
}
break;
}
}
}
}
return std::string("");
}
void CIniFile::SetFileString(const std::string& Section,const std::string& Item,const std::string& Value)
{
std::string strline;
std::string strSection;
std::string strItem;
void CIniFile::SetFileString(const std::string& Section, const std::string& Item, const std::string& Value) {
std::string strline;
std::string strSection;
std::string strItem;
if(m_bReadOnly) return;
if(m_bReadOnly) return;
size_t ii=0;
size_t iFileLines=m_FileContainer.size();
size_t ii = 0;
size_t iFileLines = m_FileContainer.size();
while(ii<iFileLines)
{
strline=m_FileContainer[ii++];
while(ii < iFileLines) {
strline = m_FileContainer[ii++];
size_t rBracketPos=0;
if('['==strline[0]) rBracketPos=strline.find(']');
if(rBracketPos>0&&rBracketPos!=std::string::npos)
{
strSection=strline.substr(1,rBracketPos-1);
if(strSection==Section)
{
while(ii<iFileLines)
{
strline=m_FileContainer[ii++];
size_t equalsignPos=strline.find('=');
if(equalsignPos!=strline.npos)
{
size_t last=equalsignPos?strline.find_last_not_of(" \t",equalsignPos-1):strline.npos;
if(last==strline.npos) strItem="";
else strItem=strline.substr(0,last+1);
size_t rBracketPos = 0;
if('[' == strline[0]) rBracketPos = strline.find(']');
if(rBracketPos > 0 && rBracketPos != std::string::npos) {
strSection = strline.substr(1, rBracketPos - 1);
if(strSection == Section) {
while(ii < iFileLines) {
strline = m_FileContainer[ii++];
size_t equalsignPos = strline.find('=');
if(equalsignPos != strline.npos) {
size_t last = equalsignPos ? strline.find_last_not_of(" \t", equalsignPos - 1) : strline.npos;
if(last == strline.npos) strItem = "";
else strItem = strline.substr(0, last + 1);
if(Item==strItem)
{
ReplaceLine(ii-1,Item+" = "+Value);
return;
}
}
else if('['==strline[0])
{
InsertLine(ii-1,Item+" = "+Value);
return;
}
}
InsertLine(ii,Item+" = "+Value);
return;
}
}
}
if(Item == strItem) {
ReplaceLine(ii - 1, Item + " = " + Value);
return;
}
} else if('[' == strline[0]) {
InsertLine(ii - 1, Item + " = " + Value);
return;
}
}
InsertLine(ii, Item + " = " + Value);
return;
}
}
}
InsertLine(ii,"["+Section+"]");
InsertLine(ii+1,Item+" = "+Value);
return;
InsertLine(ii, "[" + Section + "]");
InsertLine(ii + 1, Item + " = " + Value);
return;
}
bool CIniFile::InsertLine(size_t line,const std::string& str)
{
m_FileContainer.insert(m_FileContainer.begin()+line,str);
return true;
bool CIniFile::InsertLine(size_t line, const std::string& str) {
m_FileContainer.insert(m_FileContainer.begin() + line, str);
return true;
}
bool CIniFile::ReplaceLine(size_t line,const std::string& str)
{
m_FileContainer[line]=str;
return true;
bool CIniFile::ReplaceLine(size_t line, const std::string& str) {
m_FileContainer[line] = str;
return true;
}

View file

@ -1,21 +1,21 @@
/*
inifile.h
Copyright (C) 2007 Acekard, www.acekard.com
Copyright (C) 2007-2009 somebody
Copyright (C) 2009-2010 yellow wood goblin
inifile.h
Copyright (C) 2007 Acekard, www.acekard.com
Copyright (C) 2007-2009 somebody
Copyright (C) 2009-2010 yellow wood goblin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _INIFILE_H_
@ -25,44 +25,43 @@
#include <vector>
#include <map>
class CIniFile
{
public:
CIniFile();
CIniFile(const std::string& filename);
virtual ~CIniFile();
class CIniFile {
public:
CIniFile();
CIniFile(const std::string& filename);
virtual ~CIniFile();
public:
bool LoadIniFile(const std::string& FileName);
bool SaveIniFile(const std::string& FileName);
bool SaveIniFileModified(const std::string& FileName);
bool HasFileHandle();
public:
bool LoadIniFile(const std::string& FileName);
bool SaveIniFile(const std::string& FileName);
bool SaveIniFileModified(const std::string& FileName);
bool HasFileHandle();
std::string GetString(const std::string& Section,const std::string& Item,const std::string& DefaultValue);
void SetString(const std::string& Section,const std::string& Item,const std::string& Value);
int GetInt(const std::string& Section,const std::string& Item,int DefaultValue);
void SetInt(const std::string& Section,const std::string& Item,int Value);
void GetStringVector(const std::string& Section,const std::string& Item,std::vector<std::string>& strings,char delimiter=',');
void SetStringVector(const std::string& Section,const std::string& Item,std::vector<std::string>& strings,char delimiter=',');
protected:
std::string m_sFileName;
typedef std::vector<std::string> cStringArray;
cStringArray m_FileContainer;
bool m_bLastResult;
bool m_bModified;
bool m_bReadOnly;
bool m_bHasHandle;
typedef std::map<std::string,size_t> cSectionCache;
cSectionCache m_Cache;
std::string GetString(const std::string& Section, const std::string& Item, const std::string& DefaultValue);
void SetString(const std::string& Section, const std::string& Item, const std::string& Value);
int GetInt(const std::string& Section, const std::string& Item, int DefaultValue);
void SetInt(const std::string& Section, const std::string& Item, int Value);
void GetStringVector(const std::string& Section, const std::string& Item, std::vector<std::string>& strings, char delimiter = ',');
void SetStringVector(const std::string& Section, const std::string& Item, std::vector<std::string>& strings, char delimiter = ',');
protected:
std::string m_sFileName;
typedef std::vector<std::string> cStringArray;
cStringArray m_FileContainer;
bool m_bLastResult;
bool m_bModified;
bool m_bReadOnly;
bool m_bHasHandle;
typedef std::map<std::string, size_t> cSectionCache;
cSectionCache m_Cache;
bool InsertLine(size_t line,const std::string& str);
bool ReplaceLine(size_t line,const std::string& str);
bool InsertLine(size_t line, const std::string& str);
bool ReplaceLine(size_t line, const std::string& str);
void SetFileString(const std::string& Section,const std::string& Item,const std::string& Value);
std::string GetFileString(const std::string& Section,const std::string& Item);
void SetFileString(const std::string& Section, const std::string& Item, const std::string& Value);
std::string GetFileString(const std::string& Section, const std::string& Item);
std::string GetString(const std::string& Section,const std::string& Item);
int GetInt(const std::string& Section,const std::string& Item);
std::string GetString(const std::string& Section, const std::string& Item);
int GetInt(const std::string& Section, const std::string& Item);
};
#endif // _INIFILE_H_

View file

@ -82,27 +82,27 @@ void displayInit() {
}
bool fileExists(const std::string& file) {
struct stat buf;
return (stat(file.c_str(), &buf) == 0);
struct stat buf;
return (stat(file.c_str(), &buf) == 0);
}
std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
void stop (void) {
//---------------------------------------------------------------------------------
while (1) {
void stop(void) {
//---------------------------------------------------------------------------------
while(true) {
swiWaitForVBlank();
}
}
void wait (void) {
//---------------------------------------------------------------------------------
while (1) {
void wait(void) {
//---------------------------------------------------------------------------------
while(true) {
swiWaitForVBlank();
scanKeys();
int pressed = keysDown();
@ -112,19 +112,19 @@ void wait (void) {
const char* GetErrorString(int code) {
switch(code){
case 1:
return "fatInitDefault failed!";
case 2:
return "The MakeForwarder folder is missing!";
case 3:
return "Template.nds is missing from the MakeForwarder folder!";
case 4:
return "Error when reading the template.nds file, make sure to have the correct one!";
case 5:
return "Couldn't open the target rom, or the rom is not a valid target!";
default:
return "";
switch(code) {
case 1:
return "fatInitDefault failed!";
case 2:
return "The MakeForwarder folder is missing!";
case 3:
return "Template.nds is missing from the MakeForwarder folder!";
case 4:
return "Error when reading the template.nds file, make sure to have the correct one!";
case 5:
return "Couldn't open the target rom, or the rom is not a valid target!";
default:
return "";
}
}
@ -135,7 +135,7 @@ void PrintError(int errorcode, bool halt = false) {
consoleSetWindow(&upperScreen, 0, 3, DISPLAY_COLUMNS, 23);
consoleSelect(&upperScreen);
WriteMessage(GetErrorString(errorcode), true);
if (halt)
if(halt)
stop();
else {
WriteMessage("Press A to continue", false);
@ -147,7 +147,7 @@ void PrintError(int errorcode, bool halt = false) {
std::string GetHexid(std::string file, int* chk) {
std::ifstream infile(file, std::ifstream::binary);
infile.seekg(0xc);
if(infile.tellg()!=0xc) {
if(infile.tellg() != 0xc) {
*chk = 1;
return "";
}
@ -159,20 +159,20 @@ std::string GetHexid(std::string file, int* chk) {
}
void CreateForwarder() {
std::vector<std::string> extensionList={".nds"};
std::vector<std::string> extensionList = { ".nds" };
WriteMessage("Select the target rom", true, &upperScreen);
consoleSelect(&lowerScreen);
std::string file = browseForFile(extensionList);
chdir("sd:/");
int chk = 0;
std::string gameidhex = GetHexid(file, &chk);
if (chk){
if(chk) {
PrintError(5);
return;
}
std::string folderpath("sd:/title/00030004/" + gameidhex);
if(fileExists(folderpath + "/content/00000000.app")) {
WriteMessage("A DsiWare with the same id already exists, do you want to overwrite it?", true, &upperScreen);
int ret = yesno.DoMenu(&lowerScreen);
@ -181,63 +181,63 @@ void CreateForwarder() {
if(ret)
return;
}
WriteMessage("Creating forwarder", true, &lowerScreen);
ReplaceBanner("sd:/MakeForwarder/template.nds", file, "sd:/MakeForwarder/banner.nds");
Patch("sd:/MakeForwarder/banner.nds", false);
MakeTmd("sd:/MakeForwarder/banner.nds", "sd:/MakeForwarder/title.tmd");
chk = PathStringReplace("title/00030004/" + gameidhex + "/data/");
if(chk) {
PrintError(chk);
remove("sd:/MakeForwarder/banner.nds");
remove("sd:/MakeForwarder/title.tmd");
return;
}
if(CreatePath("title/00030004/" + gameidhex + "/data", "sd:/") && CreatePath("title/00030004/" + gameidhex + "/content", "sd:/")) {
Movefile("sd:/MakeForwarder/banner.nds", folderpath + "/content/00000000.app");
Movefile("sd:/MakeForwarder/title.tmd", folderpath + "/content/title.tmd");
if(bootstrap_template.HasFileHandle()) {
bootsrtapconfig.SaveIniFile((folderpath + "/data/config.ini").c_str());
bootstrap_template.SetString( "NDS-BOOTSTRAP", "NDS_PATH", file.c_str());
bootstrap_template.SetString("NDS-BOOTSTRAP", "NDS_PATH", file.c_str());
std::string savePath = ReplaceAll(file, ".nds", ".sav");
bootstrap_template.SetString( "NDS-BOOTSTRAP", "SAV_PATH", savePath.c_str());
bootstrap_template.SetString("NDS-BOOTSTRAP", "SAV_PATH", savePath.c_str());
bootstrap_template.SaveIniFile((folderpath + "/data/bootstrap.ini").c_str());
}
}
}
void SetBootstrap() {
std::vector<std::string> extensionList={".nds"};
std::vector<std::string> extensionList = { ".nds" };
WriteMessage("Select the target bootstrap file to be used", true, &upperScreen);
consoleSelect(&lowerScreen);
std::string file = browseForFile(extensionList);
chdir("sd:/");
size_t found = file.find_last_of("/");
std::string bootstrappath=file.substr(0,found+1);
std::string bootstrapversion=file.substr(found+1);
bootsrtapconfig.SetString( "NDS-FORWARDER", "BOOTSTRAP_PATH", bootstrappath.c_str());
bootsrtapconfig.SetString( "NDS-FORWARDER", "BOOTSTRAP_VERSION", bootstrapversion.c_str());
bootstrap_template.LoadIniFile((bootstrappath+"nds-bootstrap.ini").c_str());
size_t found = file.find_last_of("/");
std::string bootstrappath = file.substr(0, found + 1);
std::string bootstrapversion = file.substr(found + 1);
bootsrtapconfig.SetString("NDS-FORWARDER", "BOOTSTRAP_PATH", bootstrappath.c_str());
bootsrtapconfig.SetString("NDS-FORWARDER", "BOOTSTRAP_VERSION", bootstrapversion.c_str());
bootstrap_template.LoadIniFile((bootstrappath + "nds-bootstrap.ini").c_str());
}
void CheckResources() {
if (!fileExists("sd:/MakeForwarder"))
if(!fileExists("sd:/MakeForwarder"))
PrintError(2, true);
if (!fileExists("sd:/MakeForwarder/template.nds"))
if(!fileExists("sd:/MakeForwarder/template.nds"))
PrintError(3, true);
std::ifstream ndstemplate("sd:/MakeForwarder/template.nds", std::ifstream::binary);
std::string str((std::istreambuf_iterator<char>(ndstemplate)),
std::istreambuf_iterator<char>());
std::istreambuf_iterator<char>());
std::size_t found = str.find("sd:/kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
if(found == std::string::npos)
PrintError(4, true);
@ -248,7 +248,7 @@ int main() {
consoleSetWindow(&upperScreen, 0, 0, DISPLAY_COLUMNS, 3);
WriteMessage("Forwarder maker by edo9300 v1.0", true, &upperScreen);
consoleSetWindow(&upperScreen, 0, 3, DISPLAY_COLUMNS, 23);
if (!fatInitDefault())
if(!fatInitDefault())
PrintError(1, true);
CheckResources();
menu mainmenu;
@ -256,13 +256,13 @@ int main() {
mainmenu.AddOption("Set target bootstrap");
yesno.AddOption("Yes");
yesno.AddOption("No");
while(1){
while(true) {
swiWaitForVBlank();
WriteMessage("Use the option \"Set target bootstrap\" to avoid configuring the created forwarders at startup", true, &upperScreen);
int ret = mainmenu.DoMenu(&lowerScreen);
if (ret==0)
if(ret == 0)
CreateForwarder();
else if (ret==1)
else if(ret == 1)
SetBootstrap();
else
break;

View file

@ -65,7 +65,7 @@ void MakeTmd(const std::string& target, const std::string& destination) {
// Phase 4 - offset 0x1AA (fill-in 0x80 value, 0x10 times)
{
for(size_t i = 0; i<0x10; i++) {
for(size_t i = 0; i < 0x10; i++) {
tmd[0x1AA + i] = 0x80;
}
}

View file

@ -9,37 +9,37 @@
#include <string>
#include "menu.h"
void menu::AddOption(const std::string& option){
void menu::AddOption(const std::string& option) {
options.push_back(option);
}
int menu::DoMenu(PrintConsole* screen){
int menu::DoMenu(PrintConsole* screen) {
consoleSelect(screen);
current = 0;
consoleClear();
consoleSetWindow(screen, 1, 0, DISPLAY_COLUMNS-1, DISPLAY_ROWS);
consoleSetWindow(screen, 1, 0, DISPLAY_COLUMNS - 1, DISPLAY_ROWS);
for(auto option : options)
iprintf((option+"\n").c_str());
iprintf((option + "\n").c_str());
consoleSetWindow(screen, 0, 0, 1, DISPLAY_ROWS);
iprintf(">");
while (true){
while(true) {
swiWaitForVBlank();
scanKeys();
int pressed = keysDownRepeat();
if(pressed & KEY_UP) {
consoleClear();
current--;
if(current<0)
if(current < 0)
current = options.size() - 1;
for (int a = 0; a<current; a++)
for(int a = 0; a < current; a++)
iprintf("\n");
iprintf(">");
}
if(pressed & KEY_DOWN) {
consoleClear();
current++;
if(current>=options.size())
if(current >= options.size())
current = 0;
for (int a = 0; a<current; a++)
for(int a = 0; a < current; a++)
iprintf("\n");
iprintf(">");
}
@ -56,6 +56,6 @@ int menu::DoMenu(PrintConsole* screen){
}
}
}
int menu::GetLastSeletedOption(){
int menu::GetLastSeletedOption() {
return current;
}

View file

@ -7,15 +7,15 @@
#define DISPLAY_COLUMNS 32
#define DISPLAY_ROWS 26
class menu{
private:
class menu {
private:
std::vector<std::string> options;
int current;
public:
public:
void AddOption(const std::string& option);
int DoMenu(PrintConsole* screen);
int GetLastSeletedOption();
};