lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, 9 Apr 2004, Alexandre wrote:
> There any way to move, create and rename files and folder with LUA?

Look the os.* functions. Check os.execute.

Although you can do everything using os.execute, it may not be fast 
enough (it really opens another shell to execute your commands).
I am sending attached a binding that provides most of these 
functionalities.

> Other, there any way to sort a list of strings???

Look table.sort in the manual.

Regards,
Mark

static int _ren(const char* fromName, const char* toName) {
#ifdef WIN32
   return MoveFileEx(fromName, toName, 
      MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED);
#else
   sprintf(comm_buffer, "/bin/mv %s %s", fromName, toName);
   int status = system(comm_buffer);
   if (status != 0) return 0;
   return 1;
#endif
}


static int _rmfile(const char* fileName) {
#ifdef WIN32
   return DeleteFile(fileName);
#else
   sprintf(comm_buffer, "/bin/rm -f %s", fileName);
   int status = system(comm_buffer);
   if (status != 0) return 0;
   return 1;
#endif
}

static int _rmdir(const char* dirName) {
#ifdef WIN32
   return RemoveDirectory(dirName);
#else
   sprintf(comm_buffer, "/bin/rmdir %s", dirName);
   int status = system(comm_buffer);
   if (status != 0) return 0;
   return 1;
#endif
}

static int _mkdir(const char* dirName) {
#ifdef WIN32
   return CreateDirectory(dirName, NULL);
#else
   sprintf(comm_buffer, "/bin/mkdir %s", dirName);
   int status = system(comm_buffer);
   if (status != 0) return 0;
   return 1;
#endif
}

// ......................................................................
// . mac Lua functions
// ......................................................................

static int lua_getCwd(lua_State* L) {
   const int max = 512;
   static char curr[max];
#ifdef WIN32
   _getcwd(curr, max);
#else
   getcwd(curr, max);
#endif
   lua_pushstring(L, curr);
   return 1;
}

static int lua_mkDir(lua_State* L) {
   char *dirName = (char *)lua_tostring(L, 1);
   int status = _mkdir((const char*)dirName);
   lua_pushnumber(L, status);
   return 1;
}

static int lua_chDirUp(lua_State* L) {
#ifdef WIN32
   int status = _chdir((const char*)"..");
#else
   int status = chdir((const char*)"..");
#endif
   lua_pushnumber(L, status);
   return 1;
}

static int lua_renItem(lua_State* L) {
   char* fromName = (char *)lua_tostring(L, 1);
   char* toName = (char *)lua_tostring(L, 2);
   int status = _ren(fromName, toName);
   lua_pushnumber(L, status);
   return 1;
}

static int lua_chDir(lua_State* L) {
   char *dirName = (char *)lua_tostring(L, 1);
#ifdef WIN32
   int status = _chdir((const char*)dirName);
#else
   int status = chdir((const char*)dirName);
#endif
   lua_pushnumber(L, status);
   return 1;
}

static int lua_fileSize(lua_State* L) {
   char *fileName = (char *)lua_tostring(L, 1);
   FILE* file;
   unsigned long fileSize;
   unsigned long currPosition;
   
   file = fopen(fileName, "rb");
   if (!file) {
      lua_pushnumber(L, -1.0);
      return 1;
   }

   currPosition = ftell(file);
   fseek(file, 0L, SEEK_END);
   fileSize = ftell(file);
   fseek(file, currPosition, SEEK_SET);

   fclose(file);
   lua_pushnumber(L, fileSize);
   return 1;
}

static int lua_listDir(lua_State* L) {
#ifdef WIN32
  HANDLE FileFile;
  WIN32_FIND_DATA inf;
  BOOL Find = true;
  char* mask;

  /* prepara a mascara */
  if (lua_typename(L, 1) == "no value")
    mask = ".\\\\*";
  else if (!lua_isstring(L, 1))
    luaCompat_error(L, "String Expected");
  else {
    mask = (char *) malloc(sizeof(char)*lua_strlen(L, 1) +3);
    strcpy(mask, lua_tostring(L, 1));
    strcat(mask, "\\*");
  }
  
  FileFile = FindFirstFile(mask, &inf);

  /* Se nao achou o FindFirstFile */
  if (FileFile == INVALID_HANDLE_VALUE) {
     lua_newtable(L);
     return 1;
  }

  lua_newtable(L);
  Find = FindNextFile(FileFile, &inf);
  int idx = 1;
  while (Find) {
    lua_newtable(L);
    lua_pushstring(L, "name");
    lua_pushstring(L, inf.cFileName);
    lua_settable(L, -3);

    /* verifica se  diretorio */
    lua_pushstring(L, "is_directory");
    if (inf.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) lua_pushnumber(L, 
1.0);
    else lua_pushnil(L);
    lua_settable(L, -3);

    Find = FindNextFile (FileFile, &inf);  
    if (!Find && GetLastError() != ERROR_NO_MORE_FILES) {
       FindClose(FileFile);
       lua_newtable(L);
       return 1;
    }
    else {
      assert(lua_istable(L, -1));
      assert(lua_istable(L, -2));
      lua_rawseti(L, -2, idx);
      idx++;
    }
  }
  FindClose(FileFile);
  return 1;

#else
   if (lua_typename(L, 1) == "no value")
     DIR* dir = opendir((const char*)".");
   else if (!lua_isstring(L, 1))
     lua_error(L, "String Expected");
   else {
     DIR* dir = opendir((const char*) lua_tostring(L, 1));
   }  
   if (dir == NULL) return 0;
   struct dirent* dir = readdir(dir);
   if (dir == NULL) return 0;
   lua_newtable(L);
   int idx = 1;
   while (dir != NULL) {
      struct stat status;
      int flag = stat(dir->d_name, &status);
      lua_newtable(L);
      if (flag == 0) {
         lua_pushstring(L, "is_directory");
         if (S_ISDIR(status.st_mode)) lua_pushnumber(L, 1.0);
         else lua_pushnil(L);
         lua_settable(L, -3);

         lua_pushstring(L, "name");
         lua_pushstring(L, dir->d_name);
         lua_settable(L, -3);
      }
      assert(lua_istable(L, -1));
      assert(lua_istable(L, -2));
      lua_rawseti(L, -2, idx);

      dir = readdir(dir);
      idx++;
   }
   closedir(dir);
#endif
   return 1;
}

static int lua_rmFile(lua_State* L) {
   char *fileName = (char *)lua_tostring(L, 1);
   int status = _rmfile((const char*)fileName);
   lua_pushnumber(L, status);
   return 1;
}

static int lua_rmDir(lua_State* L) {
   char* dirName = (char*)lua_tostring(L, 1);
   int status = _rmdir((const char*)dirName);
   lua_pushnumber(L, status);
   return 1;
}

void OsUtilLua(lua_State *L)
{
  lua_register(L, "C_getCwd", lua_getCwd);
  lua_register(L, "C_mkDir", lua_mkDir);
  lua_register(L, "C_rmDir", lua_rmDir);
  lua_register(L, "C_chDir", lua_chDir);
  lua_register(L, "C_renItem", lua_renItem);
  lua_register(L, "C_rmFile", lua_rmFile);
  lua_register(L, "C_chDirUp", lua_chDirUp);
  lua_register(L, "C_listDir", lua_listDir);
  lua_register(L, "C_fileSize", lua_fileSize);
}