/*
//
//   ADOBE SYSTEMS INCORPORATED
//   Copyright (C) 2000-2003 Adobe Systems Incorporated
//   All rights reserved.
//
//   NOTICE: Adobe permits you to use, modify, and distribute this file
//   in accordance with the terms of the Adobe license agreement
//   accompanying it. If you have received this file from a source other
//   than Adobe, then your use, modification, or distribution of it
//   requires the prior written permission of Adobe.
//
*/

#include "PeddlerUtils.h"
#include <iostream>

#define DEBUGON 0

#define MACDELIM ":"
#define UNIXDELIM "/"
#define WINDELIM "\\"
#define UNIXPDIR "../"
#define WINPDIR "..\\"
#define MACPDIR "::"

string  
PeddlerUtils::getSysPath( string  path){

#if DEBUGON
               cout <<  "Input string "  << path << endl;
#endif

#if WIN_PLATFORM
                if  (PeddlerUtils::IsWinPath(path)) {
                                // don't need to do anything
               }
                else   if  (PeddlerUtils::IsUnixPath(path)){
                                ASSize_t  pos;
                                while  ((pos = path.find(UNIXDELIM)) != string::npos)
                                               path.replace(pos,1,WINDELIM);  // replace all UNIXDELIM chars with WINDELIM chars
                                // Want to replace \ with /
               }
                else  {  //mac path
                                ASSize_t  pos;
                                while  ((pos = path.find(MACPDIR)) != string::npos)
                                               path.replace(pos,2,WINPDIR);    // replace all :: chars with '..\'
                                while  ((pos = path.find(MACDELIM)) != string::npos)
                                               path.replace(pos,1,WINDELIM);    // replace all :: chars with '..\'
               }
               
#elif MAC_PLATFORM
                if  (PeddlerUtils::IsMacPath(path)) {
                                // don't need to do anything
               }
                else   if  (PeddlerUtils::IsUnixPath(path)){
                                ASSize_t  pos;
                                while  ((pos = path.find(UNIXPDIR)) != string::npos)
                                               path.replace(pos,3,MACPDIR);    // replace all ../ chars with ::
                                while  ((pos = path.find(UNIXDELIM)) != string::npos)
                                               path.replace(pos,1,MACDELIM);  // replace all UNIXDELIM chars with MACDELIM chars
               }
                else  {  //win path
                                ASSize_t  pos;
                                while  ((pos = path.find(WINPDIR)) != string::npos)
                                               path.replace(pos,3,MACPDIR);    // replace all ..\ chars with ::
                                while  ((pos = path.find(WINDELIM)) != string::npos)
                                               path.replace(pos,1,MACDELIM);  // replace all WINDELIM chars with MACDELIM chars
               }

#else // must be unix
                if  (PeddlerUtils::IsUnixPath(path)) {
                                // don't need to do anything
               }
                else   if  (PeddlerUtils::IsWinPath(path)){
                                ASSize_t  pos;
                                while  ((pos = path.find(WINDELIM)) != string::npos)
                                               path.replace(pos,1,UNIXDELIM);    // replace all \ chars with /
               }
                else  {  //mac path
                                ASSize_t  pos;
                                while  ((pos = path.find(MACPDIR)) != string::npos)
                                               path.replace(pos,2,UNIXPDIR);    // replace all :: chars with ../
                                while  ((pos = path.find(MACDELIM)) != string::npos)
                                               path.replace(pos,1,UNIXDELIM);    // replace all :\ chars with /
               }
#endif

#if DEBUGON
               cout <<  "Output string becomes " << path << endl;
#endif
                return  path;
}


ASBool  
PeddlerUtils::IsUnixPath( string  path){
                // it is a unix path if it does not have a mac or win delimiter
                // this allows us to use a relative path consisting purely of the filename as
                // a valid unix path
                if  ((path.find(MACDELIM) == string::npos) && (path.find(WINDELIM)==string::npos)) {
                                return   true ;
               }
                else  {
                                return   false ;
               }
}

ASBool  
PeddlerUtils::IsMacPath( string  path){
                // it is a mac path if it does not have a win delimiter
                // this allows us to use a relative path consisting purely of the filename as
                // a valid mac path
                if  (path.find(WINDELIM)==string::npos) {
                                return   true ;
               }
                else  {
                                return   false ;
               }
}


ASBool  
PeddlerUtils::IsWinPath( string  path){
                // it is a win path if it does not have a unix or mac delimiter
                // this allows us to use a relative path consisting purely of the filename as
                // a valid win path
                if  ((path.find(UNIXDELIM) == string::npos) && (path.find(MACDELIM)==string::npos)) {
                                return   true ;
               }
                else  {
                                return   false ;
               }
}