Beispiel #1
0
// -------------------------------------------------------------------------
//  Returns a pointer to the name of an rc file of the requested type.
//
//  Preconditions:
//      - MAXPATHLEN is set to the max. allowed path length
//      - fullPath points to a buffer of at least MAXPATHLEN
//
//  Returns:
//      - NULL if an error occurs while creating a directory
//      - Pointer to a static array containing the file name
// -------------------------------------------------------------------------
const char* GetRCFileName(FileTypes type)
{
   static std::string rcFiles[N_FILE_TYPES];
   static bool namesDetermined = false;

   if (!namesDetermined)
   {
      std::string nedit_home = GetEnv("NEDIT_HOME");
      if (nedit_home.empty())
      {  //  No NEDIT_HOME
         // Let's try if ~/.nedit is a regular file or not.
         std::string  legacyFile = buildFilePath(GetHomeDir(), hiddenFileNames[NEDIT_RC]);
         if (isRegFile(legacyFile))
         {
            // This is a legacy setup with rc files in $HOME
            for (int i = 0; i < N_FILE_TYPES; i++)
            {
               rcFiles[i] = buildFilePath(GetHomeDir(), hiddenFileNames[i]);
            }
         }
         else
         {
            // ${HOME}/.nedit does not exist as a regular file.
            // FIXME: Devices, sockets and fifos are ignored for now.
            std::string defaultNEditHome = buildFilePath(GetHomeDir(), DEFAULT_NEDIT_HOME);
            if (!isDir(defaultNEditHome))
            {
               /* Create DEFAULT_NEDIT_HOME */
#ifdef WIN32
               if (mkdir(defaultNEditHome.c_str()) != 0)
#else
               if (mkdir(defaultNEditHome.c_str(), 0777) != 0)
#endif
               {
                  perror("nedit: Error while creating rc file directory"
                         " $HOME/" DEFAULT_NEDIT_HOME "\n"
                         " (Make sure all parent directories exist.)");
                  return NULL;
               }
            }

            /* All set for DEFAULT_NEDIT_HOME, let's copy the names */
            for (int i = 0; i < N_FILE_TYPES; i++)
            {
               rcFiles[i] = buildFilePath(defaultNEditHome.c_str(), plainFileNames[i]);
            }
         }
      }
      else
      {  // $NEDIT_HOME is set.
         // FIXME: Is this required? Does VMS know stat(), mkdir()?
         if (!isDir(nedit_home))
         {
            /* Create $NEDIT_HOME */
#ifdef WIN32
            if (mkdir(nedit_home.c_str()) != 0)
#else
            if (mkdir(nedit_home.c_str(), 0777) != 0)
#endif
            {
               perror("nedit: Error while creating rc file directory $NEDIT_HOME\n"
                      "nedit: (Make sure all parent directories exist.)");
               return NULL;
            }
         }

         // All set for NEDIT_HOME, let's copy the names
         for (int i = 0; i < N_FILE_TYPES; i++)
         {
            rcFiles[i] = buildFilePath(nedit_home.c_str(), plainFileNames[i]);
         }
      }

      namesDetermined = true;
   }

   return rcFiles[type].c_str();
}
Beispiel #2
0
/*
**  Returns a pointer to the name of an rc file of the requested type.
**
**  Preconditions:
**      - MAXPATHLEN is set to the max. allowed path length
**      - fullPath points to a buffer of at least MAXPATHLEN
**
**  Returns:
**      - NULL if an error occurs while creating a directory
**      - Pointer to a static array containing the file name
**
*/
const char* GetRCFileName(int type)
{
    static char rcFiles[N_FILE_TYPES][MAXPATHLEN + 1];
    static int namesDetermined = False;

    if (!namesDetermined)
    {
        char* nedit_home;
        int i;

        if ((nedit_home = getenv("NEDIT_HOME")) == NULL)
        {
            /*  No NEDIT_HOME */
#ifdef VMS
            /* This is a default VMS setup */
            for (i = 0; i < N_FILE_TYPES; i++)
            {
                buildFilePath(rcFiles[i], "SYS$LOGIN", hiddenFileNames[i]);
            }
#else /* #ifdef VMS */
            /* Let's try if ~/.nedit is a regular file or not. */
            char legacyFile[MAXPATHLEN + 1];
            buildFilePath(legacyFile, GetHomeDir(), hiddenFileNames[NEDIT_RC]);
            if (isRegFile(legacyFile))
            {
                /* This is a legacy setup with rc files in $HOME */
                for (i = 0; i < N_FILE_TYPES; i++)
                {
                    buildFilePath(rcFiles[i], GetHomeDir(), hiddenFileNames[i]);
                }
            } else
            {
                /* ${HOME}/.nedit does not exist as a regular file. */
                /* FIXME: Devices, sockets and fifos are ignored for now. */
                char defaultNEditHome[MAXPATHLEN + 1];
                buildFilePath(defaultNEditHome, GetHomeDir(), DEFAULT_NEDIT_HOME);
                if (!isDir(defaultNEditHome))
                {
                    /* Create DEFAULT_NEDIT_HOME */
                    if (mkdir(defaultNEditHome, 0777) != 0)
                    {
                        perror("nedit: Error while creating rc file directory"
                                " $HOME/" DEFAULT_NEDIT_HOME "\n"
                                " (Make sure all parent directories exist.)");
                        return NULL;
                    }
                }

                /* All set for DEFAULT_NEDIT_HOME, let's copy the names */
                for (i = 0; i < N_FILE_TYPES; i++)
                {
                    buildFilePath(rcFiles[i], defaultNEditHome, plainFileNames[i]);
                }
            }
#endif /* #ifdef VMS */
        } else
        {
            /*  $NEDIT_HOME is set. */
#ifndef VMS
            /* FIXME: Is this required? Does VMS know stat(), mkdir()? */
            if (!isDir(nedit_home))
            {
                /* Create $NEDIT_HOME */
                if (mkdir(nedit_home, 0777) != 0)
                {
                    perror("nedit: Error while creating rc file directory $NEDIT_HOME\n"
                            "nedit: (Make sure all parent directories exist.)");
                    return NULL;
                }
            }
#endif /* #ifndef VMS */

            /* All set for NEDIT_HOME, let's copy the names */
            for (i = 0; i < N_FILE_TYPES; i++)
            {
                buildFilePath(rcFiles[i], nedit_home, plainFileNames[i]);
            }
        }

        namesDetermined = True;
    }

    return rcFiles[type];
}
Beispiel #3
0
/**********************************************************************
 * 函数名称: GetDirContents
 * 功能描述: 把某目录下所含的顶层子目录、顶层文件都记录下来,并且按名字排序
 * 输入参数: strDirName - 目录名(含绝对路径)
 * 输出参数: pptDirContents - (*pptDirContents)指向一个PT_DirContent数组,
 *                             (*pptDirContents)[0,1,...]指向T_DirContent结构体,
 *                             T_DirContent中含有"目录/文件"的名字等信息
 *            piNumber       - strDirName下含有多少个"顶层子目录/顶层文件",
 *                             即数组(*pptDirContents)[0,1,...]有多少项
 * 返 回 值: 0 - 成功
 *            1 - 失败
 * 修改日期        版本号     修改人	      修改内容
 * -----------------------------------------------
 * 2013/02/08	     V1.0	  韦东山	      创建
 ***********************************************************************/
