小编bob*_*ski的帖子

Delphi TDictionary迭代

我有一个函数,我存储一些键值对,当我迭代它们时,我得到两次错误:[dcc32错误] App.pas(137):E2149类没有默认属性.这是我的代码的一部分:

function BuildString: string;
var
  i: Integer;
  requestContent: TDictionary<string, string>;
  request: TStringBuilder;
begin
  requestContent := TDictionary<string, string>.Create();

  try
    // add some key-value pairs
    request :=  TStringBuilder.Create;
    try
      for i := 0 to requestContent.Count - 1 do
      begin
        // here I get the errors
        request.Append(requestContent.Keys[i] + '=' +
          TIdURI.URLEncode(requestContent.Values[i]) + '&');
      end;

      Result := request.ToString;
      Result := Result.Substring(0, Result.Length - 1); //remove the last '&'
    finally
      request.Free;
    end; 
  finally
    requestContent.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

我需要收集字典中每个项目的信息.我该如何解决?

delphi iteration tdictionary

18
推荐指数
1
解决办法
2万
查看次数

Perl恢复了一个帖子

我正在尝试启动一个我可以随时暂停/恢复的线程.以下是我创建线程的方法:

use strict;
use warnings;
use threads;
use threads::shared;
use Thread::Suspend;
use Thread::Semaphore;

sub createThread { 
    my $semaphore = Thread::Semaphore->new();
    my $thr = threads->create(sub {
        local $SIG{KILL} = sub { 
            die "Thread killed\n";
        };
        local $SIG{STOP} = sub { 
            print "sig stop\n";
            $semaphore->down();
        };
        local $SIG{CONT} = sub {
            $semaphore->up();
            print "sig cont\n";
        };

        my $test = sub {
            while (1) {
                $semaphore->down();
                print STDERR "Working process\n";
                sleep(2);
                $semaphore->up();
            }
        };

        return $test->();
    });

    return $thr->tid();
}
Run Code Online (Sandbox Code Playgroud)

检索线程ID(带return $thr->tid(); …

perl multithreading

8
推荐指数
1
解决办法
194
查看次数

上传图片到 HTTP 服务器

在我的 HTTP 服务器上,我试图允许用户通过这种形式上传图片:

<form action="/?command=saveImage" method="post" enctype="multipart/form-data">
  <input type="file" name="images" multiple="multiple"/>
  <input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)

我正在使用此源上传图像:http : //embarcadero.newsgroups.archived.at/public.delphi.internet.winsock/201107/1107276163.html并更改了 ProcessMimePart 程序以便以正确的格式保存图像:

procedure ProcessMimePart(var aDecoder: TIdMessageDecoder;
  var aMsgEnd: Boolean);
var
  LMStream: TMemoryStream;
  LNewDecoder: TIdMessageDecoder;
  fileName, fileExtension: string;
begin
  fileName := aDecoder.fileName;
  fileExtension := GetFileExtension(fileName);

  if (fileExtension <> 'jpg') and (fileExtension <> 'png') 
    and (fileExtension <> 'bmp') then
  begin
    Exit;
  end;

  LMStream := TMemoryStream.Create;
  try
    LNewDecoder := aDecoder.ReadBody(LMStream, aMsgEnd);
    try
      LMStream.Position := 0;
      TSaveImageController.WriteImage(fileName, fileExtension, LMStream);
    except
      LNewDecoder.Free;
      raise;
    end;
    aDecoder.Free;
    aDecoder …
Run Code Online (Sandbox Code Playgroud)

delphi file-upload indy httpserver multiple-file-upload

5
推荐指数
0
解决办法
1995
查看次数

Delphi TObjectDictionary 具有类实例键

我有以下几点class

TTest = class
private
  FId: Integer;
  FSecField: Integer;
  FThirdField: Integer;
public
  constructor Create(AId, ASecField, AThirdField: Integer);
  // ..... 
end;
Run Code Online (Sandbox Code Playgroud)

然后我创建一个TObjectDictionary这样的:

procedure TMainForm.btnTestClick(Sender: TObject);
var
  TestDict: TObjectDictionary<TTest, string>;
  Instance: TTest;
