about summary refs log blame commit diff stats
path: root/html/088file.mu.html
blob: 46d9363a28ced5c737acc60c718d8981e83bc8ae (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15














                                                                                                 
                           
                              
                              
                            
                             
                            
                             










                               
                                                                                                        

                                                       
                                                 
           
                       

 
                                                

               

 
                                                                                                                                                         

                                                
                                  


                                                                                                     

                                         









                                                                                                                                                                             


                                                                                                                    
                                  
                                                                         
                                                 

                                                                               

                                                                                                            
                                                     

                                                                                                                                    
                                                       


                                                                                 

 
                                                                                                                                             


                                                
                                                                             
                                                
                                                         

                                       

                                                          

 
                                                                                                                                                  

                                                

                                                                               
                                  
                                                                         
                                                 


                                                                               

                                       
                                                    

 
                                                                                                                                                                   

                                                
                                                                                                                                
                                  

                                                         
                                                                                                                 

                                        



                                                                                           

 
                                                                                                                                                    


                                                
                                                                             



                                                 
                                                          

 
                                                                                                                                                                                                                    

                                                

                                                                                                                     
                                                          
                                                                                                 
                                  
                                                                             
                                                 
                                                        

                                       

                                                                                   
                                                   

                                                                                                                    
                                                                           

                                                                               
                                  
                                                                         
                                                 


                                                                                                       
                                                     
                                    
              


                                                                               

                                                                                                                       
                                                                     
                                                    
                                                                               
                                  
                                                                         
                                                 
                                                                  


                                               
                                        
            




                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 088file.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.muData { color: #ffff00; }
.muControl { color: #c0a020; }
.Delimiter { color: #800080; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muRecipe { color: #ff8700; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Wrappers around file system primitives that take a 'resources' object and</span>
<span class="Comment"># are thus easier to test.</span>

<span class="muData">container</span> resources [
  lock:bool
  data:&amp;:@:resource
]

<span class="muData">container</span> resource [
  name:text
  contents:text
]

<span class="muRecipe">def</span> start-reading resources:&amp;:resources, filename:text<span class="muRecipe"> -&gt; </span>contents:&amp;:source:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> resources
    <span class="Comment"># fake file system</span>
    contents <span class="Special">&lt;-</span> start-reading-from-fake-resources resources, filename
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># real file system</span>
  file:num <span class="Special">&lt;-</span> $open-file-for-reading filename
  assert file, <span class="Constant">[file not found]</span>
  contents:&amp;:source:char, sink:&amp;:sink:char <span class="Special">&lt;-</span> new-channel<span class="Constant"> 30</span>
  start-running receive-from-file file, sink
]

<span class="muRecipe">def</span> start-reading-from-fake-resources resources:&amp;:resources, resource:text<span class="muRecipe"> -&gt; </span>contents:&amp;:source:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  data:&amp;:@:resource <span class="Special">&lt;-</span> get *resources, <span class="Constant">data:offset</span>
  len:num <span class="Special">&lt;-</span> length *data
  <span class="Delimiter">{</span>
    done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    tmp:resource <span class="Special">&lt;-</span> index *data, i
    i <span class="Special">&lt;-</span> add i,<span class="Constant"> 1</span>
    curr-resource:text <span class="Special">&lt;-</span> get tmp, <span class="Constant">name:offset</span>
    found?:bool <span class="Special">&lt;-</span> equal resource, curr-resource
    <span class="muControl">loop-unless</span> found?
    contents:&amp;:source:char, sink:&amp;:sink:char <span class="Special">&lt;-</span> new-channel<span class="Constant"> 30</span>
    curr-contents:text <span class="Special">&lt;-</span> get tmp, <span class="Constant">contents:offset</span>
    start-running receive-from-text curr-contents, sink
    <span class="muControl">return</span>
  <span class="Delimiter">}</span>
  <span class="muControl">return</span> <span class="Constant">0/not-found</span>
]

<span class="muRecipe">def</span> receive-from-file file:num, sink:&amp;:sink:char<span class="muRecipe"> -&gt; </span>sink:&amp;:sink:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    c:char, eof?:bool <span class="Special">&lt;-</span> $read-from-file file
    <span class="muControl">break-if</span> eof?
    sink <span class="Special">&lt;-</span> write sink, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  sink <span class="Special">&lt;-</span> close sink
  file <span class="Special">&lt;-</span> $close-file file
]

<span class="muRecipe">def</span> receive-from-text contents:text, sink:&amp;:sink:char<span class="muRecipe"> -&gt; </span>sink:&amp;:sink:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  len:num <span class="Special">&lt;-</span> length *contents
  <span class="Delimiter">{</span>
    done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    c:char <span class="Special">&lt;-</span> index *contents, i
    sink <span class="Special">&lt;-</span> write sink, c
    i <span class="Special">&lt;-</span> add i,<span class="Constant"> 1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  sink <span class="Special">&lt;-</span> close sink
]

<span class="muRecipe">def</span> start-writing resources:&amp;:resources, filename:text<span class="muRecipe"> -&gt; </span>sink:&amp;:sink:char, routine-id:num [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  source:&amp;:source:char, sink:&amp;:sink:char <span class="Special">&lt;-</span> new-channel<span class="Constant"> 30</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> resources
    <span class="Comment"># fake file system</span>
    routine-id <span class="Special">&lt;-</span> start-running transmit-to-fake-file resources, filename, source
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># real file system</span>
  file:num <span class="Special">&lt;-</span> $open-file-for-writing filename
  assert file, <span class="Constant">[no such file]</span>
  routine-id <span class="Special">&lt;-</span> start-running transmit-to-file file, source
]

<span class="muRecipe">def</span> transmit-to-file file:num, source:&amp;:source:char<span class="muRecipe"> -&gt; </span>source:&amp;:source:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  <span class="Delimiter">{</span>
    c:char, done?:bool, source <span class="Special">&lt;-</span> read source
    <span class="muControl">break-if</span> done?
    $write-to-file file, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  file <span class="Special">&lt;-</span> $close-file file
]

<span class="muRecipe">def</span> transmit-to-fake-file resources:&amp;:resources, filename:text, source:&amp;:source:char<span class="muRecipe"> -&gt; </span>resources:&amp;:resources, source:&amp;:source:char [
  <span class="Constant">local-scope</span>
  <span class="Constant">load-ingredients</span>
  lock:location <span class="Special">&lt;-</span> get-location *resources, <span class="Constant">lock:offset</span>
  wait-for-reset-then-set lock
  <span class="Comment"># compute new file contents</span>
  buf:&amp;:buffer <span class="Special">&lt;-</span> new-buffer<span class="Constant"> 30</span>
  <span class="Delimiter">{</span>
    c:char, done?:bool, source <span class="Special">&lt;-</span> read source
    <span class="muControl">break-if</span> done?
    buf <span class="Special">&lt;-</span> append buf, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  contents:text <span class="Special">&lt;-</span> buffer-to-array buf
  new-resource:resource <span class="Special">&lt;-</span> merge filename, contents
  <span class="Comment"># write to resources</span>
  curr-filename:text <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  data:&amp;:@:resource <span class="Special">&lt;-</span> get *resources, <span class="Constant">data:offset</span>
  <span class="Comment"># replace file contents if it already exists</span>
  i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  len:num <span class="Special">&lt;-</span> length *data
  <span class="Delimiter">{</span>
    done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    tmp:resource <span class="Special">&lt;-</span> index *data, i
    curr-filename <span class="Special">&lt;-</span> get tmp, <span class="Constant">name:offset</span>
    found?:bool <span class="Special">&lt;-</span> equal filename, curr-filename
    <span class="muControl">loop-unless</span> found?
    put-index *data, i, new-resource
    reset lock
    <span class="muControl">reply</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># if file didn't already exist, make room for it</span>
  new-len:num <span class="Special">&lt;-</span> add len,<span class="Constant"> 1</span>
  new-data:&amp;:@:resource <span class="Special">&lt;-</span> new <span class="Constant">resource:type</span>, new-len
  put *resources, <span class="Constant">data:offset</span>, new-data
  <span class="Comment"># copy over old files</span>
  i:num <span class="Special">&lt;-</span> copy<span class="Constant"> 0</span>
  <span class="Delimiter">{</span>
    done?:bool <span class="Special">&lt;-</span> greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    tmp:resource <span class="Special">&lt;-</span> index *data, i
    put-index *new-data, i, tmp
  <span class="Delimiter">}</span>
  <span class="Comment"># write new file</span>
  put-index *new-data, len, new-resource
  reset lock
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->