Exemplo n.º 1
0
bool Directory::open(const char * dirname)
{
    close();

    if (nullptr == dirname || '\0' == dirname[0])
    {
        return(false);
    }

    m_dir_name = dirname;
    if ('/' != *m_dir_name.rbegin() && '\\' != *m_dir_name.rbegin())
    {
        m_dir_name += g_directory_separator;
    }

#ifdef _MSC_VER
    std::wstring pattern(utf8_to_unicode(m_dir_name) + L"*");
    m_dir = FindFirstFileW(pattern.c_str(), &m_file);
    if (INVALID_HANDLE_VALUE == m_dir)
    {
        return(false);
    }
    m_eof = false;
#else
    m_dir = opendir(utf8_to_ansi(m_dir_name).c_str());
    if (nullptr == m_dir)
    {
        return(false);
    }
    m_file = readdir(m_dir);
#endif // _MSC_VER

    return(true);
}
Exemplo n.º 2
0
void AsinbowAPI::executeCommandWork(const std::string& command, const FB::JSObjectPtr& callback) {
    char buf[4096];
    FILE* pipe = popen(
#ifdef _WIN32
        utf8_to_ansi(command).c_str(),
#else
        command.c_str(),
#endif
        "r");
    if (!pipe) {
        callback->Invoke("", FB::variant_list_of(true)("popen failed!"));
        return;
    }
    std::string result;
    int c;
    while ((c = fgetc(pipe)) != EOF) {
        result += (char)c;
        if (c=='\n') {
#ifdef _WIN32
            result = ansi_to_utf8(result);
#endif
            callback->Invoke("", FB::variant_list_of(false)(result));
            result = "";
        }
    }
    pclose(pipe);
    callback->Invoke("", FB::variant_list_of(true));
}
Exemplo n.º 3
0
bool stupid_set_current_work_directory(const std::string & dirname)
{
#ifdef _MSC_VER
    std::string platform_dirname(dirname);
    stupid_directory_format(platform_dirname);
    return(0 == _wchdir(utf8_to_unicode(platform_dirname).c_str()));
#else
    std::string platform_dirname(dirname);
    stupid_directory_format(platform_dirname);
    return(0 == chdir(utf8_to_ansi(platform_dirname).c_str()));
#endif // _MSC_VER
}
Exemplo n.º 4
0
bool Directory::read()
{
    if (!is_open())
    {
        return(false);
    }

#ifdef _MSC_VER
    while (!m_eof)
    {
        const std::wstring file_name(m_file.cFileName);
        const DWORD file_type = m_file.dwFileAttributes;
        m_eof = !FindNextFileW(m_dir, &m_file);

        if (L"." == file_name || L".." == file_name)
        {
            continue;
        }

        if (FILE_ATTRIBUTE_DIRECTORY == (FILE_ATTRIBUTE_DIRECTORY & file_type))
        {
            m_current_sub_path_short_name = unicode_to_utf8(file_name);
            m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name + g_directory_separator;
            m_current_sub_path_is_directory = true;
        }
        else
        {
            m_current_sub_path_short_name = unicode_to_utf8(file_name);
            m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name;
            m_current_sub_path_is_directory = false;
        }

        return(true);
    }
#else
    while (nullptr != m_file)
    {
        /*
         * do not do like this:
         *     struct dirent * file = m_file;
         *     m_file = readdir(m_dir);
         *     operate_function(file);
         * the behavior is undefined, the result is not expected
         */
        const std::string d_name(m_file->d_name);
#if 0
        const size_t d_type = m_file->d_type;
#endif // 0
        m_file = readdir(m_dir);

        if ("." == d_name || ".." == d_name)
        {
            continue;
        }

#if 0
        /*
         * d_type: not supported by all filesystem
         */
        if (DT_DIR == (DT_DIR & d_type))
        {
            m_current_sub_path_short_name = ansi_to_utf8(d_name);
            m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name + g_directory_separator;
            m_current_sub_path_is_directory = true;
            return(true);
        }
        else if (DT_REG == (DT_REG & d_type))
        {
            m_current_sub_path_short_name = ansi_to_utf8(d_name);
            m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name;
            m_current_sub_path_is_directory = false;
            return(true);
        }
#else
        stupid_stat_t stat_buf = { 0x00 };
        const std::string file_name(utf8_to_ansi(m_dir_name) + d_name);
        if (0 != stupid_stat(file_name.c_str(), &stat_buf))
        {
            continue;
        }

        if (S_IFDIR == (S_IFDIR & stat_buf.st_mode))
        {
            m_current_sub_path_short_name = ansi_to_utf8(d_name);
            m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name + g_directory_separator;
            m_current_sub_path_is_directory = true;
            return(true);
        }
        else if (S_IFREG == (S_IFREG & stat_buf.st_mode))
        {
            m_current_sub_path_short_name = ansi_to_utf8(d_name);
            m_current_sub_path_name = m_dir_name + m_current_sub_path_short_name;
            m_current_sub_path_is_directory = false;
            return(true);
        }
#endif // 0
    }
#endif // _MSC_VER

    m_current_sub_path_short_name.clear();
    m_current_sub_path_name.clear();
    m_current_sub_path_is_directory = false;

    return(false);
}
Exemplo n.º 5
0
int CV8File::SaveBlockToTextFile(FILE *file_out, stBlock *pBlock, UINT mode)
{

	char *ansi_string = NULL;
	UINT ansi_len;

	ansi_len = utf8_to_ansi(pBlock->pData, &ansi_string, pBlock->DataSize);

	/*
	if (!ansi_len)
		return 0;
	char *cur_pos = ansi_string;

	//char spaces[] = "                                                 ";

	int tabs;

	if (0 && *cur_pos == '{') // ������ ����� ��� ���������������
	{
		int num_chars = 0;
		int open_brackets = 0;
		bool print_tabs = false;
		do
		{

			if (*cur_pos == '{')
			{
				fprintf(file_out, "{");
				open_brackets++;
			}
			else if (*cur_pos == '}')
			{
				fprintf(file_out, "\r\n");
				open_brackets--;
				for(tabs = 0; tabs < open_brackets; tabs++)
					fprintf(file_out, "|\t");
				fprintf(file_out, "}");
			}
			else if (*cur_pos == '\r')
			{
			}
			else if (*cur_pos == '\n')
			{
			}
			else if (*cur_pos == ',')
			{
				fprintf(file_out, ",\r\n");
				print_tabs = true;
			}
			else
				fprintf(file_out, "%c", *cur_pos);

			if (print_tabs)
			{
				for(tabs = 0; tabs < open_brackets; tabs++)
					fprintf(file_out, "|\t");
				print_tabs = false;
			}

			cur_pos++;

		}
		while (open_brackets != 0);

	}
	else
	*/

	if (ansi_len)
		fwrite(ansi_string, 1, ansi_len, file_out);
	else
		fwrite(pBlock->pData, 1, pBlock->DataSize, file_out);

	if (ansi_string)
		free(ansi_string);

	return 0;

}
Exemplo n.º 6
0
/*----------------------*
 * Paste from Clipboard *
 *----------------------*/