int GetDirContents(char *strDirName, PT_DirContent **pptDirContents, int *piNumber)	
{
    PT_DirContent *aptDirContents;
	struct dirent **aptNameList;
	int iNumber;
	int i;
	int j;

	/* 扫描目录,结果按名字排序,存在aptNameList[0],aptNameList[1],... */
	/*int scandir( const char *dir, struct dirent **namelist,int (*filter) (const void *b), int ( * compare )( const struct dirent **, const struct dirent ** ) );
	  *函数scandir扫描dir目录下(不包括子目录)满足filter过滤模式的文件,返回的结果是compare函数经过排序
	  *的,并保存在namelist中。注意namelist是通过malloc动态分配内存的,所以在使用时要注意释放内存。 
	  *alphasort和versionsort是使用到的两种排序的函数
	  */
	iNumber = scandir(strDirName, &aptNameList, 0, alphasort);
	if (iNumber < 0)
	{
		DBG_PRINTF("scandir error : %s!\n", strDirName);
		return -1;
	}

	/* (iNumber - 2) 忽略".", ".."这两个目录 */
	/*先向系统申请分配PT_DirContent数组大小的内存空间*/
	aptDirContents = malloc(sizeof(PT_DirContent) * (iNumber - 2));
	if (NULL == aptDirContents)
	{
		DBG_PRINTF("malloc error!\n");
		return -1;
	}
	/* 			(*pptDirContents)指向一个PT_DirContent数组			*/
    *pptDirContents = aptDirContents;

	/*后向系统申请分配T_DirContent结构体大小的内存空间
	  T_DirContent中含有"目录/文件"的名字等信息*/
	for (i = 0; i < iNumber - 2; i++)
	{
		aptDirContents[i] = malloc(sizeof(T_DirContent));
		if (NULL == aptDirContents)
		{
			DBG_PRINTF("malloc error!\n");
			return -1;
		}
	}

	/* 先把目录挑出来存入aptDirContents */
	for (i = 0, j = 0; i < iNumber; i++)
	{
		/* 忽略".",".."这两个目录 */
		if ((0 == strcmp(aptNameList[i]->d_name, ".")) || (0 == strcmp(aptNameList[i]->d_name, "..")))
			continue;
        /* 并不是所有的文件系统都支持d_type, 所以不能直接判断d_type */
		/* if (aptNameList[i]->d_type == DT_DIR) */
        if (isDir(strDirName, aptNameList[i]->d_name))
		{
			strncpy(aptDirContents[j]->strName, aptNameList[i]->d_name, 256);
			aptDirContents[j]->strName[255] = '\0';
			aptDirContents[j]->eFileType    = FILETYPE_DIR;
            free(aptNameList[i]);
            aptNameList[i] = NULL;
			j++;
		}
	}

	/* 再把常规文件挑出来存入aptDirContents */
	for (i = 0; i < iNumber; i++)
	{
        if (aptNameList[i] == NULL)
            continue;
        
		/* 忽略".",".."这两个目录 */
		if ((0 == strcmp(aptNameList[i]->d_name, ".")) || (0 == strcmp(aptNameList[i]->d_name, "..")))
			continue;
        /* 并不是所有的文件系统都支持d_type, 所以不能直接判断d_type */
		/* if (aptNameList[i]->d_type == DT_REG) */
        if (isRegFile(strDirName, aptNameList[i]->d_name))
		{
			strncpy(aptDirContents[j]->strName, aptNameList[i]->d_name, 256);
			aptDirContents[j]->strName[255] = '\0';
			aptDirContents[j]->eFileType    = FILETYPE_FILE;
            free(aptNameList[i]);
            aptNameList[i] = NULL;
			j++;
		}
	}

	/* 释放aptDirContents中未使用的项 */
	for (i = j; i < iNumber - 2; i++)
	{
		free(aptDirContents[i]);
	}

	/* 释放scandir函数分配的内存 */
	for (i = 0; i < iNumber; i++)
	{
        if (aptNameList[i])
        {
    		free(aptNameList[i]);
        }
	}
	free(aptNameList);

	*piNumber = j;
	
	return 0;
}