Skip to content

Commit 2516e81

Browse files
committed
Use relative paths for favorites
1 parent c45f13e commit 2516e81

File tree

4 files changed

+170
-15
lines changed

4 files changed

+170
-15
lines changed

TegraRcmGUI/DialogTab01.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,25 @@ BOOL DialogTab01::OnInitDialog()
107107
for (int i = 0; i < m_TegraRcm->Favorites.GetCount(); i++)
108108
{
109109
CListBox*pListBox = (CListBox*)GetDlgItem(IDC_LIST1);
110-
111-
int nIndex = m_TegraRcm->Favorites[i].ReverseFind(_T('\\'));
110+
CString fav = m_TegraRcm->Favorites[i];
111+
int nIndex = fav.ReverseFind(_T('\\'));
112+
CString csFilename, csPath, Item;
112113
if (nIndex > 0)
113114
{
114-
CString csFilename, csPath, Item;
115-
csFilename = m_TegraRcm->Favorites[i].Right(m_TegraRcm->Favorites[i].GetLength() - nIndex - 1);
116-
csPath = m_TegraRcm->Favorites[i].Left(nIndex);
115+
csFilename = fav.Right(fav.GetLength() - nIndex - 1);
116+
csPath = fav.Left(nIndex);
117117
Item = csFilename + _T(" (") + csPath + _T(")");
118-
pListBox->AddString(_tcsdup(Item));
118+
}
119+
else
120+
{
121+
Item = fav;
122+
}
123+
pListBox->AddString(_tcsdup(Item));
119124

120-
wstring wcsPath(csPath);
121-
string scsPath(wcsPath.begin(), wcsPath.end());
122-
m_TegraRcm->AppendLog("Add favorites to listbox");
123-
m_TegraRcm->AppendLog(scsPath);
124-
}
125+
wstring wcsPath(csPath);
126+
string scsPath(wcsPath.begin(), wcsPath.end());
127+
m_TegraRcm->AppendLog("Add favorites to listbox");
128+
m_TegraRcm->AppendLog(scsPath);
125129
}
126130

127131
CFont* pFont = GetFont();

TegraRcmGUI/TegraRcm.cpp

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424
SOFTWARE.
2525
2626
*/
27-
27+
#include <stdlib.h>
2828
#include "stdafx.h"
2929
#include "TegraRcm.h"
3030

