@@ -24,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2424SOFTWARE.
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}
380401void 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+ }
0 commit comments