begin
  TestDict := TObjectDictionary<TTest, string>.Create([doOwnsKeys]);

  try
    TestDict.Add(TTest.Create(1, 1, 1), '');

    if TestDict.ContainsKey(TTest.Create(1, 1, 1)) then
      ShowMessage('Match found')
    else
      ShowMessage('Match not found');

    Instance := TTest.Create(1, 1, 1);
    TestDict.Add(Instance, 'str');

    if TestDict.ContainsKey(Instance) then
      ShowMessage('Match found')
    else
      ShowMessage('Match not found');
  finally
    TestDict.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

结果是:“找不到匹配项”、“找到匹配项”。我应该怎么做才能比较每个键的字段值而不是它们的地址?

delphi generics dictionary memory-address

5
推荐指数
1
解决办法
612
查看次数

如何在delphi中更改布尔数组的值

我正在使用 Delphi XE5 制作一个小的 Delphi 程序。在我的代码中有一个动态布尔数组,我无法更改某些数组元素的值。我尝试在设置长度后初始化数组,但没有帮助。这是代码的一部分:

procedure DoSomething(names: array of string);
var startWithA: array of Boolean;
    i: integer;
begin
    SetLength(startWithA, Length(names)); // each element is false by default
    for i := 0 to Length(names) - 1 do begin
       if (names[i].indexOf('A') = 0) then begin
          startWithA[i] := true; // the value is not changed after executing this line
       end;
    end;
end;
Run Code Online (Sandbox Code Playgroud)

arrays delphi

3
推荐指数
1
解决办法
3087
查看次数

HTTP客户端错误403