@@ -370,6 +370,27 @@ void TegraRcm::GetFavorites()
370370
string sfav(wfav.begin(), wfav.end());
371371
AppendLog("Append new favorite : ");
372372
AppendLog(sfav);
373+
374+
// For relative path
375+
int nIndex = fav.ReverseFind(_T(':'));
376+
if (nIndex <= 0)
377+
{
378+
// Get current directory
379+
CString csPath;
380+
TCHAR szPath[_MAX_PATH];
381+
VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH));
382+
CString csPathf(szPath);
383+
int nIndex = csPathf.ReverseFind(_T('\\'));
384+
if (nIndex > 0) csPath = csPathf.Left(nIndex);
385+
csPath.Append(_T("\\"));
386+
csPath.Append(fav);
387+
fav = csPath;
388+
// Get absolute path
389+
TCHAR buffer[4096] = TEXT("");
390+
GetFullPathName(fav, 4096, buffer, NULL);
391+
fav = buffer;
392+
}
393+
373394
Favorites.Add(fav);
374395
}
375396
}
@@ -379,6 +400,28 @@ void TegraRcm::GetFavorites()
379400
}
380401
void TegraRcm::AddFavorite(CString value)
381402
{
403+
// Get current directory
404+
CString csPath;
405+
TCHAR szPath[_MAX_PATH];
406+
VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH));
407+
CString csPathf(szPath);
408+
int nIndex = csPathf.ReverseFind(_T('\\'));
409+
if (nIndex > 0) csPath = csPathf.Left(nIndex);
410+
else csPath.Empty();
411+
412+
CT2A pPath(csPath.GetBuffer(csPath.GetLength()));
413+
CT2A pvalue(value.GetBuffer(value.GetLength()));
414+
char* rvalue = GetRelativeFilename(pPath, pvalue);
415+
value = rvalue;
416+
417+
/*
418+
if (value.Find(csPath) != -1)
419+
{
420+
csPath.Append(_T("\\"));
421+
value.Replace(csPath, _T(""));
422+
}
423+
*/
424+
382425
CString CoutLine(value + _T('\n'));
383426
CT2CA pszConvertedAnsiString(CoutLine);
384427
std::string outLine = pszConvertedAnsiString;
@@ -792,3 +835,110 @@ TCHAR* TegraRcm::GetAbsolutePath(TCHAR* relative_path, DWORD dwFlags)
792835
return _T("");
793836
*/
794837
}
838+
839+
840+
// GetRelativeFilename(), by Rob Fisher.
841+
842+
// http://come.to/robfisher
843+
// defines
844+
#define MAX_FILENAME_LEN 512
845+
// The number of characters at the start of an absolute filename. e.g. in DOS,
846+
// absolute filenames start with "X:\" so this value should be 3, in UNIX they start
847+
// with "\" so this value should be 1.
848+
#define ABSOLUTE_NAME_START 3
849+
// set this to '\\' for DOS or '/' for UNIX
850+
#define SLASH '\\'
851+
// Given the absolute current directory and an absolute file name, returns a relative file name.
852+
// For example, if the current directory is C:\foo\bar and the filename C:\foo\whee\text.txt is given,
853+
// GetRelativeFilename will return ..\whee\text.txt.
854+
char* TegraRcm::GetRelativeFilename(char *currentDirectory, char *absoluteFilename)
855+
{
856+
// declarations - put here so this should work in a C compiler
857+
int afMarker = 0, rfMarker = 0;
858+
int cdLen = 0, afLen = 0;
859+
int i = 0;
860+
int levels = 0;
861+
static char relativeFilename[MAX_FILENAME_LEN + 1];
862+
cdLen = strlen(currentDirectory);
863+
afLen = strlen(absoluteFilename);
864+
865+
// make sure the names are not too long or too short
866+
if (cdLen > MAX_FILENAME_LEN || cdLen < ABSOLUTE_NAME_START + 1 ||
867+
afLen > MAX_FILENAME_LEN || afLen < ABSOLUTE_NAME_START + 1)
868+
{
869+
return NULL;
870+
}
871+
872+
// Handle DOS names that are on different drives:
873+
if (currentDirectory[0] != absoluteFilename[0])
874+
{
875+
// not on the same drive, so only absolute filename will do
876+
strcpy(relativeFilename, absoluteFilename);
877+
return relativeFilename;
878+
}
879+
// they are on the same drive, find out how much of the current directory
880+
// is in the absolute filename
881+
i = ABSOLUTE_NAME_START;
882+
while (i < afLen && i < cdLen && currentDirectory[i] == absoluteFilename[i])
883+
{
884+
i++;
885+
}
886+
if (i == cdLen && (absoluteFilename[i] == SLASH || absoluteFilename[i - 1] == SLASH))
887+
{
888+
// the whole current directory name is in the file name,
889+
// so we just trim off the current directory name to get the
890+
// current file name.
891+
if (absoluteFilename[i] == SLASH)
892+
{
893+
// a directory name might have a trailing slash but a relative
894+
// file name should not have a leading one...
895+
i++;
896+
}
897+
strcpy(relativeFilename, &absoluteFilename[i]);
898+
return relativeFilename;
899+
}
900+
// The file is not in a child directory of the current directory, so we
901+
// need to step back the appropriate number of parent directories by
902+
// using "..\"s. First find out how many levels deeper we are than the
903+
// common directory
904+
afMarker = i;
905+
levels = 1;
906+
// count the number of directory levels we have to go up to get to the
907+
// common directory
908+
while (i < cdLen)
909+
{
910+
i++;
911+
if (currentDirectory[i] == SLASH)
912+
{
913+
// make sure it's not a trailing slash
914+
i++;
915+
if (currentDirectory[i] != '\0')
916+
{
917+
levels++;
918+
}
919+
}
920+
}
921+
// move the absolute filename marker back to the start of the directory name
922+
// that it has stopped in.
923+
while (afMarker > 0 && absoluteFilename[afMarker - 1] != SLASH)
924+
{
925+
afMarker--;
926+
}
927+
// check that the result will not be too long
928+
if (levels * 3 + afLen - afMarker > MAX_FILENAME_LEN)
929+
{
930+
return NULL;
931+
}
932+
933+
// add the appropriate number of "..\"s.
934+
rfMarker = 0;
935+
for (i = 0; i < levels; i++)
936+
{
937+
relativeFilename[rfMarker++] = '.';
938+
relativeFilename[rfMarker++] = '.';
939+
relativeFilename[rfMarker++] = SLASH;
940+
}
941+
// copy the rest of the filename into the result string
942+
strcpy(&relativeFilename[rfMarker], &absoluteFilename[afMarker]);
943+
return relativeFilename;
944+
}

TegraRcmGUI/TegraRcm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class TegraRcm
4040
void BitmapDisplay(int IMG);
4141
void LookUp();
4242
int Smasher(TCHAR args[]);
43-
43+
char* GetRelativeFilename(char *currentDirectory, char *absoluteFilename);
44+
4445
BOOL CmdShow = TRUE;
4546
// Notify Icon
4647
NOTIFYICONDATA m_NID;

TegraRcmGUI/TegraRcmGUI.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<PrecompiledHeader>Use</PrecompiledHeader>
9191
<WarningLevel>Level3</WarningLevel>
9292
<Optimization>Disabled</Optimization>
93-
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
<PreprocessorDefinitions>WIN32;_WINDOWS;_CRT_SECURE_NO_WARNINGS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9494
<SDLCheck>true</SDLCheck>
9595
<AdditionalIncludeDirectories>$(LIBUSBK_DIR)\includes\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
9696
</ClCompile>
@@ -145,7 +145,7 @@
145145
<Optimization>MaxSpeed</Optimization>
146146
<FunctionLevelLinking>true</FunctionLevelLinking>
147147
<IntrinsicFunctions>true</IntrinsicFunctions>
148-
<PreprocessorDefinitions>WIN32;_WINDOWS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
148+
<PreprocessorDefinitions>WIN32;_WINDOWS;_CRT_SECURE_NO_WARNINGS;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
149149
<SDLCheck>true</SDLCheck>
150150
<AdditionalIncludeDirectories>$(LIBUSBK_DIR)\includes\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
151151
</ClCompile>

0 commit comments

Comments
 (0)