BOOL PasteClip (LONG x, struct line_node *actline, struct InstData *data)
{
  struct line_node *line = NULL;
  struct line_node *startline = NULL;
  struct line_node *previous = NULL;
  UWORD   *styles = NULL;
  UWORD   *colors = NULL;
  STRPTR  textline;
  BOOL newline = TRUE;
  BOOL res = FALSE;

  ENTER();

  if(InitClipboard(data, IFFF_READ))
  {
    if(StopChunk(data->iff, ID_FTXT, ID_CHRS) == 0 &&
       StopChunk(data->iff, ID_FTXT, ID_FLOW) == 0 &&
       StopChunk(data->iff, ID_FTXT, ID_HIGH) == 0 &&
       StopChunk(data->iff, ID_FTXT, ID_SBAR) == 0 &&
       StopChunk(data->iff, ID_FTXT, ID_COLS) == 0 &&
       StopChunk(data->iff, ID_FTXT, ID_STYL) == 0 &&
       StopChunk(data->iff, ID_FTXT, ID_CSET) == 0)
    {
      LONG error, codeset = 0;
      UWORD flow = MUIV_TextEditor_Flow_Left;
      UWORD color = FALSE;
      UWORD separator = 0;
      BOOL ownclip = FALSE;
      LONG updatefrom;

      while(TRUE)
      {
        struct ContextNode *cn;

        error = ParseIFF(data->iff, IFFPARSE_SCAN);
        SHOWVALUE(DBF_CLIPBOARD, error);
        if(error == IFFERR_EOC)
          continue;
        else if(error)
          break;

        if((cn = CurrentChunk(data->iff)) != NULL)
        {
          switch (cn->cn_ID)
          {
            case ID_CSET:
              D(DBF_CLIPBOARD, "reading FLOW");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);
              if(cn->cn_Size >= 4)
              {
                /* Only the first four bytes are interesting */
                if(ReadChunkBytes(data->iff, &codeset, 4) != 4)
                {
                  codeset = 0;
                }
                SHOWVALUE(DBF_CLIPBOARD, codeset);
              }
              break;

            case ID_FLOW:
              D(DBF_CLIPBOARD, "reading FLOW");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);
              if(cn->cn_Size == 2)
              {
                if(ReadChunkBytes(data->iff, &flow, 2) == 2)
                  if(flow > MUIV_TextEditor_Flow_Right)
                    flow = MUIV_TextEditor_Flow_Left;
                SHOWVALUE(DBF_CLIPBOARD, flow);
              }
              break;

            case ID_HIGH:
              D(DBF_CLIPBOARD, "reading HIGH");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);
              if (cn->cn_Size == 2)
              {
                error = ReadChunkBytes(data->iff, &color, 2);
                SHOWVALUE(DBF_CLIPBOARD, color);
                SHOWVALUE(DBF_CLIPBOARD, error);
              }
              break;

            case ID_SBAR:
              D(DBF_CLIPBOARD, "reading SBAR");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);
              if (cn->cn_Size == 2)
              {
                error = ReadChunkBytes(data->iff, &separator, 2);
                SHOWVALUE(DBF_CLIPBOARD, separator);
                SHOWVALUE(DBF_CLIPBOARD, error);
              }
              break;

            case ID_COLS:
              D(DBF_CLIPBOARD, "reading COLS");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);
              if(colors)
              {
                MyFreePooled(data->mypool, colors);
                colors = NULL;
              }
              // allocate one word more than the chunk tell us, because we terminate the array with an additional value
              if(cn->cn_Size > 0 && (colors = (UWORD *)MyAllocPooled(data->mypool, cn->cn_Size + sizeof(UWORD))) != NULL)
              {
                error = ReadChunkBytes(data->iff, colors, cn->cn_Size);
                SHOWVALUE(DBF_CLIPBOARD, error);
                colors[cn->cn_Size / 2] = 0xffff;
              }
              break;

            case ID_STYL:
              D(DBF_CLIPBOARD, "reading STYL");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);
              ownclip = TRUE;
              if(styles)
              {
                MyFreePooled(data->mypool, styles);
                styles = NULL;
              }
              // allocate one word more than the chunk tell us, because we terminate the array with an additional value
              if(cn->cn_Size > 0 && (styles = (UWORD *)MyAllocPooled(data->mypool, cn->cn_Size + sizeof(UWORD))) != NULL)
              {
                error = ReadChunkBytes(data->iff, styles, cn->cn_Size);
                SHOWVALUE(DBF_CLIPBOARD, error);
                styles[cn->cn_Size / 2] = EOS;
              }
              break;

            case ID_CHRS:
            {
              D(DBF_CLIPBOARD, "reading CHRS");
              SHOWVALUE(DBF_CLIPBOARD, cn->cn_Size);

              data->HasChanged = TRUE;

              if(cn->cn_Size > 0 && !ownclip)
              {
                char *contents;
                ULONG length = cn->cn_Size;

                if((contents = (char *)MyAllocPooled(data->mypool, length + 1)) != NULL)
                {
                  error = ReadChunkBytes(data->iff, contents, length);
                  SHOWVALUE(DBF_CLIPBOARD, error);

                  if(contents[length - 1] != '\n')
                  {
                    newline = FALSE;
                  }
                  else
                  {
                    length--;
                  }
                  contents[length] = '\0';

                  #if defined(__MORPHOS__)
                  if (codeset == CODESET_UTF8)
                  {
                    if (IS_MORPHOS2)
                      contents = utf8_to_ansi(data, contents);
                  }
                  #endif

                  if((line = ImportText(contents, data, &ImPlainHook, data->ImportWrap)))
                  {
                    if(!startline)
                      startline = line;
                    if(previous)
                      previous->next  = line;

                    line->previous    = previous;
                    line->visual    = VisualHeight(line, data);
                    data->totallines += line->visual;
                    while(line->next)
                    {
                      line = line->next;
                      line->visual    = VisualHeight(line, data);
                      data->totallines += line->visual;
                    }
                    previous = line;
                  }
                  MyFreePooled(data->mypool, contents);
                }
              }
              else
              {
                ULONG length = cn->cn_Size;

                if(length > 0 && (textline = (char *)MyAllocPooled(data->mypool, length + 2)) != NULL)
                {
                  error = ReadChunkBytes(data->iff, textline, length);
                  SHOWVALUE(DBF_CLIPBOARD, error);

                  if (textline[length - 1] != '\n')
                  {
                    newline = FALSE;
                    textline[length] = '\n';
                    length++;
                  }
                  textline[length] = '\0';

                  if((line = AllocLine(data)))
                  {
                    line->next     = NULL;
                    line->previous   = previous;
                    line->line.Contents   = textline;
                    line->line.Length   = length;
                    line->visual   = VisualHeight(line, data);
                    line->line.Color    = color;
                    line->line.Flow     = flow;
                    line->line.Separator = separator;
                    line->line.Styles   = styles;
                    line->line.Colors   = colors;
                    data->totallines += line->visual;

                    if(!startline)
                      startline = line;
                    if(previous)
                      previous->next  = line;

                    previous = line;
                  }
                  else
                  {
                    if(styles)
                      MyFreePooled(data->mypool, (void *)styles);
                    if(colors)
                      MyFreePooled(data->mypool, (void *)colors);
                  }
                }
                else
                {
                  if(styles)
                    MyFreePooled(data->mypool, styles);
                  if(colors)
                    MyFreePooled(data->mypool, (void *)colors);
                }
                styles    = NULL;
                colors    = NULL;
                flow      = MUIV_TextEditor_Flow_Left;
                color     = FALSE;
                separator = 0;
                ownclip   = FALSE;
              }
            }
            break;
          }
        }
      }

      if(line)
      {
        BOOL oneline = FALSE;

        SplitLine(x, actline, FALSE, NULL, data);
        line->next = actline->next;
        actline->next->previous = line;
        actline->next = startline;
        startline->previous = actline;
        data->CPos_X = line->line.Length-1;
        if(actline->next == line)
        {
          data->CPos_X += actline->line.Length-1;
          oneline = TRUE;
        }
        if(!newline)
          MergeLines(line, data);
        MergeLines(actline, data);
        if(oneline)
          line = actline;
        if(newline)
        {
          line = line->next;
          data->CPos_X = 0;
        }
        data->actualline = line;
      }
      else
      {
        switch(error)
        {
          case IFFERR_MANGLED:
          case IFFERR_SYNTAX:
          case IFFERR_NOTIFF:
            D(DBF_CLIPBOARD, "no FTXT clip!");
            DoMethod(data->object, MUIM_TextEditor_HandleError, Error_ClipboardIsNotFTXT);
            break;
          default:
            D(DBF_CLIPBOARD, "clipboard is empty!");
            DoMethod(data->object, MUIM_TextEditor_HandleError, Error_ClipboardIsEmpty);
            break;
        }
      }
      data->update = TRUE;

      ScrollIntoDisplay(data);
      updatefrom = LineToVisual(actline, data)-1;
      if(updatefrom < 0)
        updatefrom = 0;
      DumpText(data->visual_y+updatefrom, updatefrom, data->maxlines, TRUE, data);

      if(data->update)
        res = TRUE;
      else
        data->update = TRUE;
    }

    EndClipSession(data);
  }

  RETURN(res);
  return res;
}
Exemplo n.º 7
0
Arquivo: hw.cpp Projeto: is9117/os_hw1
void read_bob_txt(wchar_t *bob2_txt)
{
	// readfile
	// 파일 생성
	HANDLE file_handle = CreateFileW(
		bob2_txt,
		GENERIC_READ,
		FILE_SHARE_READ,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL);
	if (file_handle == INVALID_HANDLE_VALUE)
	{
		fprintf(stderr, "err, CreateFile(path=%ws), gle=0x%08x", bob2_txt, GetLastError());
		return;
	}

	unsigned char buf[512];
	DWORD nRead = 0;
	do {
		char ansi_str[1024];
		DWORD ansi_len;
		ReadFile(file_handle, buf, 512, &nRead, 0);
		utf8_to_ansi(buf, ansi_str, nRead, &ansi_len);
		for (DWORD i = 0; i < ansi_len; i++)
			fputc(ansi_str[i], stdout);

	} while (nRead != 0);

	// new line
	puts("");

	//filemap
	HANDLE hFileMap = CreateFileMapping(file_handle, NULL, PAGE_READONLY, 0, 0, NULL);
	if (hFileMap == NULL)
	{
		fprintf(stderr, "err, CreateFileMapping failed");
		return;
	}

	DWORD dwSize = GetFileSize(file_handle, 0);
	if (dwSize == NULL)
	{
		fprintf(stderr, "err, GetFileSize failed");
		return;
	}

	PVOID pBaseOfFile = (PVOID)MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, dwSize);
	if (pBaseOfFile == NULL)
	{
		fprintf(stderr, "err, MapViewOfFile failed");
		return;
	}
	unsigned char* buf2 = (unsigned char*)pBaseOfFile;
	nRead = 0;
	DWORD nToRead = 0;
	do {
		char ansi_str[1024];
		DWORD ansi_len;

		nToRead = (dwSize - nRead) > 512 ? 512 : (dwSize - nRead) % 512;

		utf8_to_ansi(buf2, ansi_str, nToRead, &ansi_len);
		for (DWORD i = 0; i < ansi_len; i++)
			fputc(ansi_str[i], stdout);
		nRead += nToRead;

	} while (nRead < dwSize);

	// new line
	puts("");

	UnmapViewOfFile(pBaseOfFile);	
	CloseHandle(hFileMap);
	CloseHandle(file_handle);
}