我有以下问题:当我点击网站上的按钮(http://domain-location.com)时,我收到回复给我的信息,并且网址已更改为http://domain-location.com?key=value.我想创建一个示例http客户端来轻松接收信息.这是我的代码:

function DoRequest(aVal: string): string;
 const DOMAIN = 'http://domain-location.com';
 var
   request: TIdHTTP;
   responseStream: TMemoryStream;
   responseLoader: TStringList;
   urlRequest: string;
 begin
   request := TIdHTTP.Create(nil);
   responseStream := TMemoryStream.Create;
   responseLoader := TStringList.Create;

   try
     try
       // accept ranges didn't help
       // request.Response.AcceptRanges := 'text/json';
       // request.Response.AcceptRanges := 'text/xml';
       urlRequest := DOMAIN + '?key=' + aVal;
       request.Get(urlRequest, responseStream);
       responseStream.Position := 0;
       responseLoader.LoadFromStream(responseStream);
       Result := responseLoader.Text;
     except on E: Exception do
       Result := e.Message;
     end;
   finally
     responseLoader.Free;
     responseStream.Free;
     request.Free;
   end;
 end;
Run Code Online (Sandbox Code Playgroud)

编辑在第一个答案后我编辑了我的功能(仍然无法正常工作): …

delphi httpclient indy10 http-status-code-403

3
推荐指数
1
解决办法
1102
查看次数

Delphi字典释放

我实现了以下类:

type
  TUtilProcedure = procedure(var AJsonValue: TJSONObject);

  TCallback = class
  private
    FName: string;
    FProcedure: TUtilProcedure;
    FAnnotation: string;
  public
    constructor Create(AName: string; AProcedure: TUtilProcedure; AAnnotation: string); overload;
    constructor Create(ACallback: TCallback); overload;
    property Name: string read FName;
    property Proc: TUtilProcedure read FProcedure;
    property Annotation: string read FAnnotation;
 end;
Run Code Online (Sandbox Code Playgroud)

然后我有一个全局变量:

procedures: TDictionary<string, TCallback>;
Run Code Online (Sandbox Code Playgroud)

OnFormActivate程序I初始化procedures变量:

procedures := TDictionary<string, TCallback>.Create();
procedures.Add('something', TCallback.Create('sth', @proc, 'annotation')); 
// ....
Run Code Online (Sandbox Code Playgroud)

然后在OnFormClose我释放它:

procedures.Clear;
procedures.Free;
Run Code Online (Sandbox Code Playgroud)

我的代码是否泄漏内存?如果是这样,什么是正确的释放方式dictionary?据我所知,迭代不是好主意.

delphi dictionary memory-leaks tdictionary

3
推荐指数
1
解决办法
3225
查看次数

用Delphi构建JSON

我正在尝试实现一个返回包含类元素的json对象的函数.这是我的功能:

procedure ListToJson(AInputList: TList<TRating>;
  AResponse: TJSONObject);
var
  i: Integer;
  jsonPair: TJSONPair;
  jsonObject: TJSONObject;
  jsonArray: TJSONArray;
begin
  jsonArray := TJSONArray.Create();
  jsonObject := TJSONObject.Create();
  jsonPair := TJSONPair.Create('ratings', jsonArray);

  for i := 0 to AInputList.Count - 1 do
  begin
    jsonObject.AddPair(TJSONPair.Create('idrating', IntToStr(AInputList[i].IdRating)));
    jsonObject.AddPair(TJSONPair.Create('idmark', IntToStr(AInputList[i].IdMark)));
    jsonObject.AddPair(TJSONPair.Create('value', IntToStr(AInputList[i].Value)));
    jsonObject.AddPair(TJSONPair.Create('description', AInputList[i].Description));
    jsonObject.AddPair(TJSONPair.Create('timeposted', FormatDateTime('yyyy-mm-dd hh:mm:ss', AInputList[i].TimePosted)));

    jsonArray.AddElement(jsonObject);
  end;

  AResponse.AddPair(jsonPair);
 end;
Run Code Online (Sandbox Code Playgroud)

当我用包含两个元素的列表测试它时,返回的字符串是:

{
  "ratings":[{
      "idrating":"1",
      "idmark":"0",
      "value":"0",
      "description":"",
      "timeposted":"2015-07-29 11:25:03",
      "idrating":"2",
      "idmark":"0",
      "value":"0",
      "description":"",
      "timeposted":"2015-07-29 11:25:24"
  },{
      "idrating":"1",
      "idmark":"0",
      "value":"0",
      "description":"",
      "timeposted":"2015-07-29 11:25:03",
      "idrating":"2",
      "idmark":"0",
      "value":"0",
      "description":"",
      "timeposted":"2015-07-29 11:25:24" …
Run Code Online (Sandbox Code Playgroud)

delphi json

2
推荐指数
1
解决办法
1万
查看次数

递归析构函数

我有以下两个类:

type
  TItemProp = class
  private
    FItemPropName: string;
    FItemPropValue: string;
  public
    constructor Create(AItemPropName, AItemPropValue: string);
    class function GetItemProp(AHTMLElement: OleVariant; AProp: string): TItemProp;
    property ItemPropName: string read FItemPropName;
    property ItemPropValue: string read FItemPropValue;
 end;

TTest = class
private
  FName: string;
  FProps: TList<TItemProp>;
  FTest: TList<TTest>;
public
  constructor Create(AName: string);
  destructor Destoroy();
  property Name: string read FName;
  property ItemProps: TList<TItemProp> read FProps write FProps;
  property Test: TList<TTest> read FTest write FTest;
end;
Run Code Online (Sandbox Code Playgroud)

这里是构造函数的代码和TTest类的析构函数:

constructor TTest.Create(AName: string);
begin
  Self.FName := AName;
  Self.FProps := TList<TItemProp>.Create(); …
Run Code Online (Sandbox Code Playgroud)

delphi destructor memory-leaks

1
推荐指数
1
解决办法
232
查看次数

打印给定目录的文件和子目录

我试图从给定的目录中获取所有文件和目录,但我不能指定什么是类型(文件/目录).什么都没打印出来.我做错了什么以及如何解决它.这是代码:

sub DoSearch {
    my $currNode = shift;
    my $currentDir = opendir (my $dirHandler, $currNode->rootDirectory) or die $!;

    while (my $node = readdir($dirHandler)) {
        if ($node eq '.' or $node eq '..') {
            next;
        }

        print "File: " . $node . "\n" if -f $node;
        print "Directory " . $node . "\n" if -d $node;
   }

   closedir($dirHandler);
}
Run Code Online (Sandbox Code Playgroud)

perl file readdir subdirectory

1
推荐指数
1
解决办法
491
查看次数