RUBY   17
get param name
Guest on 7th March 2023 01:29:22 PM


  1. #!/usr/bin/ruby
  2.  
  3. require 'cgi'
  4.  
  5. class CGI
  6.   def get_param(name)
  7.     val = self.params[name]
  8.     return nil unless val
  9.     return nil if val.empty?
  10.     val.to_s
  11.   end
  12. end
  13.  
  14. def main
  15.   cgi = CGI.new
  16.   res = respond(cgi)
  17.   print "Content-Type: #{res.content_type}\r\n"
  18.   print "Content-Length: #{res.content_length}\r\n"
  19.   print "Cache-Control: no-cache\r\n"
  20.   print "Pragma: no-cache\r\n"
  21.   print "\r\n"
  22.   print res.body
  23. end
  24.  
  25. def respond(req)
  26.   q = req.query_string.to_s.strip
  27.   if /\A\d+,\d+\z/ =~ q
  28.     x, y = *q.split(',', 2).map {|n| [[0, n.to_i].max, 640].min }
  29.     return edit_response(x, y)
  30.   else
  31.     cmd = req.get_param('cmd').to_s.downcase
  32.     case cmd
  33.     when 'add'
  34.       x = req.get_param('x').to_i
  35.       y = req.get_param('y').to_i
  36.       text = req.get_param('text').to_s
  37.       add_node load_graph(), Node.new(x, y, text)
  38.       return view_response()
  39.     when 'img'
  40.       return image_response()
  41.     else
  42.       return view_response()
  43.     end
  44.   end
  45. end
  46.  
  47. CGI_URL = File.basename($0)
  48.  
  49. def view_response
  50.   Response.new('text/html', <<HTML)
  51. <html>
  52. <head><title>view</title></head>
  53. <p>
  54. <a href="#{CGI_URL}">
  55. <img ismap src="#{CGI_URL}?cmd=img" width="640" height="640">
  56. </a>
  57. </p>
  58. </html>
  59. HTML
  60. end
  61.  
  62. def image_response
  63.   Response.new('image/png', File.read(IMAGE_FILE))
  64. end
  65.  
  66. def edit_response(x, y)
  67.   Response.new('text/html; charset="euc-jp"', <<HTML)
  68. <html>
  69. <head><title>edit</title></head>
  70. <body>
  71. <p>日本語通りません</p>
  72. <form method#{CGI_URL}">
  73. n="#{CGI_URL}">
  74. <input type="hidden" name=">
  75. <input type="">
  76. <input type=" value="#{x}">
  77. " value="#{x}">
  78. <input type=" value="#{y}">
  79. " value="#{y}">
  80. <textarea name="text" cols="40" rows="3"></textarea>
  81. <input type="submit" id="write" value="Add">
  82. </form>
  83. </body>
  84. </html>
  85. HTML
  86. end
  87.  
  88. class Response
  89.   def initialize(type, body)
  90.     @type = type
  91.     @body = body
  92.   end
  93.  
  94.   def content_type
  95.     @type
  96.   end
  97.  
  98.   def content_length
  99.     @body.length
  100.   end
  101.  
  102.   attr_reader :body
  103. end
  104.  
  105. require 'log'
  106. LOGFILE = 'log'
  107. IMAGE_FILE = 'graph.png'
  108.  
  109. def load_graph
  110.   File.open(LOGFILE) {|f|
  111.     return Marshal.load(f)
  112.   }
  113. rescue
  114.   return Graph.new
  115. end
  116.  
  117. def add_node(graph, node)
  118.   lock {
  119.     graph.add node
  120.    #{$$}", 'w') {|f|
  121. $$}", 'w') {|f|
  122.       f.write generate_image(graph).png(f)
  123.     }
  124.     F#{$$}", IMAGE_FILE
  125. $}", IMAGE_FILE
  126.     File.open(LOGFILE, 'w') {|f|
  127.       Marshal.dump(graph, f)
  128.     }
  129.   }
  130. end
  131.  
  132. INIT_W = 640
  133. INIT_H =#000000'
  134. LOR = '#000#ffffff'
  135. LOR = '#ffffff'
  136.  
  137. def generate_image(graph)
  138.   image = GD::Image.new(640, 640)
  139.   image.transparent(image#fffffe'))
  140. ate('#fffffe'))
  141.   image.interlace = true
  142.   fgcolor = image.colorAllocate(FGCOLOR)
  143.   bgcolor = image.colorAllocate(BGCOLOR)
  144.   graph.each_node do |n|
  145.     image.filledRectangle n.x, n.y, n.x2, n.y2, bgcolor
  146.     image.rectangle       n.x, n.y, n.x2, n.y2, fgcolor
  147.     n.each_text_line do |line, x, y|
  148.       image.string GD::Font::MediumFont, x, y, line, fgcolor
  149.     end
  150.   end
  151.   image
  152. end
  153.  
  154. def lock
  155.   File.open('lockfile') {|f|
  156.     begin
  157.       f.flock(File::LOCK_EX)
  158.       yield
  159.     ensure
  160.       f.flock(File::LOCK_UN)
  161.     end
  162.   }
  163. end
  164.  
  165. class Graph
  166.   def initialize
  167.     @nodes = []
  168.   end
  169.  
  170.   def add(node)
  171.     @nodes.push node
  172.   end
  173.  
  174.   def width
  175.     @nodes.map {|n| n.x }.max
  176.   end
  177.  
  178.   def height
  179.     @nodes.map {|n| n.y }.max
  180.   end
  181.  
  182.   def each_node(&block)
  183.     @nodes.each(&block)
  184.   end
  185. end
  186.  
  187. class Node
  188.   def initialize(x, y, text)
  189.     @x = x
  190.     @y = y
  191.     @edges = []
  192.     @lines = text.map {|line| line.rstrip }
  193.     @created_at = Time.now
  194.   end
  195.  
  196.   attr_reader :x
  197.   attr_reader :y
  198.   attr_reader :created_at
  199.  
  200.   def each_text_line(&block)
  201.     y = @y + GAP_H
  202.     @lines.each do |line|
  203.       yield line, @x + GAP_W, y
  204.       y += CHAR_HEIGHT + 1
  205.     end
  206.   end
  207.  
  208.   CHAR_WIDTH = 7
  209.   CHAR_HEIGHT = 13
  210.   GAP_W = 6
  211.   GAP_H = 3
  212.  
  213.   def width
  214.     @lines.map {|line| CHAR_WIDTH * line.size }.max + GAP_W * 2
  215.   end
  216.  
  217.   def height
  218.     CHAR_HEIGHT * @lines.size + GAP_H * 2
  219.   end
  220.  
  221.   def x2
  222.     @x + width()
  223.   end
  224.  
  225.   def y2
  226.     @y + height()
  227.   end
  228.  
  229.   def each_edge(&block)
  230.     @edges.each(&block)
  231.  

Raw Paste

Login or Register to edit or fork this paste. It's free.