/*----------------------------------------------------------------------------+
 |       ___     _       _                                                    |
 |      /   |   | |     | |                                                   |
 |     / /| | __| | ___ | |__   ___                                           |
 |    / /_| |/ _  |/ _ \|  _ \ / _ \                                          |
 |   / ___  | (_| | (_) | |_) |  __/                                          |
 |  /_/   |_|\__,_|\___/|____/ \___|                                          |
 |                                                                            |
 |                                                                            |
 |  ADOBE CONFIDENTIAL                                                        |
 |  __________________                                                        |
 |                                                                            |
 |  Copyright (c) 2003 - 2007, Adobe Systems Incorporated.                    |
 |  All rights reserved.                                                      |
 |                                                                            |
 |  NOTICE:  All information contained herein is, and remains the property    |
 |  of Adobe Systems Incorporated and its suppliers, if any. The intellectual |
 |  and technical concepts contained herein are proprietary to Adobe Systems  |
 |  Incorporated and its suppliers and may be covered by U.S. and Foreign     |
 |  Patents, patents in process, and are protected by trade secret or         |
 |  copyright law. Dissemination of this information or reproduction of this  |
 |  material is strictly forbidden unless prior written permission is         |
 |  obtained from Adobe Systems Incorporated.                                 |
 |                                                                            |
 |          Adobe Systems Incorporated       415.832.2000                     |
 |          601 Townsend Street              415.832.2020 fax                 |
 |          San Francisco, CA 94103                                           |
 |                                                                            |
 +----------------------------------------------------------------------------*/

#include "StdAfx.h"
#include "SimpleFileAdaptor.h"

#ifdef WIN32
	inline static int rmdir( const char* file )		{ return _rmdir(file); }
#endif

bool SimpleFileAdaptor::isAbsolutePath( const char* strPath )
{
	int len;
	if (!strPath || !(len = strlen(strPath)))
		return false;

#if defined(WIN32)
	if (strstr(strPath, "\\\\") != 0) 
	{
		return true; 
	}
	if (len > 1 && strstr(strPath, ":\\") != 0)
	{
		return true;
	}
#else
	if (*strPath == '/')
	{
		return true;
	}
#endif
	return false;
}

bool SimpleFileAdaptor::isAttributeAllExist(int* pAttrKeys, int nAttrs)
{	
	//check for kAll
	for (int i = 0; i < nAttrs; i++)
	{
		if (pAttrKeys[i] == FmsFileAttribute::kAll)
		{
			return true;
		}
	}
	return false;
}

int SimpleFileAdaptor::removeDir(const char* sFileName, unsigned long uFlags)
{	
	if (!sFileName || !strlen(sFileName))
		return -1;
	
	//this function should walk through all subdirectories deleting files
	//in each one and remove each subdirectory untill sFileName directory will be empty.
	//and finally remove sFileName directory by calling
	return ::rmdir(sFileName);
}

void SimpleFileAdaptor::setupFileAttribute(int* pAttrKeys, FmsFileAttribute* pAttribute, 
	int nSize, stbuf* pBuffer)
{
	if (!pAttrKeys || !pAttribute | !pBuffer | !nSize)
		return;
	
	int nLen = nSize;
	int i;

	//check kAll
	if (isAttributeAllExist(pAttrKeys, nSize))
	{
		for (i = 0; i < nLen; i++)
		{
			switch(i)
			{
				case FmsFileAttribute::kAll:
					pAttribute[i].attr = FmsFileAttribute::kAll;
					pAttribute[i].value.u32 = 0;
				break;
				case FmsFileAttribute::kTimeCreated:
					pAttribute[i].attr = FmsFileAttribute::kTimeCreated;
					pAttribute[i].value.u32 = (U32)pBuffer->st_ctime;
				break;
				case FmsFileAttribute::kTimeModified:
					pAttribute[i].attr = FmsFileAttribute::kTimeModified;
					pAttribute[i].value.u32 = (U32)pBuffer->st_mtime;
				break;
				case FmsFileAttribute::kTimeAccessed:
					pAttribute[i].attr = FmsFileAttribute::kTimeAccessed;
					pAttribute[i].value.u32 = (U32)pBuffer->st_atime;
				break;
				case FmsFileAttribute::kSize:
					pAttribute[i].attr = FmsFileAttribute::kSize;
					pAttribute[i].value.i64 = pBuffer->st_size;
				break;
				case FmsFileAttribute::kMode:
					pAttribute[i].attr = FmsFileAttribute::kMode;
					if (S_ISDIR(pBuffer->st_mode))
						pAttribute[i].value.u8 = 1;
					else
						pAttribute[i].value.u8 = 0;
				break;
			}
		}
		return;
	}

	i=0;
	while (nLen > 0)
	{
		switch(pAttrKeys[i])
		{
		case FmsFileAttribute::kTimeCreated:
			pAttribute[i].attr = FmsFileAttribute::kTimeCreated;
			pAttribute[i].value.u32 = (U32)pBuffer->st_ctime;
			break;

		case FmsFileAttribute::kTimeModified:
			pAttribute[i].attr = FmsFileAttribute::kTimeModified;
			pAttribute[i].value.u32 = (U32)pBuffer->st_mtime;
			break;

		case FmsFileAttribute::kTimeAccessed:
			pAttribute[i].attr = FmsFileAttribute::kTimeAccessed;
			pAttribute[i].value.u32 = (U32)pBuffer->st_atime;
			break;

		case FmsFileAttribute::kSize:
			pAttribute[i].attr = FmsFileAttribute::kSize;
			pAttribute[i].value.i64 = pBuffer->st_size;
			break;
		
		case FmsFileAttribute::kMode:
			pAttribute[i].attr = FmsFileAttribute::kMode;
			if (S_ISDIR(pBuffer->st_mode))
				pAttribute[i].value.u8 = 1;
			else
				pAttribute[i].value.u8 = 0;
			break;
		}
		nLen--;
		i++;
	}
}

